Skip to content

Commit

Permalink
Merge pull request #6 from santoshshinde2012/bugfixes/error-handler
Browse files Browse the repository at this point in the history
Fixed the error handler
  • Loading branch information
santoshshinde2012 authored Jul 7, 2021
2 parents 89cdc81 + b43177b commit 65ed00b
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/middleware/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import * as express from 'express';
import * as util from 'util';
import logger from '../lib/logger';
import ApiError from '../abstractions/ApiError';
import { Environments } from '../environments/environment.constant';

const addErrorHandler = (
err: ApiError, req: express.Request,
res: express.Response,
next: express.NextFunction,
): void => {

if (err) {
const status: number = err.status || 500;
logger.debug(`REQUEST HANDLING ERROR:
Expand All @@ -23,7 +25,12 @@ const addErrorHandler = (
status,
stack: '',
};
body.stack = err.stack;

// If the environment is production then no need to send error stack trace
if(process.env.NODE_ENV === Environments.PRODUCTION) {
body.stack = err.stack;
}

res.status(status).json(body);
}
next();
Expand Down

0 comments on commit 65ed00b

Please sign in to comment.