Skip to content

Commit

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

/**
* Middleware for error handling
* @param error The error object
* @param req The express request
* @param res The Express response
* @param next
* @returns
*/
export const errorMiddleware = (error, _req, res, _next) => {
if (error instanceof CustomError) {
if (typeof error.statusCode === 'number') {
res.status(error.statusCode).json({
message: error.message,
errors: error.errors,
stack: error.stack
});

return;
}
}

res.status(500).send('Internal server error');
res.status(500).send('Internal server error: ' + error.stack);
};
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import CustomError from '../errors/custom.error.js';

/**
* Middleware for error handling
* @param error The error object
* @param req The express request
* @param res The Express response
* @param next
* @returns
*/
export const errorMiddleware = (error, req, res, _next) => {
export const errorMiddleware = (error, _req, res, _next) => {
if (error instanceof CustomError) {
if (typeof error.statusCode === 'number') {
res.status(error.statusCode).json({
message: error.message,
errors: error.errors,
stack: error.stack
});

return;
}
}

res.status(500).send('Internal server error');
res.status(500).send('Internal server error: ' + error.stack);
};
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import CustomError from '../errors/custom.error.js';

/**
* Middleware for error handling
*/
export const errorMiddleware = (error, req, res, _next) => {
export const errorMiddleware = (error, _req, res, _next) => {
if (error instanceof CustomError) {
if (typeof error.statusCode === 'number') {
res.status(error.statusCode).json({
message: error.message,
errors: error.errors,
stack: error.stack
});

return;
}
}

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

/**
* Middleware for error handling
* @param error The error object
* @param req The express request
* @param res The Express response
* @param next
* @returns
*/
export const errorMiddleware = (
error: ErrorRequestHandler,

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

res.status(error.statusCode as number).json({
message: error.message,
errors: error.errors,
stack: error.stack,
});

return;
}

}

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

/**
* Middleware for error handling
* @param error The error object
* @param req The express request
* @param res The Express response
* @param next
* @returns
*/
export const errorMiddleware = (
error: ErrorRequestHandler,

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

res.status(error.statusCode as number).json({
message: error.message,
errors: error.errors,
stack: error.stack,
});

return;
}

}

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

/**
* Middleware for error handling
* @param error The error object
* @param req The express request
* @param res The Express response
* @param next
* @returns
*/
export const errorMiddleware = (
error: ErrorRequestHandler,

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

res.status(error.statusCode as number).json({
message: error.message,
errors: error.errors,
stack: error.stack,
});

return;
}

}

res.status(500).send('Internal server error');
res.status(500).send('Internal server error: ' + error.stack);
};

0 comments on commit 91109ff

Please sign in to comment.