Skip to content

Commit

Permalink
fix(templates): fix error middleware handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ruidias-commercetools committed Jul 16, 2024
1 parent 91109ff commit d4d194a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import CustomError from '../errors/custom.error.js';

export const errorMiddleware = (error, _req, res, _next) => {
export const errorMiddleware = (error, _req, res) => {
if (error instanceof CustomError) {
if (typeof error.statusCode === 'number') {
res.status(error.statusCode).json({
message: error.message,
errors: error.errors,
stack: error.stack
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined,
});

return;
}
}

res.status(500).send('Internal server error: ' + error.stack);
res.status(500).send('Internal server error: ' + process.env.NODE_ENV === 'development' ? error.stack : undefined);
};

Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import CustomError from '../errors/custom.error.js';

export const errorMiddleware = (error, _req, res, _next) => {
export const errorMiddleware = (error, _req, res) => {
if (error instanceof CustomError) {
if (typeof error.statusCode === 'number') {
res.status(error.statusCode).json({
message: error.message,
errors: error.errors,
stack: error.stack
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined,
});

return;
}
}

res.status(500).send('Internal server error: ' + error.stack);
res.status(500).send('Internal server error: ' + process.env.NODE_ENV === 'development' ? error.stack : undefined);
};

Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import CustomError from '../errors/custom.error.js';

export const errorMiddleware = (error, _req, res, _next) => {
export const errorMiddleware = (error, _req, res) => {
if (error instanceof CustomError) {
if (typeof error.statusCode === 'number') {
res.status(error.statusCode).json({
message: error.message,
errors: error.errors,
stack: error.stack
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined,
});

return;
}
}

res.status(500).send('Internal server error: ' + error.stack);
res.status(500).send('Internal server error: ' + process.env.NODE_ENV === 'development' ? error.stack : undefined);
};

Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import { ErrorRequestHandler, NextFunction, Request, Response } from 'express';
import { ErrorRequestHandler, Request, Response } from 'express';
import CustomError from '../errors/custom.error';


export const errorMiddleware: ErrorRequestHandler = (
error: Error,
req: Request,
_: Request,
res: Response,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
next: NextFunction
) => {
if (error instanceof CustomError) {

res.status(error.statusCode as number).json({
message: error.message,
errors: error.errors,
stack: error.stack,
});
res.status(error.statusCode as number).json({
message: error.message,
errors: error.errors,
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined,
});

return;

return;
}

res.status(500).send('Internal server error: ' + error.stack);
res.status(500).send('Internal server error: ' + process.env.NODE_ENV === 'development' ? error.stack : undefined);
};
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import { ErrorRequestHandler, NextFunction, Request, Response } from 'express';
import { ErrorRequestHandler, Request, Response } from 'express';
import CustomError from '../errors/custom.error';


export const errorMiddleware: ErrorRequestHandler = (
error: Error,
req: Request,
_: Request,
res: Response,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
next: NextFunction
) => {
if (error instanceof CustomError) {

res.status(error.statusCode as number).json({
message: error.message,
errors: error.errors,
stack: error.stack,
});
res.status(error.statusCode as number).json({
message: error.message,
errors: error.errors,
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined,
});

return;

return;
}

res.status(500).send('Internal server error: ' + error.stack);
res.status(500).send('Internal server error: ' + process.env.NODE_ENV === 'development' ? error.stack : undefined);
};
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import { ErrorRequestHandler, NextFunction, Request, Response } from 'express';
import { ErrorRequestHandler, Request, Response } from 'express';
import CustomError from '../errors/custom.error';


export const errorMiddleware: ErrorRequestHandler = (
error: Error,
req: Request,
_: Request,
res: Response,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
next: NextFunction
) => {
if (error instanceof CustomError) {

res.status(error.statusCode as number).json({
message: error.message,
errors: error.errors,
stack: error.stack,
});
res.status(error.statusCode as number).json({
message: error.message,
errors: error.errors,
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined,
});

return;

return;
}

res.status(500).send('Internal server error: ' + error.stack);
res.status(500).send('Internal server error: ' + process.env.NODE_ENV === 'development' ? error.stack : undefined);
};
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d4d194a

Please sign in to comment.