Skip to main content

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 ParameterDescription
T extends stringThe 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

ParameterTypeDescription
params{ detail: string; instance: string; rootError: Error | Record<string, unknown>; status: number; title: string; type: T; }The parameters to initialize the error.
params.detail?stringA detailed explanation of the error.
params.instancestringA unique identifier for this specific error occurrence.
params.rootError?Error | Record<string, unknown>The original error, if any.
params.status?numberThe HTTP status code associated with the error.
params.title?stringA short summary of the error. Defaults to the type if not provided.
params.typeTThe specific error type identifier.

Returns

KeybanBaseError<T>

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

PropertyTypeDescription
detail?stringA 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."
instancestringA 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?numberThe 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
timestampstringA 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"
titlestringA 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"
typeTA 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.

NameType
detailundefined | string
instancestring
rootErrorundefined | Error | Record<string, unknown>
statusundefined | number
timestampstring
titlestring
typeT

Example

const errorJson = error.toJSON();
console.log(errorJson);