-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
63 additions
and
19 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ErrorRequestHandler } from "express"; | ||
import { DrizzleError } from "drizzle-orm"; | ||
import { JSendException } from "@/utils/exception"; | ||
|
||
/** | ||
* Handles all Drizzle errors and converts them to JSend exceptions | ||
*/ | ||
const handler: ErrorRequestHandler = (err, req, res, next) => { | ||
if (err instanceof DrizzleError) { | ||
return next( | ||
new JSendException(500, { | ||
status: "error", | ||
message: err.message, | ||
}) | ||
); | ||
} | ||
|
||
next(err); | ||
}; | ||
|
||
export default handler; |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { JSendException } from "@/utils/exception"; | ||
import { JSend } from "@/utils/jsend"; | ||
import type { ErrorRequestHandler } from "express"; | ||
|
||
/** | ||
* Global error handler for the application. This function should be the last middleware in the chain | ||
* to catch any errors that were not handled by the other middlewares | ||
* | ||
* You can use JSendException to throw errors from your controllers and services | ||
*/ | ||
const globalErrorHandler: ErrorRequestHandler = (err, req, res, next) => { | ||
if (err instanceof JSendException) { | ||
return res.status(err.status).json(err.response); | ||
} | ||
|
||
console.error(err); | ||
|
||
/** | ||
* Any errors that were not manually throw with JSendException will return a 500 status code | ||
* | ||
* Ideally, the API should not reach this point. Instead, all errors should be handled by | ||
* throwing JSendException with the appropriate status code and response | ||
* | ||
* A good pattern for that is creating a chain of error handlers to handle different types of | ||
* errors that are not manually throwed (database errors, ...) and convert them to | ||
* JSendException before reaching the global error handler. This is done, for example, in the | ||
* `src/errors/database.handler.ts` file | ||
*/ | ||
res.status(500).json(JSend.error("Internal Server Error")); | ||
}; | ||
|
||
export default globalErrorHandler; |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// This is only an example file. You can safely delete it | ||
|
||
export function getHelloMessage(name: string, prefix: string = "Hello") { | ||
return `${prefix}, ${name}!`; | ||
} |
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,4 +1,5 @@ | ||
{ | ||
"exclude": ["node_modules"], | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"paths": { | ||
|