Skip to content

Commit

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

export const errorMiddleware = (error, _req, res) => {
const isDevelopment = process.env.NODE_ENV === 'development';

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

return;
}
return;
}

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

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

export const errorMiddleware = (error, _req, res) => {
const isDevelopment = process.env.NODE_ENV === 'development';

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

return;
}
return;
}

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

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

export const errorMiddleware = (error, _req, res) => {
const isDevelopment = process.env.NODE_ENV === 'development';

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

return;
}
return;
}

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

res.status(500).send(isDevelopment ? error : 'Internal server error');
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import CustomError from '../errors/custom.error';
export const errorMiddleware: ErrorRequestHandler = (
error: Error,
_: Request,
res: Response,
res: Response
) => {
const isDevelopment = process.env.NODE_ENV === 'development';

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

return;
}

res.status(500).send('Internal server error: ' + process.env.NODE_ENV === 'development' ? error.stack : undefined);
};
res.status(500).send(isDevelopment ? error : 'Internal server error');
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import CustomError from '../errors/custom.error';
export const errorMiddleware: ErrorRequestHandler = (
error: Error,
_: Request,
res: Response,
res: Response
) => {
const isDevelopment = process.env.NODE_ENV === 'development';

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

return;
}

res.status(500).send('Internal server error: ' + process.env.NODE_ENV === 'development' ? error.stack : undefined);
};
res.status(500).send(isDevelopment ? error : 'Internal server error');
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import CustomError from '../errors/custom.error';
export const errorMiddleware: ErrorRequestHandler = (
error: Error,
_: Request,
res: Response,
res: Response
) => {
const isDevelopment = process.env.NODE_ENV === 'development';

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

return;
}

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

0 comments on commit 05e4927

Please sign in to comment.