Skip to content

Commit a301dd9

Browse files
committed
feat: prevent code related bugs from showing to end-users, bump deps, fixed message rendering
1 parent af62bf6 commit a301dd9

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

index.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const fs = require('fs');
2-
const path = require('path');
3-
const process = require('process');
4-
const { Buffer } = require('buffer');
1+
const fs = require('node:fs');
2+
const path = require('node:path');
3+
const process = require('node:process');
4+
const { Buffer } = require('node:buffer');
55

66
const co = require('co');
77
const Boom = require('@hapi/boom');
@@ -124,7 +124,7 @@ function errorHandler(
124124

125125
if (!type) {
126126
err.status = 406;
127-
err.message = translate(Boom.notAcceptable().output.payload);
127+
err.message = translate(Boom.notAcceptable().output.payload.message);
128128
}
129129

130130
const val = Number.parseInt(err.message, 10);
@@ -141,7 +141,7 @@ function errorHandler(
141141
) {
142142
// redis errors (e.g. ioredis' MaxRetriesPerRequestError)
143143
err.status = 408;
144-
err.message = translate(Boom.clientTimeout().output.payload);
144+
err.message = translate(Boom.clientTimeout().output.payload.message);
145145
} else if (passportLocalMongooseErrorNames.has(err.name)) {
146146
// passport-local-mongoose support
147147
if (!err.no_translate) err.message = translate(err.message);
@@ -152,13 +152,26 @@ function errorHandler(
152152
} else if (err.name === 'MongooseError') {
153153
// parse mongoose (and mongodb connection errors)
154154
err.status = 408;
155-
err.message = translate(Boom.clientTimeout().output.payload);
155+
err.message = translate(Boom.clientTimeout().output.payload.message);
156156
} else if (
157157
err.name === 'ValidationError' &&
158158
Object.getPrototypeOf(err.constructor).name === 'MongooseError'
159159
) {
160160
// parse mongoose validation errors
161161
err = parseValidationError(this, err, translate);
162+
} else if (
163+
// prevent code related bugs from
164+
// displaying to users in production environments
165+
// (and log as a fatal error)
166+
err instanceof TypeError ||
167+
err instanceof SyntaxError ||
168+
err instanceof ReferenceError ||
169+
err instanceof RangeError ||
170+
err instanceof URIError ||
171+
err instanceof EvalError
172+
) {
173+
logger.fatal(err, { isCodeBug: true });
174+
err.message = translate(Boom.internal().output.payload.message);
162175
}
163176

164177
// check if we have a boom error that specified
@@ -169,7 +182,7 @@ function errorHandler(
169182
// check if this was a DNS error and if so
170183
// then set status code for retries appropriately
171184
err.status = 408;
172-
err.message = translate(Boom.clientTimeout().output.payload);
185+
err.message = translate(Boom.clientTimeout().output.payload.message);
173186
}
174187

175188
if (!_isNumber(err.status)) err.status = 500;
@@ -210,7 +223,7 @@ function errorHandler(
210223
};
211224

212225
switch (type) {
213-
case 'html':
226+
case 'html': {
214227
this.type = 'html';
215228

216229
if (this.status === 404) {
@@ -291,14 +304,19 @@ function errorHandler(
291304
}
292305

293306
break;
294-
case 'json':
307+
}
308+
309+
case 'json': {
295310
this.type = 'json';
296311
this.body = stringify(this.body, null, 2);
297312
break;
298-
default:
313+
}
314+
315+
default: {
299316
this.type = this.api ? 'json' : 'text';
300317
this.body = stringify(this.body, null, 2);
301318
break;
319+
}
302320
}
303321

304322
this.length = Buffer.byteLength(this.body);
@@ -310,14 +328,12 @@ function makeAPIFriendly(ctx, message) {
310328
return ctx.api
311329
? convert(message, {
312330
wordwrap: false,
313-
hideLinkHrefIfSameAsText: true,
314331
selectors: [
315332
{
316333
selector: 'a',
317334
options: {
318-
baseUrl: process.env.ERROR_HANDLER_BASE_URL
319-
? process.env.ERROR_HANDLER_BASE_URL
320-
: ''
335+
hideLinkHrefIfSameAsText: true,
336+
baseUrl: process.env.ERROR_HANDLER_BASE_URL || ''
321337
}
322338
},
323339
{ selector: 'img', format: 'skip' }

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"capitalize": "^2.0.4",
3737
"co": "^4.6.0",
3838
"fast-safe-stringify": "^2.1.1",
39-
"html-to-text": "^8.2.0",
39+
"html-to-text": "^9.0.3",
4040
"humanize-string": "2",
4141
"lodash.iserror": "^3.1.1",
4242
"lodash.isfunction": "^3.0.9",
@@ -49,32 +49,32 @@
4949
"toidentifier": "^1.0.1"
5050
},
5151
"devDependencies": {
52-
"@commitlint/cli": "^17.0.3",
53-
"@commitlint/config-conventional": "^17.0.3",
54-
"@koa/router": "^10.1.1",
55-
"ava": "^4.3.0",
52+
"@commitlint/cli": "^17.4.2",
53+
"@commitlint/config-conventional": "^17.4.2",
54+
"@koa/router": "^12.0.0",
55+
"ava": "^5.1.1",
5656
"cross-env": "^7.0.3",
5757
"eslint-config-xo-lass": "^2.0.1",
5858
"fixpack": "^4.0.0",
5959
"get-port": "5",
60-
"husky": "^8.0.1",
61-
"koa": "^2.13.4",
60+
"husky": "^8.0.3",
61+
"koa": "^2.14.1",
6262
"koa-404-handler": "^0.1.0",
6363
"koa-basic-auth": "^4.0.0",
6464
"koa-connect-flash": "^0.1.2",
6565
"koa-convert": "^2.0.0",
6666
"koa-generic-session": "^2.3.0",
6767
"koa-redis": "^4.0.1",
68-
"lint-staged": "^13.0.3",
69-
"mongoose": "^6.4.2",
68+
"lint-staged": "^13.1.0",
69+
"mongoose": "^6.8.4",
7070
"nyc": "^15.1.0",
71-
"redis": "^4.1.1",
71+
"redis": "^4.5.1",
7272
"redis-errors": "^1.2.0",
7373
"remark-cli": "^11.0.0",
7474
"remark-preset-github": "^4.0.4",
75-
"rimraf": "^3.0.2",
76-
"supertest": "^6.2.3",
77-
"xo": "^0.50.0"
75+
"rimraf": "^4.1.1",
76+
"supertest": "^6.3.3",
77+
"xo": "^0.53.1"
7878
},
7979
"engines": {
8080
"node": ">= 14"

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const http = require('http');
1+
const http = require('node:http');
22

33
const Koa = require('koa');
44
const Router = require('@koa/router');

0 commit comments

Comments
 (0)