https://github.com/jangbl/express-error-handling-typescript
Error handling with express and Typescript
https://github.com/jangbl/express-error-handling-typescript
Last synced: 2 months ago
JSON representation
Error handling with express and Typescript
- Host: GitHub
- URL: https://github.com/jangbl/express-error-handling-typescript
- Owner: jangbl
- Created: 2022-06-29T20:34:22.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-13T10:59:05.000Z (over 2 years ago)
- Last Synced: 2025-02-22T07:16:23.452Z (11 months ago)
- Language: TypeScript
- Size: 33.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Express Error Handling with Node and Typescript
This repo demonstrates how to implement easy-to maintain and extend error handling for an [express](https://github.com/expressjs/express) application with Typescript.
The idea is to create a new subclass for each error type and to return additional fields in the response. Adding a `errorType` property ensures that the client can parse and deserialize the response from the server correctly.
Please help this repo with a ⭐️ if you find it useful! 😁
For updates, please follow [@_jgoebel](https://twitter.com/_jgoebel) on Twitter.
## How to run this project
```
npm i
npm run dev
```
## Endpoints
### `POST /withdraw`
calling this endpoint will throw an `InsufficientFundsError` and return
```
[
{
"requestedAmount": 200,
"availableAmount": 100,
"message": "Insufficient funds: attempted to disburse 200, but there is only 100 available.",
"errorType": "INSUFFICIENT_FUNDS_ERROR"
}
]
```
### `POST /withdraw-async`
The same endpoint as `POST /withdraw` - but with an async handler function. If you attempt to throw within an async handler, you need to make sure that you have the
[express-async-errors](https://github.com/davidbanham/express-async-errors) package installed.