@gala-chain/api ∙ API
API > ChainError
Class: abstract ChainError
Contents
- Extends
- Implements
- Constructors
- new ChainError(message)
- new ChainError(message, key)
- new ChainError(message, key, payload)
- new ChainError(message, payload)
- Properties
- code
- key
- message
- name
- payload
- stack
- stackTraceLimit
- Methods
- andExec()
- logError()
- logWarn()
- map()
- matches()
- captureStackTrace()
- from()
- ignore()
- isChainError()
- map()
- matches()
- normalizedKey()
- prepareStackTrace()
- recover()
- withCode()
Extends
Error
Implements
Constructors
new ChainError(message)
new ChainError(
message):ChainError
Parameters
▪ message: string
Overrides
Error.constructor
Source
chain-api/src/utils/error.ts:60
new ChainError(message, key)
new ChainError(
message,key):ChainError
Parameters
▪ message: string
▪ key: Uppercase\<string>
Overrides
Error.constructor
Source
chain-api/src/utils/error.ts:61
new ChainError(message, key, payload)
new ChainError(
message,key,payload):ChainError
Parameters
▪ message: string
▪ key: Uppercase\<string>
▪ payload: Record\<string, unknown>
Overrides
Error.constructor
Source
chain-api/src/utils/error.ts:62
new ChainError(message, payload)
new ChainError(
message,payload):ChainError
Parameters
▪ message: string
▪ payload: unknown
Overrides
Error.constructor
Source
chain-api/src/utils/error.ts:63
Properties
code
readonlycode:ErrorCode
Status code, a value from ErrorCode enum. It is directly mapped to HTTP, status, it is a constant value to be used by clients integrating with the chain.
Implementation of
Source
chain-api/src/utils/error.ts:46
key
readonlykey:Uppercase\<string>
An upper case string to be used as a key do diagnose where the error comes from and help with regular development. It should not be used by client integrating with the chain since we don't guarantee it won't change. It is generated from original error class name.
Implementation of
Source
chain-api/src/utils/error.ts:54
message
message:
string
Implementation of
OptionalChainErrorData.message
Inherited from
Error.message
Source
node_modules/typescript/lib/lib.es5.d.ts:1076
name
name:
string
Inherited from
Error.name
Source
node_modules/typescript/lib/lib.es5.d.ts:1075
payload
readonlypayload?:Record\<string,unknown>
Additional information to be used by
Implementation of
OptionalChainErrorData.payload
Source
chain-api/src/utils/error.ts:59
stack
stack?:
string
Inherited from
Error.stack
Source
node_modules/typescript/lib/lib.es5.d.ts:1077
stackTraceLimit
staticstackTraceLimit:number
The Error.stackTraceLimit property specifies the number of stack frames
collected by a stack trace (whether generated by new Error().stack or
Error.captureStackTrace(obj)).
The default value is 10 but may be set to any valid JavaScript number. Changes
will affect any stack trace captured after the value has been changed.
If set to a non-number value, or set to a negative number, stack traces will not capture any frames.
Inherited from
Error.stackTraceLimit
Source
node_modules/@types/node/globals.d.ts:68
Methods
andExec()
andExec(
fn):ChainError
Allows to execute function getting as a parameter the current error.
Parameters
▪ fn: (e) => void
Returns
Example
Source
chain-api/src/utils/error.ts:115
logError()
logError(
logger):ChainError
Parameters
▪ logger: object
▪ logger.error
Source
chain-api/src/utils/error.ts:120
logWarn()
logWarn(
logger):ChainError
Parameters
▪ logger: object
▪ logger.warn
Source
chain-api/src/utils/error.ts:125
map()
map(
key,newError):ChainError
Maps ChainError to another chain error by error code if key param matches
current error code or current diagnostic key. Otherwise, returns original
error.
Useful in rethrowing an error or mapping an error to another one in catch clauses or catch methods in promises.
Parameters
▪ key: ErrorCode | ClassConstructor\<ChainError>
error code or error class to match
▪ newError: ChainError | (e) => ChainError
new error or a function to create the new error
Source
chain-api/src/utils/error.ts:149
matches()
matches(
key):boolean
Parameters
▪ key: ErrorCode | ClassConstructor\<ChainError>
Source
chain-api/src/utils/error.ts:130
captureStackTrace()
staticcaptureStackTrace(targetObject,constructorOpt?):void
Creates a .stack property on targetObject, which when accessed returns
a string representing the location in the code at which
Error.captureStackTrace() was called.
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack; // Similar to `new Error().stack`
The first line of the trace will be prefixed with
${myObject.name}: ${myObject.message}.
The optional constructorOpt argument accepts a function. If given, all frames
above constructorOpt, including constructorOpt, will be omitted from the
generated stack trace.
The constructorOpt argument is useful for hiding implementation
details of error generation from the user. For instance:
function a() {
b();
}
function b() {
c();
}
function c() {
// Create an error without stack trace to avoid calculating the stack trace twice.
const { stackTraceLimit } = Error;
Error.stackTraceLimit = 0;
const error = new Error();
Error.stackTraceLimit = stackTraceLimit;
// Capture the stack trace above function b
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
throw error;
}
a();
Parameters
▪ targetObject: object
▪ constructorOpt?: Function
Inherited from
Error.captureStackTrace
Source
node_modules/@types/node/globals.d.ts:52
from()
staticfrom(e):ChainError
Parameters
▪ e: object & object
Source
chain-api/src/utils/error.ts:169
ignore()
staticignore\<T>(e,key,defaultValue?):T
Recovers (mutes) ChainError to a specified return value or undefined.
Type parameters
▪ T = undefined
Parameters
▪ e: ChainError | object
▪ key: ErrorCode | ClassConstructor\<ChainError>
▪ defaultValue?: T
Returns
Deprecated
Use ChainError.recover instead.
Source
chain-api/src/utils/error.ts:247
isChainError()
staticisChainError(e):e is ChainError
Parameters
▪ e: undefined | object
Source
chain-api/src/utils/error.ts:165
map()
staticmap(e,key,newError):ChainError
Maps ChainError to another chain error by error code, or returns original error if no error code matches, or returns default chain error if a given parameter is not a ChainError instance.
Useful in rethrowing an error or mapping an error to another one in catch clauses or catch methods in promises.
Parameters
▪ e: ChainError | object
original error
▪ key: ErrorCode | ClassConstructor\<ChainError>
error code or error class to match
▪ newError: ChainError | (e) => ChainError
new error or a function to create the new error
Source
chain-api/src/utils/error.ts:192
matches()
staticmatches(e,key):boolean
Parameters
▪ e: ChainError | object
▪ key: ErrorCode | ClassConstructor\<ChainError>
Source
chain-api/src/utils/error.ts:173
normalizedKey()
staticnormalizedKey(fn):Uppercase\<string>
Parameters
▪ fn: string | Function
Source
chain-api/src/utils/error.ts:81
prepareStackTrace()
staticprepareStackTrace(err,stackTraces):any
Parameters
▪ err: Error
▪ stackTraces: CallSite[]
Returns
Inherited from
Error.prepareStackTrace
See
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
Source
node_modules/@types/node/globals.d.ts:56
recover()
staticrecover\<T>(e,key,defaultValue?):T
Recovers (mutes) ChainError to a specified return value or undefined.
If the error is a ChainError and matches the error code, the error is recovered and the specified return value is returned. Otherwise, the error is re-thrown.
For instance when you want to get an object from chain, and instead of throwing a NOT_FOUND error, you want to return undefined:
If you want to return a default value instead of undefined, you can do:
Type parameters
▪ T = undefined
Parameters
▪ e: ChainError | object
original error
▪ key: ErrorCode | ClassConstructor\<ChainError>
error code or error class to match
▪ defaultValue?: T
value to be returned if error code matches
Source
chain-api/src/utils/error.ts:230
withCode()
staticwithCode(code):ClassConstructor\<ChainError>
Parameters
▪ code: ErrorCode