Class.KeybanBaseError
The KeybanBaseError
class serves as the foundational structure for all custom errors within the Keyban SDK.
It extends the native JavaScript Error
class, providing additional properties to enrich error handling
with more context and information.
Remarks
This class is intended to be extended by more specific error classes that define particular error types. It standardizes error information, making it easier to handle and debug errors across the SDK.
Example
import { KeybanBaseError, SdkErrorTypes } from '@keyban/sdk';
class AddressInvalidError extends KeybanBaseError<SdkErrorTypes.AddressInvalid> {
constructor(instance: string, detail?: string) {
super({
type: SdkErrorTypes.AddressInvalid,
instance,
detail,
title: "Invalid Ethereum Address",
status: 400,
});
}
}
// Usage in a function
function validateAddress(address: string) {
if (!isValidEthereumAddress(address)) {
throw new AddressInvalidError("validateAddress-001", "The provided Ethereum address is not valid.");
}
}
Extends
Error
Extended by
Type Parameters
Type Parameter | Description |
---|---|
T extends string | The enum type representing the specific error category. |
Constructors
new KeybanBaseError()
new KeybanBaseError<T>(params: {
detail: string;
instance: string;
rootError: Error | Record<string, unknown>;
status: number;
title: string;
type: T;
}): KeybanBaseError<T>
Creates an instance of KeybanBaseError
. It initializes the error with the provided properties,
sets the error message, and records the timestamp.
Parameters
Parameter | Type | Description |
---|---|---|
params | { detail : string ; instance : string ; rootError : Error | Record <string , unknown >; status : number ; title : string ; type : T ; } | The parameters to initialize the error. |
params.detail ? | string | A detailed explanation of the error. |
params.instance | string | A unique identifier for this specific error occurrence. |
params.rootError ? | Error | Record <string , unknown > | The original error, if any. |
params.status ? | number | The HTTP status code associated with the error. |
params.title ? | string | A short summary of the error. Defaults to the type if not provided. |
params.type | T | The specific error type identifier. |
Returns
Example
const error = new KeybanBaseError({
type: SdkErrorTypes.AddressInvalid,
instance: "validateAddress-001",
detail: "The provided Ethereum address is not valid.",
status: 400,
});
Overrides
Error.constructor
Properties
Property | Type | Description |
---|---|---|
detail? | string | A human-readable explanation specific to this occurrence of the problem. This field helps the client understand and potentially correct the issue. Example error.detail // "The provided Ethereum address is not valid." |
instance | string | A URI reference that identifies the specific occurrence of the problem. This provides a unique identifier for the particular instance of the error. Example error.instance // "validateAddress-001" |
rootError? | Error | Record <string , unknown > | The original error instance, if any. This can be used to trace the root cause of the error. Example error.rootError // Original Error instance or additional error details |
status? | number | The HTTP status code generated by the origin server for this occurrence of the problem. It is a numeric value and is included for the convenience of the client. Example error.status // 400 |
timestamp | string | A timestamp recording the exact date and time when the exception occurred, in ISO8601 format (YYYY-MM-DDTHH:mm:ss.sssZ ). Example error.timestamp // "2024-04-27T12:34:56.789Z" |
title | string | A short, human-readable summary of the problem type. It should remain consistent across occurrences of the problem except for localization purposes. Example error.title // "Invalid Ethereum Address" |
type | T | A URI reference that identifies the problem type. This property is mandatory and provides a machine-readable identifier for the error. Example error.type // "https://api.keyban.io/errors/address-invalid" |
Methods
toJSON()
toJSON(): {
detail: undefined | string;
instance: string;
rootError: undefined | Error | Record<string, unknown>;
status: undefined | number;
timestamp: string;
title: string;
type: T;
}
Serializes the error instance into a JSON object, including all relevant properties. This is useful for logging, debugging, or transmitting error information.
Returns
{
detail: undefined | string;
instance: string;
rootError: undefined | Error | Record<string, unknown>;
status: undefined | number;
timestamp: string;
title: string;
type: T;
}
A JSON representation of the error.
Name | Type |
---|---|
detail | undefined | string |
instance | string |
rootError | undefined | Error | Record <string , unknown > |
status | undefined | number |
timestamp | string |
title | string |
type | T |
Example
const errorJson = error.toJSON();
console.log(errorJson);