Skip to content

Commit

Permalink
fix: response error global
Browse files Browse the repository at this point in the history
  • Loading branch information
masb0ymas committed Oct 31, 2020
1 parent f36b36b commit 9d8719d
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/middlewares/ExpressErrorResponse.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// eslint-disable-next-line no-unused-vars
import { NextFunction, Request, Response } from 'express'
import ResponseError from 'modules/ResponseError'
import ResponseError from 'modules/Response/ResponseError'
import { isObject } from 'lodash'

function generateErrorResponseError(e: Error) {
return isObject(e.message) ? e.message : { message: e.message }
function generateErrorResponseError(e: Error, code: Number) {
return isObject(e.message) ? e.message : { code, message: e.message }
}

async function ExpressErrorResponse(
Expand All @@ -14,7 +14,9 @@ async function ExpressErrorResponse(
next: NextFunction
) {
if (err instanceof ResponseError.BaseResponse) {
return res.status(err.statusCode).json(generateErrorResponseError(err))
return res
.status(err.statusCode)
.json(generateErrorResponseError(err, err.statusCode))
}

next(err)
Expand Down

0 comments on commit 9d8719d

Please sign in to comment.