UNPKG

@mark01/express-utils

Version:

npm package that contains utilities for express.js

25 lines (24 loc) 742 B
import { getReasonPhrase } from 'http-status-codes'; import { nanoid } from 'nanoid'; import { asError } from '../util/catch-unknown'; export class AppError extends Error { id; title; status; statusCode; isOperational; type; error; constructor(message, statusCode, error) { const safeError = asError(error || new Error()); super(message, safeError); this.id = nanoid(); this.title = getReasonPhrase(statusCode); this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error'; this.statusCode = statusCode; this.isOperational = true; this.type = 'operational.error'; this.error = safeError; this.stack = safeError.stack; } }