UNPKG

@httpc/kit

Version:

httpc toolbox for building function-based API with minimal code and end-to-end type safety

30 lines (29 loc) 986 B
import { HttpCallError } from "@httpc/server"; export class ServiceError extends HttpCallError { constructor(error, message, data) { const isInfo = typeof error === "object"; const status = isInfo && error.status || 400; const errorCode = isInfo ? error.errorCode : error; data = isInfo ? error.data : typeof message === "string" ? data : message; message = isInfo ? error.message : typeof message === "string" ? message : undefined; super({ status, errorCode, message, data, }); } } export class ServiceErrorPresets { constructor(map = new Map()) { this.map = map; } add(errorCode, errorInfo) { const map = new Map(this.map); map.set(errorCode, { status: 500, ...errorInfo, errorCode }); return new ServiceErrorPresets(map); } get(errorCode) { return this.map.get(errorCode); } }