Skip to content

Commit cc93ffd

Browse files
committed
Update readme
1 parent 81222d1 commit cc93ffd

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

README.md

+12-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# @csnw/express-async-errors
22

3-
An async/await patch for [Express](https://expressjs.com/) error handlers. Async functions already work fine in Express, this module improves support for thrown errors.
4-
5-
This package is compatible with both ESM and CommonJS projects. It additionally differs from [express-async-errors](https://www.npmjs.com/package/express-async-errors) by expecting the patch function be called explicitly instead of when the module is `require`'d.
3+
This module patches [Express](https://expressjs.com/) to better support errors thrown from async request handlers. The primary difference of this package from [express-async-errors](https://www.npmjs.com/package/express-async-errors) is that you are expected to call the patch function explicitly, rather than it applying as soon as the module is `require`'d or `import`'ed. Both ESM and CommonJS projects are supported.
64

75
This is a fork of a fork; thank you to the original authors:
86

@@ -15,7 +13,8 @@ This is a fork of a fork; thank you to the original authors:
1513
npm install @csnw/express-async-errors --save
1614
```
1715

18-
Run the patch before building `express()`. For example:
16+
Run the patch before building `express()`.
17+
Example:
1918

2019
```js
2120
import express from 'express';
@@ -25,32 +24,29 @@ import expressAsyncErrors from '@csnw/express-async-errors';
2524
expressAsyncErrors();
2625
const app = express();
2726

28-
app.get('/version', async (req, res) => {
29-
const version = parseInt(req.query.v);
30-
if (isNaN(version)) {
27+
app.get('/delay', async (req, res) => {
28+
const ms = parseInt(req.query.ms);
29+
if (isNaN(ms)) {
3130
// Throw error from async request handler
32-
throw new Error('version should be a number');
31+
throw new Error('bad request: ms should be a number');
3332
}
34-
res.status(200).json({ version });
33+
await new Promise((resolve) => setTimeout(resolve, ms));
34+
res.status(200).json({ success: true, ms });
3535
});
3636

37-
// Request error handler receives the thrown error
3837
app.use((err, req, res, next) => {
39-
if (err.message === 'version should be a number') {
38+
// Request error handler receives the thrown error
39+
if (err.message?.startsWith('bad request:')) {
4040
res.status(400).json({ error: err.message });
4141
return;
4242
}
4343
res.status(500).json({ error: 'unexpected error' });
4444
});
45-
46-
app.listen(3000, () => {
47-
console.info('Server running at http://localhost:3000');
48-
});
4945
```
5046
5147
## How Does This Work?
5248
53-
This is a minimalistic and unintrusive hack. Instead of patching all methods on an express `Router`, it wraps the `Layer#handle` property in one place, leaving all the rest of express as-is. Apply the patch once and then freely throw errors from all your async request handlers!
49+
This is a minimalistic and unintrusive hack. Instead of patching all methods on an express `Router`, it wraps the `Layer#handle` property in one place. When a request handler is invoked, the wrapper evaluates the handler and then checks whether the returned value is a promise. If it is, a `catch` is appended to the promise: `.catch((err) => next(err))`. Your request error handler is then able to handle the error passed by `next(err)`. If no error occurs, the `catch` never evaluates.
5450
5551
## License
5652

0 commit comments

Comments
 (0)