-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(templates): fix error middleware signature for better error handling
- Loading branch information
1 parent
7bb3267
commit 91109ff
Showing
6 changed files
with
32 additions
and
66 deletions.
There are no files selected for viewing
11 changes: 2 additions & 9 deletions
11
application-templates/javascript/event/src/middleware/error.middleware.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
13 changes: 3 additions & 10 deletions
13
application-templates/javascript/job/src/middleware/error.middleware.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
8 changes: 3 additions & 5 deletions
8
application-templates/javascript/service/src/middleware/error.middleware.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
22 changes: 8 additions & 14 deletions
22
application-templates/typescript/event/src/middleware/error.middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
22 changes: 8 additions & 14 deletions
22
application-templates/typescript/job/src/middleware/error.middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
22 changes: 8 additions & 14 deletions
22
application-templates/typescript/service/src/middleware/error.middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |