Comments (2)
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.
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)
- docs: Re-instantiate Typedocs for `hidden` exported functions HOT 2
- Exceeding Maximum Number of Outputs in `estimateTxDependencies`
- Exceeding Maximum Number of Inputs When Funding a Transaction HOT 3
- Implement connectors spec changes
- Fire the `build` and `deploy` events during `fuels dev`
- Seek better tooling integration by using `fuel-toolchain.toml` HOT 1
- Use `fuel-toolchain.toml` in our templates HOT 1
- Incorrect testnet URL
- Add `testnet` to the chain IDs HOT 1
- Add Documentation Reference For `getMintedAssetId` Helper
- isReadOnly not available in Contract Methods
- Problem with a test of account HOT 5
- setConfigurableConstants Error HOT 1
- Deploying a contract doesn't work with the metamask connector
- Doc: Create `Connectors` section
- Pagination to `Account` methods
- Unskip e2e tests
- `txSummary` not returning contract call operation in some cases
- CallReceipt is returning wrong `from` property
- Rename the Provider `call` Method to `dryRun`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fuels-ts.