export interface ApplicationError extends Error { readonly code: C readonly data: D toJson(): ApplicationError.Literal } export namespace ApplicationError { export interface Literal { message: string data: D stack?: string } export interface Constructor { (...args: any[]): ApplicationError; code: C; is(arg: object | undefined): arg is ApplicationError } const codes: number[] = []; export function declare(code: C, factory: (...args: any[]) => Literal): Constructor { if (codes.indexOf(code) !== -1) { throw new Error(`An application error for '${code}' code is already declared`); } const constructorOpt = Object.assign((...args: any[]) => new Impl(code, factory(...args), constructorOpt), { code, is(arg: object | undefined): arg is ApplicationError { return arg instanceof Impl && arg.code === code; } }); return constructorOpt; } export function is(arg: object | undefined): arg is ApplicationError { return arg instanceof Impl; } export function fromJson(code: C, raw: Literal): ApplicationError { return new Impl(code, raw); } class Impl extends Error implements ApplicationError { readonly data: D; constructor( readonly code: C, raw: ApplicationError.Literal, constructorOpt?: Function ) { super(raw.message); this.data = raw.data; Object.setPrototypeOf(this, Impl.prototype); if (raw.stack) { this.stack = raw.stack; } else if (Error.captureStackTrace && constructorOpt) { Error.captureStackTrace(this, constructorOpt); } } toJson(): ApplicationError.Literal { const { message, data, stack } = this; return { message, data, stack }; } } }