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
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);