Skip to content

Commit cec8765

Browse files
committed
feat: added more DNS error handling
1 parent a514394 commit cec8765

File tree

6 files changed

+1698
-1469
lines changed

6 files changed

+1698
-1469
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ function middleware(ctx) {
199199

200200
If you specify `app.context.api = true` or set `ctx.api = true`, and if a Mongoose validation error message occurs that has more than one message (e.g. multiple fields were invalid) – then `err.message` will be joined by a comma instead of by `<li>`.
201201

202-
Therefore if you _DO_ want your API error messages to return HTML formatted error lists for Mongoose validation, then set `app.context.api = false`, `ctx.api = false`, or simply make sure to not set them before using this error handler.
202+
Therefore if you *DO* want your API error messages to return HTML formatted error lists for Mongoose validation, then set `app.context.api = false`, `ctx.api = false`, or simply make sure to not set them before using this error handler.
203203

204204
```js
205205
try {
@@ -244,7 +244,7 @@ You can also specify a base URI in the environment variable for rendering as `pr
244244
[MIT](LICENSE) © [Nick Baugh](http://niftylettuce.com/)
245245

246246

247-
##
247+
##
248248

249249
[boom]: https://github.com/hapijs/boom
250250

examples/api.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ app.use(koa404Handler);
2020
const router = new Router();
2121

2222
// throw an error anywhere you want!
23-
router.get('/404', ctx => ctx.throw(404));
24-
router.get('/500', ctx => ctx.throw(500));
23+
router.get('/404', (ctx) => ctx.throw(404));
24+
router.get('/500', (ctx) => ctx.throw(500));
2525

2626
// initialize routes on the app
2727
app.use(router.routes());

examples/web-app.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ app.keys = ['foo', 'bar'];
1818
// initialize redis store
1919
const redisClient = redis.createClient();
2020
redisClient.on('connect', () => app.emit('log', 'info', 'redis connected'));
21-
redisClient.on('error', err => app.emit('error', err));
21+
redisClient.on('error', (err) => app.emit('error', err));
2222

2323
// define our storage
2424
const redisStore = new RedisStore({
@@ -47,8 +47,8 @@ app.use(koa404Handler);
4747
const router = new Router();
4848

4949
// throw an error anywhere you want!
50-
router.get('/404', ctx => ctx.throw(404));
51-
router.get('/500', ctx => ctx.throw(500));
50+
router.get('/404', (ctx) => ctx.throw(404));
51+
router.get('/500', (ctx) => ctx.throw(500));
5252

5353
// initialize routes on the app
5454
app.use(router.routes());

index.js

+36-5
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,41 @@ const statuses = require('statuses');
1212
const toIdentifier = require('toidentifier');
1313

1414
// lodash
15+
const _isError = require('lodash.iserror');
16+
const _isFunction = require('lodash.isfunction');
1517
const _isNumber = require('lodash.isnumber');
16-
const _isString = require('lodash.isstring');
1718
const _isObject = require('lodash.isobject');
18-
const _isFunction = require('lodash.isfunction');
19-
const _isError = require('lodash.iserror');
19+
const _isString = require('lodash.isstring');
2020
const _map = require('lodash.map');
2121
const _values = require('lodash.values');
2222

23+
// NOTE: if you change this, be sure to sync in `forward-email`
24+
// <https://github.com/nodejs/node/blob/08dd4b1723b20d56fbedf37d52e736fe09715f80/lib/dns.js#L296-L320>
25+
const CODES_TO_RESPONSE_CODES = {
26+
EADDRGETNETWORKPARAMS: 421,
27+
EADDRINUSE: 421,
28+
EAI_AGAIN: 421,
29+
EBADFLAGS: 421,
30+
EBADHINTS: 421,
31+
ECANCELLED: 421,
32+
ECONNREFUSED: 421,
33+
ECONNRESET: 442,
34+
EDESTRUCTION: 421,
35+
EFORMERR: 421,
36+
ELOADIPHLPAPI: 421,
37+
ENETUNREACH: 421,
38+
ENODATA: 421,
39+
ENOMEM: 421,
40+
ENOTFOUND: 421,
41+
ENOTINITIALIZED: 421,
42+
EPIPE: 421,
43+
EREFUSED: 421,
44+
ESERVFAIL: 421,
45+
ETIMEOUT: 420
46+
};
47+
48+
const RETRY_CODES = Object.keys(CODES_TO_RESPONSE_CODES);
49+
2350
const opts = {
2451
encoding: 'utf8'
2552
};
@@ -81,8 +108,13 @@ function errorHandler(
81108

82109
// check if we have a boom error that specified
83110
// a status code already for us (and then use it)
84-
if (_isObject(err.output) && _isNumber(err.output.statusCode))
111+
if (_isObject(err.output) && _isNumber(err.output.statusCode)) {
85112
err.status = err.output.statusCode;
113+
} else if (_isString(err.code) && RETRY_CODES.includes(err.code)) {
114+
// check if this was a DNS error and if so
115+
// then set status code for retries appropriately
116+
err.status = CODES_TO_RESPONSE_CODES[err.code];
117+
}
86118

87119
if (!_isNumber(err.status)) err.status = 500;
88120

@@ -186,7 +218,6 @@ function errorHandler(
186218
this.cookies.set(cookiesKey, this.sessionId, this.session.cookie);
187219
} catch (err) {
188220
logger.error(err);
189-
// eslint-disable-next-line max-depth
190221
if (err.code === 'ERR_HTTP_HEADERS_SENT') return;
191222
}
192223
}

package.json

+21-21
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@
3636
}
3737
],
3838
"dependencies": {
39-
"@hapi/boom": "^9.1.0",
40-
"camelcase": "^6.0.0",
39+
"@hapi/boom": "^9.1.1",
40+
"ava": "3.11.1",
41+
"camelcase": "^6.2.0",
4142
"capitalize": "^2.0.3",
4243
"co": "^4.6.0",
4344
"fast-safe-stringify": "^2.0.7",
44-
"html-to-text": "^5.1.1",
45+
"html-to-text": "5",
4546
"humanize-string": "^2.1.0",
4647
"lodash.iserror": "^3.1.1",
4748
"lodash.isfunction": "^3.0.9",
@@ -50,35 +51,34 @@
5051
"lodash.isstring": "^4.0.1",
5152
"lodash.map": "^4.6.0",
5253
"lodash.values": "^4.3.0",
53-
"statuses": "^2.0.0",
54+
"statuses": "^2.0.1",
5455
"toidentifier": "^1.0.0"
5556
},
5657
"devDependencies": {
57-
"@commitlint/cli": "^9.1.2",
58-
"@commitlint/config-conventional": "^9.1.2",
59-
"@koa/router": "^9.4.0",
60-
"ava": "3.11.1",
61-
"codecov": "^3.7.2",
62-
"cross-env": "^7.0.2",
63-
"eslint-config-xo-lass": "^1.0.3",
64-
"fixpack": "^3.0.6",
65-
"husky": "^4.2.5",
66-
"koa": "^2.13.0",
58+
"@commitlint/cli": "^11.0.0",
59+
"@commitlint/config-conventional": "^11.0.0",
60+
"@koa/router": "^10.0.0",
61+
"codecov": "^3.8.1",
62+
"cross-env": "^7.0.3",
63+
"eslint-config-xo-lass": "^1.0.5",
64+
"fixpack": "^4.0.0",
65+
"husky": "^5.0.9",
66+
"koa": "^2.13.1",
6767
"koa-404-handler": "^0.0.2",
6868
"koa-basic-auth": "^4.0.0",
6969
"koa-connect-flash": "^0.1.2",
70-
"koa-convert": "^1.2.0",
71-
"koa-generic-session": "^2.0.4",
70+
"koa-convert": "^2.0.0",
71+
"koa-generic-session": "^2.1.1",
7272
"koa-redis": "^4.0.1",
73-
"lint-staged": "^10.2.11",
73+
"lint-staged": "^10.5.4",
7474
"lodash": "^4.17.20",
7575
"nyc": "^15.1.0",
7676
"redis": "^3.0.2",
77-
"remark-cli": "^8.0.1",
78-
"remark-preset-github": "^3.0.0",
77+
"remark-cli": "^9.0.0",
78+
"remark-preset-github": "^4.0.1",
7979
"rimraf": "^3.0.2",
80-
"supertest": "^4.0.2",
81-
"xo": "^0.33.0"
80+
"supertest": "^6.1.3",
81+
"xo": "^0.37.1"
8282
},
8383
"engines": {
8484
"node": ">= 10.14"

0 commit comments

Comments
 (0)