Skip to content

Commit b39386a

Browse files
committed
fix: upgraded deps, added docs for no_translate (per #17)
1 parent d50bb06 commit b39386a

File tree

7 files changed

+2026
-2254
lines changed

7 files changed

+2026
-2254
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ const koa404Handler = require('koa-404-handler');
6060
const app = new Koa();
6161

6262
// override koa's undocumented error handler
63-
// eslint-disable-next-line unicorn/prefer-add-event-listener
6463
app.context.onerror = errorHandler;
6564

6665
// specify that this is our api
@@ -128,7 +127,6 @@ app.use(
128127
app.use(convert(flash()));
129128

130129
// override koa's undocumented error handler
131-
// eslint-disable-next-line unicorn/prefer-add-event-listener
132130
app.context.onerror = errorHandler;
133131

134132
// use koa-404-handler
@@ -169,6 +167,19 @@ curl -H "Accept: application/json" http://localhost/some-page-does-not-exist
169167
```
170168

171169

170+
## Prevent Errors From Being Automatically Translated
171+
172+
As of v3.0.5, you can prevent an error from being automatically translated by setting the error property of `no_translate` to have a value of `true`:
173+
174+
```js
175+
function middleware(ctx) {
176+
const err = Boom.badRequest('Uh oh!');
177+
err.no_translate = true; // <----
178+
ctx.throw(err);
179+
}
180+
```
181+
182+
172183
## HTML Error Lists
173184

174185
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>`.

examples/api.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const errorHandler = require('..');
88
const app = new Koa();
99

1010
// override koa's undocumented error handler
11-
// eslint-disable-next-line unicorn/prefer-add-event-listener
1211
app.context.onerror = errorHandler;
1312

1413
// specify that this is our api

examples/web-app.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ app.use(
3838
app.use(convert(flash()));
3939

4040
// override koa's undocumented error handler
41-
// eslint-disable-next-line unicorn/prefer-add-event-listener
4241
app.context.onerror = errorHandler;
4342

4443
// use koa-404-handler

package.json

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
"Nick Baugh <[email protected]>"
1414
],
1515
"dependencies": {
16-
"@hapi/boom": "^7.4.3",
16+
"@hapi/boom": "^9.0.0",
1717
"camelcase": "^5.3.1",
18-
"capitalize": "^2.0.0",
18+
"capitalize": "^2.0.1",
1919
"co": "^4.6.0",
2020
"debug": "^4.1.1",
2121
"humanize-string": "^2.1.0",
@@ -24,34 +24,34 @@
2424
"toidentifier": "^1.0.0"
2525
},
2626
"devDependencies": {
27-
"@babel/cli": "^7.5.5",
28-
"@babel/core": "^7.5.5",
29-
"@babel/preset-env": "^7.5.5",
30-
"@commitlint/cli": "^8.1.0",
31-
"@commitlint/config-conventional": "^8.1.0",
32-
"ava": "^2.3.0",
33-
"codecov": "^3.5.0",
34-
"cross-env": "^5.2.1",
35-
"eslint": "^6.3.0",
27+
"@babel/cli": "^7.8.3",
28+
"@babel/core": "^7.8.3",
29+
"@babel/preset-env": "^7.8.3",
30+
"@commitlint/cli": "^8.3.5",
31+
"@commitlint/config-conventional": "^8.3.4",
32+
"@koa/router": "^8.0.6",
33+
"ava": "2.x",
34+
"codecov": "^3.6.2",
35+
"cross-env": "6.x",
36+
"eslint": "^6.8.0",
3637
"eslint-config-xo-lass": "^1.0.3",
37-
"eslint-plugin-node": "^10.0.0",
38+
"eslint-plugin-node": "^11.0.0",
3839
"fixpack": "^2.3.1",
39-
"husky": "^3.0.5",
40-
"koa": "^2.8.1",
40+
"husky": "3.x",
41+
"koa": "^2.11.0",
4142
"koa-404-handler": "^0.0.2",
4243
"koa-basic-auth": "^4.0.0",
4344
"koa-connect-flash": "^0.1.2",
4445
"koa-convert": "^1.2.0",
45-
"koa-generic-session": "^2.0.1",
46-
"koa-redis": "^4.0.0",
47-
"koa-router": "^7.4.0",
48-
"lint-staged": "^9.2.5",
49-
"nyc": "^14.1.1",
46+
"koa-generic-session": "^2.0.4",
47+
"koa-redis": "^4.0.1",
48+
"lint-staged": "^10.0.4",
49+
"nyc": "^15.0.0",
5050
"redis": "^2.8.0",
51-
"remark-cli": "^7.0.0",
51+
"remark-cli": "^7.0.1",
5252
"remark-preset-github": "^0.0.16",
5353
"supertest": "^4.0.2",
54-
"xo": "^0.24.0"
54+
"xo": "^0.25.3"
5555
},
5656
"engines": {
5757
"node": ">=7.6.0"

src/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ async function errorHandler(err) {
9797
err.statusCode = err.status;
9898
this.statusCode = err.statusCode;
9999
this.status = this.statusCode;
100-
this.body = new Boom(err.message, { statusCode: err.status }).output.payload;
100+
this.body = new Boom.Boom(err.message, {
101+
statusCode: err.status
102+
}).output.payload;
101103

102104
// set any additional error headers specified
103105
// (e.g. for BasicAuth we use `basic-auth` which specifies WWW-Authenticate)
@@ -128,8 +130,8 @@ async function errorHandler(err) {
128130
try {
129131
debug('rendering 404 page');
130132
await this.render('404');
131-
} catch (err2) {
132-
debug('could not find 404 page, using built-in 404 html');
133+
} catch (err_) {
134+
debug('could not find 404 page, using built-in 404 html', err_);
133135
this.body = _404;
134136
}
135137
} else {
@@ -149,8 +151,8 @@ async function errorHandler(err) {
149151
try {
150152
debug('rendering 500 page');
151153
await this.render('500');
152-
} catch (err2) {
153-
debug('could not find 500 page, using built-in 500 html');
154+
} catch (err_) {
155+
debug('could not find 500 page, using built-in 500 html', err_);
154156
this.body = _500;
155157
}
156158
} else {

test/test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const http = require('http');
22
const test = require('ava');
3-
const Router = require('koa-router');
3+
const Router = require('@koa/router');
44
const request = require('supertest');
55
const Koa = require('koa');
66
const _ = require('lodash');
77
const koa404Handler = require('koa-404-handler');
88
const auth = require('koa-basic-auth');
99

10-
const errorHandler = require('../lib');
10+
const errorHandler = require('..');
1111

1212
const statusCodes = _.keys(http.STATUS_CODES)
1313
.map(code => {
@@ -25,7 +25,6 @@ test.beforeEach(t => {
2525
t.context.app = new Koa();
2626

2727
// override koa's undocumented error handler
28-
// eslint-disable-next-line unicorn/prefer-add-event-listener
2928
t.context.app.context.onerror = errorHandler;
3029

3130
// set up some routes

0 commit comments

Comments
 (0)