UNPKG

steady-api

Version:

Configurable REST API built with Express and TypeScript

54 lines (47 loc) 1.08 kB
import { Request, Response } from 'express'; export interface IErrorResponseOptions { req?: Request res?: Response errorMessage: string errors: string[] status: number } export interface IErrorData { errorMessage: string errors: string[] status: number } export class ErrorResponse { private req: Request; private res: Response; private status: number; private errorMessage: string; private errors: string[]; constructor(options: IErrorResponseOptions) { this.req = options.req; this.res = options.res; this.status = options.status; this.errorMessage = options.errorMessage; this.errors = options.errors; } public send(): void { const requestInfo = { request: { method: this.req.method, url: this.req.url, body: this.req.body || {}, query: this.req.query || {} } } this.res.status(this.status).send( Object.assign( {}, { errorMessage: this.errorMessage, errors: this.errors }, requestInfo ) ) } }