You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+12-16
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,6 @@
1
1
# @csnw/express-async-errors
2
2
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.
6
4
7
5
This is a fork of a fork; thank you to the original authors:
8
6
@@ -15,7 +13,8 @@ This is a fork of a fork; thank you to the original authors:
15
13
npm install @csnw/express-async-errors --save
16
14
```
17
15
18
-
Run the patch before building `express()`. For example:
16
+
Run the patch before building `express()`.
17
+
Example:
19
18
20
19
```js
21
20
importexpressfrom'express';
@@ -25,32 +24,29 @@ import expressAsyncErrors from '@csnw/express-async-errors';
25
24
expressAsyncErrors();
26
25
constapp=express();
27
26
28
-
app.get('/version', async (req, res) => {
29
-
constversion=parseInt(req.query.v);
30
-
if (isNaN(version)) {
27
+
app.get('/delay', async (req, res) => {
28
+
constms=parseInt(req.query.ms);
29
+
if (isNaN(ms)) {
31
30
// Throw error from async request handler
32
-
thrownewError('version should be a number');
31
+
thrownewError('bad request: ms should be a number');
console.info('Server running at http://localhost:3000');
48
-
});
49
45
```
50
46
51
47
## How Does This Work?
52
48
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.
0 commit comments