-
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.
feat(templates): fix the error handling on middleware
- Loading branch information
1 parent
d4d194a
commit 05e4927
Showing
6 changed files
with
48 additions
and
45 deletions.
There are no files selected for viewing
21 changes: 10 additions & 11 deletions
21
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,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'); | ||
}; |
21 changes: 10 additions & 11 deletions
21
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,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'); | ||
}; |
21 changes: 10 additions & 11 deletions
21
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,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'); | ||
}; |
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
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
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