Skip to content

Commit

Permalink
fix(filters): Add debug mode for all errors handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
EndyKaufman committed Jan 17, 2018
1 parent afe4933 commit efc0c53
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/libs/core/exceptions/custom-exception.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import { CustomValidationError } from './custom-validation.error';

@Catch(SyntaxError, CustomValidationError, JsonWebTokenError, Error)
export class CustomExceptionFilter implements ExceptionFilter {
catch(exception: CustomValidationError | JsonWebTokenError, response) {
catch(exception: CustomValidationError | JsonWebTokenError | SyntaxError | Error, response) {
const errors = {};
if (exception instanceof CustomValidationError) {
if (process.env.DEBUG === 'true' || process.env.DEBUG === '1') {
response.status(HttpStatus.BAD_REQUEST).json(exception);
return;
}
exception.errors.forEach((error: ValidationError) => {
Object.keys(error.constraints).forEach(
(key: string) => {
Expand All @@ -23,16 +27,24 @@ export class CustomExceptionFilter implements ExceptionFilter {
return;
}
if (exception instanceof JsonWebTokenError) {
if (process.env.DEBUG === 'true' || process.env.DEBUG === '1') {
response.status(HttpStatus.BAD_REQUEST).json(exception);
return;
}
response.status(HttpStatus.BAD_REQUEST).json({
nonFieldErrors: [exception.message]
message: exception.message
});
return;
}
if (process.env.DEBUG === 'true' || process.env.DEBUG === '1') {
console.error(exception);
response.status(HttpStatus.BAD_REQUEST).json({ message: String(exception) });
if (exception instanceof SyntaxError || exception instanceof Error) {
if (process.env.DEBUG === 'true' || process.env.DEBUG === '1') {
response.status(HttpStatus.BAD_REQUEST).json(exception);
return;
}
response.status(HttpStatus.BAD_REQUEST).json({
message: exception.message ? exception.message : String(exception)
});
return;
}
response.status(HttpStatus.BAD_REQUEST).json(exception);
}
}

0 comments on commit efc0c53

Please sign in to comment.