Skip to content

Commit 1c4c72f

Browse files
committed
Update package ownership
- Namespace package under @CSNW - Update links and author - Initial readme updates - Specify which files should be published (technically, could be empty array and all of license, readme, and "main" file would be included automatically, but opting to be explicit to reduce confusion)
1 parent 694b215 commit 1c4c72f

File tree

5 files changed

+45
-70
lines changed

5 files changed

+45
-70
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
node_modules
22
.nyc_output/
3-
.tern-port
43
coverage/

.travis.yml

-11
This file was deleted.

README.md

+30-43
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,56 @@
1-
# ExpressJS Async Errors
1+
# @csnw/express-async-errors
22

3-
[![Build Status](https://travis-ci.org/davidbanham/express-async-errors.svg?branch=master)](https://travis-ci.org/davidbanham/express-async-errors)
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.
44

5-
A dead simple ES6 async/await support hack for [ExpressJS](http://expressjs.com)
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.
66

7-
Shamelessly copied from [express-yields](https://github.com/MadRabbit/express-yields)
7+
This is a fork of a fork; thank you to the original authors:
88

9-
This has been lightly reworked to handle async rather than generators.
9+
- [MadRabbit/express-yields](https://github.com/MadRabbit/express-yields)
10+
- [davidbanham/express-async-errors](https://github.com/davidbanham/express-async-errors)
1011

1112
## Usage
1213

14+
```sh
15+
npm install @csnw/express-async-errors --save
1316
```
14-
npm install express-async-errors --save
15-
```
16-
17-
Then require this script somewhere __before__ you start using it:
1817

19-
Async functions already work fine in Express.
18+
Run the patch before building `express()`. For example:
2019

2120
```js
22-
const express = require('express');
23-
require('express-async-errors');
24-
const User = require('./models/user');
25-
const app = express();
26-
27-
app.get('/users', async (req, res) => {
28-
const users = await User.findAll();
29-
res.send(users);
30-
});
31-
```
32-
33-
This library is about what happens when you hit an error.
21+
import express from 'express';
22+
import expressAsyncErrors from '@csnw/express-async-errors';
3423

35-
## A Notice About Calling `next`
36-
37-
As we all know express sends a function called `next` into the middleware, which
38-
then needs to be called with or without error to make it move the request handling
39-
to the next middleware. It still works, but in case of an async function, you
40-
don't need to do that. If you want to pass an error, just throw a normal exception:
41-
42-
```js
43-
app.use(async (req, res) => {
44-
const user = await User.findByToken(req.get('authorization'));
24+
// Apply patch and then build express
25+
expressAsyncErrors();
26+
const app = express();
4527

46-
if (!user) throw Error("access denied");
28+
app.get('/version', async (req, res) => {
29+
const version = parseInt(req.query.v);
30+
if (isNaN(version)) {
31+
// Throw error from async request handler
32+
throw new Error('version should be a number');
33+
}
34+
res.status(200).json({ version });
4735
});
4836

37+
// Request error handler receives the thrown error
4938
app.use((err, req, res, next) => {
50-
if (err.message === 'access denied') {
51-
res.status(403);
52-
res.json({ error: err.message });
39+
if (err.message === 'version should be a number') {
40+
res.status(400).json({ error: err.message });
41+
return;
5342
}
43+
res.status(500).json({ error: 'unexpected error' });
44+
});
5445

55-
next(err);
46+
app.listen(3000, () => {
47+
console.info('Server running at http://localhost:3000');
5648
});
5749
```
5850

5951
## How Does This Work?
6052

61-
This is a very minimalistic and unintrusive hack. Instead of patching all methods
62-
on an express `Router`, it wraps the `Layer#handle` property in one place, leaving
63-
all the rest of the express guts intact.
64-
65-
The idea is that you require the patch once and then use the `'express'` lib the
66-
usual way in the rest of your application.
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!
6754

6855
## License
6956

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
{
2-
"name": "express-async-errors",
3-
"version": "3.1.1",
2+
"name": "@csnw/express-async-errors",
3+
"version": "4.0.0",
44
"description": "Async/await error handling support for expressjs",
55
"main": "index.js",
6+
"files": [
7+
"index.js"
8+
],
69
"scripts": {
710
"coverage": "nyc mocha test.js",
811
"report": "nyc report --reporter=html",
912
"test": "mocha test.js",
1013
"lint": "eslint .",
1114
"precommit": "npm run lint"
1215
},
13-
"repository": {
14-
"type": "git",
15-
"url": "git+https://github.com/davidbanham/express-async-errors.git"
16-
},
16+
"repository": "CSNW/express-async-errors",
1717
"keywords": [
18+
"express",
1819
"expressjs",
19-
"async/await",
2020
"async",
2121
"await",
22-
"es6"
22+
"error"
2323
],
24-
"author": "[email protected]",
24+
"author": "[email protected]",
2525
"license": "ISC",
2626
"bugs": {
27-
"url": "https://github.com/davidbanham/express-async-errors/issues"
27+
"url": "https://github.com/CSNW/express-async-errors/issues"
2828
},
29-
"homepage": "https://github.com/davidbanham/express-async-errors#readme",
29+
"homepage": "https://github.com/CSNW/express-async-errors#readme",
3030
"peerDependencies": {
3131
"express": "^4"
3232
},

0 commit comments

Comments
 (0)