Coder Social home page Coder Social logo

Comments (2)

arboleya avatar arboleya commented on June 6, 2024 1

Let's not worry about code ownership; it will only worsen the problem.

Having everything condensed is easier to maintain.

Error handling can be simple.


Single package:

packages/errors
└── src
    ├── error-codes.ts
    └── index.ts

Single enum:

export enum ErrorCodes {
  // provider
  INVALID_URL = 'invalid-url',

  // wallet
  INSUFFICIENT_BALANCE = 'insufficient-balance',

  // coder
  // ...
}

Single class:

import { ErrorCodes } from './error-codes';

export * from './error-codes';

export class FuelError extends Error {
  static readonly CODES = ErrorCodes;
  static parse(e: unknown) {
    return e as FuelError;
  }

  code: ErrorCodes;
  constructor(code: ErrorCodes, message: string) {
    super(message);
    this.code = code;
  }
}

Internal usage:

import { FuelError, ErrorCodes } from '@fuel-ts/error';

export function singleImport() {
  throw new FuelError(FuelError.CODES.INVALID_URL, 'Invalid URL');
}

export function multipleImports() {
  throw new FuelError(ErrorCodes.INVALID_URL, 'Invalid URL');
}

External usage:

import { FuelError, Provider } from 'fuels';

type Locale = 'PT_BR' | 'BS_BA';

const currentLocale: Locale = 'PT_BR';

const i18nDict = {
  PT_BR: {
    [FuelError.CODES.INVALID_URL]: 'Endereço inválido',
    [FuelError.CODES.INSUFFICIENT_BALANCE]: 'Saldo insuficiente',
  },
  BS_BA: {
    [FuelError.CODES.INVALID_URL]: 'Nevažeća adresa',
    [FuelError.CODES.INSUFFICIENT_BALANCE]: 'Nedovoljan balans',
  },
};

function translateError(e: unknown) {
  const { code } = FuelError.parse(e)
  return i18nDict[currentLocale][code];
}

(function main() {
  try {
    const p = new Provider('0004:tƨoʜlɒɔol//:qttʜ');
    console.log(p);
  } catch (e) {
    const prettyError = translateError(e);
    console.log({ prettyError });
  }
})();

from fuels-ts.

nedsalk avatar nedsalk commented on June 6, 2024

A possible solution for error code ownership is to make the SdkError abstract and have each package define their own package error:

// sdk-error.ts

export abstract class SdkError<T extends string> extends Error {
  code: T;
  constructor(code: T, message?: string) {
    super(message);
    this.code = code;
  }
}
// abi-coder/abi-coder-error.ts

type ErrorCode = 'InvalidData' | 'TypeNotFoundOnAbi';

export class AbiCoderError extends SdkError<ErrorCode> {
  constructor(code: ErrorCode, additional_message?: string) {
    super(code, additional_message);

    this.name = 'SdkError/AbiCoder';
  }
}

And the same in other packages (ProviderError, ContractError, ...).

This would strike a middle line between excessive stub error classes like TransactionNotFoundError and having one class to rule them all - SdkError - that knows more than it should know.

from fuels-ts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.