UNPKG

@statezero/core

Version:

The type-safe frontend client for StateZero - connect directly to your backend models with zero boilerplate

139 lines (138 loc) 4.15 kB
/** * Parses a JSON error response from the backend and returns an instance * of the corresponding custom error. * * @param {IErrorResponse} errorResponse - The error response JSON. * @returns {StateZeroError} An instance of a StateZeroError subclass. */ export function parseStateZeroError(errorResponse: IErrorResponse): StateZeroError; /** * @typedef {Object} IErrorResponse * @property {number} status - The HTTP status code. * @property {string} type - The error type. * @property {*} detail - The error details. */ /** * @typedef {Object} IErrorDetail * @property {string} message - The error message. * @property {string} code - The error code. */ /** * Base error class for StateZero errors. */ export class StateZeroError extends Error { /** * Creates a new StateZeroError. * * @param {string} message - The error message. * @param {string} code - The error code. * @param {IErrorDetail|Object|string} detail - The error details. * @param {number} status - The HTTP status code. */ constructor(message: string, code: string, detail: IErrorDetail | Object | string, status: number); code: string; detail: string | Object | IErrorDetail; status: number; /** * Returns a full error message including the detail. * * @returns {string} The full error message with details */ getFullMessage(): string; } /** * Error class for validation errors. */ export class ValidationError extends StateZeroError { /** * Creates a new ValidationError. * * @param {IErrorDetail|Object|string} detail - The error details. * @param {number} [status=400] - The HTTP status code. */ constructor(detail: IErrorDetail | Object | string, status?: number); } /** * Error class for "Does Not Exist" errors (renamed from NotFound). */ export class DoesNotExist extends StateZeroError { /** * Creates a new DoesNotExist error. * * @param {IErrorDetail|Object|string} [detail="Does not exist"] - The error details. * @param {number} [status=404] - The HTTP status code. */ constructor(detail?: IErrorDetail | Object | string, status?: number); } /** * Error class for permission denied errors. */ export class PermissionDenied extends StateZeroError { /** * Creates a new PermissionDenied error. * * @param {IErrorDetail|Object|string} [detail="Permission denied"] - The error details. * @param {number} [status=403] - The HTTP status code. */ constructor(detail?: IErrorDetail | Object | string, status?: number); } /** * Error class for multiple objects returned errors. */ export class MultipleObjectsReturned extends StateZeroError { /** * Creates a new MultipleObjectsReturned error. * * @param {IErrorDetail|Object|string} [detail="Multiple objects returned"] - The error details. * @param {number} [status=500] - The HTTP status code. */ constructor(detail?: IErrorDetail | Object | string, status?: number); } /** * Error class for AST validation errors. */ export class ASTValidationError extends StateZeroError { /** * Creates a new ASTValidationError. * * @param {IErrorDetail|Object|string} detail - The error details. * @param {number} [status=400] - The HTTP status code. */ constructor(detail: IErrorDetail | Object | string, status?: number); } /** * Error class for configuration errors. */ export class ConfigError extends StateZeroError { /** * Creates a new ConfigError. * * @param {IErrorDetail|Object|string} detail - The error details. * @param {number} [status=500] - The HTTP status code. */ constructor(detail: IErrorDetail | Object | string, status?: number); } export type IErrorResponse = { /** * - The HTTP status code. */ status: number; /** * - The error type. */ type: string; /** * - The error details. */ detail: any; }; export type IErrorDetail = { /** * - The error message. */ message: string; /** * - The error code. */ code: string; };