Skip to content

Commit fa8cb81

Browse files
committed
feat: bump deps, translate error messages, fixed tests
1 parent 590599a commit fa8cb81

12 files changed

+3254
-3927
lines changed

.commitlintrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['@commitlint/config-conventional']
3+
};

.husky/commit-msg

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
yarn commitlint --edit $1

.husky/pre-commit

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
yarn lint-staged && yarn test

.lintstagedrc.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
"*.md": filenames => filenames.map(filename => `remark ${filename} -qfo`),
3+
'package.json': 'fixpack',
4+
'*.js': 'xo --fix'
5+
};

.prettierrc.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
singleQuote: true,
3+
bracketSpacing: true,
4+
trailingComma: 'none'
5+
};

.remarkrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
plugins: ['preset-github']
3+
};

.travis.yml

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
language: node_js
22
node_js:
3-
- 10
4-
- 12
5-
script:
6-
- npm run test-ci
7-
after_script:
8-
- npm run coverage
3+
- '14'
4+
- '16'
5+
- '18'
6+
after_success:
7+
npm run coverage

.xo-config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
prettier: true,
3+
space: true,
4+
extends: ['xo-lass']
5+
};

index.js

+45-26
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
const fs = require('fs');
22
const path = require('path');
3+
const process = require('process');
4+
const { Buffer } = require('buffer');
35

4-
const fastSafeStringify = require('fast-safe-stringify');
56
const Boom = require('@hapi/boom');
67
const camelCase = require('camelcase');
78
const capitalize = require('capitalize');
89
const co = require('co');
9-
const htmlToText = require('html-to-text');
10+
const fastSafeStringify = require('fast-safe-stringify');
1011
const humanize = require('humanize-string');
1112
const statuses = require('statuses');
1213
const toIdentifier = require('toidentifier');
14+
const { convert } = require('html-to-text');
1315

1416
// lodash
1517
const _isError = require('lodash.iserror');
@@ -56,7 +58,7 @@ const opts = {
5658
const _404 = fs.readFileSync(path.join(__dirname, '404.html'), opts);
5759
const _500 = fs.readFileSync(path.join(__dirname, '500.html'), opts);
5860

59-
const passportLocalMongooseErrorNames = [
61+
const passportLocalMongooseErrorNames = new Set([
6062
'AuthenticationError',
6163
'MissingPasswordError',
6264
'AttemptTooSoonError',
@@ -66,7 +68,7 @@ const passportLocalMongooseErrorNames = [
6668
'IncorrectUsernameError',
6769
'MissingUsernameError',
6870
'UserExistsError'
69-
];
71+
]);
7072

7173
// initialize try/catch error handling right away
7274
// adapted from: https://github.com/koajs/onerror/blob/master/index.js
@@ -86,6 +88,20 @@ function errorHandler(
8688
return async function (err) {
8789
if (!err) return;
8890

91+
// nothing we can do here other
92+
// than delegate to the app-level
93+
// handler and log.
94+
if (this.headerSent || !this.writable) {
95+
err.headerSent = true;
96+
this.app.emit('error', err, this);
97+
this.app.emit(
98+
'error',
99+
new Error('Headers were already sent, returning early'),
100+
this
101+
);
102+
return;
103+
}
104+
89105
const logger = useCtxLogger && this.logger ? this.logger : _logger;
90106

91107
if (!_isError(err)) err = new Error(err);
@@ -102,7 +118,7 @@ function errorHandler(
102118
err = parseValidationError(this, err);
103119

104120
// check if we threw just a status code in order to keep it simple
105-
const val = parseInt(err.message, 10);
121+
const val = Number.parseInt(err.message, 10);
106122
if (_isNumber(val) && val >= 400)
107123
err = Boom[camelCase(toIdentifier(statuses.message[val]))]();
108124

@@ -127,15 +143,6 @@ function errorHandler(
127143
// check if we're about to go into a possible endless redirect loop
128144
const noReferrer = this.get('Referrer') === '';
129145

130-
// nothing we can do here other
131-
// than delegate to the app-level
132-
// handler and log.
133-
if (this.headerSent || !this.writable) {
134-
logger.error(new Error('Headers were already sent, returning early'));
135-
err.headerSent = true;
136-
return;
137-
}
138-
139146
// populate the status and body with `boom` error message payload
140147
// (e.g. you can do `ctx.throw(404)` and it will output a beautiful err obj)
141148
err.status = err.status || 500;
@@ -159,8 +166,13 @@ function errorHandler(
159166
// fix page title and description
160167
if (!this.api) {
161168
this.state.meta = this.state.meta || {};
162-
this.state.meta.title = this.body.error;
163-
this.state.meta.description = err.message;
169+
if (!err.no_translate && _isFunction(this.request.t)) {
170+
this.state.meta.title = this.request.t(this.body.error);
171+
this.state.meta.description = this.request.t(err.message);
172+
} else {
173+
this.state.meta.title = this.body.error;
174+
this.state.meta.description = err.message;
175+
}
164176
}
165177

166178
switch (type) {
@@ -227,7 +239,7 @@ function errorHandler(
227239
// <https://github.com/koajs/generic-session/pull/95#issuecomment-246308544>
228240
//
229241
// these comments may no longer be valid and need reconsidered:
230-
//
242+
//
231243
// if we're using `koa-session-store` we need to add
232244
// `this._session = new Session()`, and then run this:
233245
await co.wrap(this._session._store.save).call(
@@ -261,16 +273,23 @@ function errorHandler(
261273
}
262274

263275
function makeAPIFriendly(ctx, message) {
264-
return !ctx.api
265-
? message
266-
: htmlToText.fromString(message, {
276+
return ctx.api
277+
? convert(message, {
267278
wordwrap: false,
268-
linkHrefBaseUrl: process.env.ERROR_HANDLER_BASE_URL
269-
? process.env.ERROR_HANDLER_BASE_URL
270-
: '',
271279
hideLinkHrefIfSameAsText: true,
272-
ignoreImage: true
273-
});
280+
selectors: [
281+
{
282+
selector: 'a',
283+
options: {
284+
baseUrl: process.env.ERROR_HANDLER_BASE_URL
285+
? process.env.ERROR_HANDLER_BASE_URL
286+
: ''
287+
}
288+
},
289+
{ selector: 'img', format: 'skip' }
290+
]
291+
})
292+
: message;
274293
}
275294

276295
function parseValidationError(ctx, err) {
@@ -281,7 +300,7 @@ function parseValidationError(ctx, err) {
281300
: message;
282301

283302
// passport-local-mongoose support
284-
if (passportLocalMongooseErrorNames.includes(err.name)) {
303+
if (passportLocalMongooseErrorNames.has(err.name)) {
285304
err.message = translate(err.message);
286305
// this ensures the error shows up client-side
287306
err.status = 400;

package.json

+34-88
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88
"url": "http://niftylettuce.com/"
99
},
1010
"bugs": "https://github.com/ladjs/koa-better-error-handler/issues",
11-
"commitlint": {
12-
"extends": [
13-
"@commitlint/config-conventional"
14-
]
15-
},
1611
"contributors": [
1712
{
1813
"name": "Nick Baugh",
@@ -36,13 +31,13 @@
3631
}
3732
],
3833
"dependencies": {
39-
"@hapi/boom": "^9.1.1",
40-
"camelcase": "^6.2.0",
41-
"capitalize": "^2.0.3",
34+
"@hapi/boom": "^10.0.0",
35+
"camelcase": "^6.3.0",
36+
"capitalize": "^2.0.4",
4237
"co": "^4.6.0",
43-
"fast-safe-stringify": "^2.0.7",
44-
"html-to-text": "5",
45-
"humanize-string": "^2.1.0",
38+
"fast-safe-stringify": "^2.1.1",
39+
"html-to-text": "^8.2.0",
40+
"humanize-string": "2",
4641
"lodash.iserror": "^3.1.1",
4742
"lodash.isfunction": "^3.0.9",
4843
"lodash.isnumber": "^3.0.3",
@@ -51,45 +46,42 @@
5146
"lodash.map": "^4.6.0",
5247
"lodash.values": "^4.3.0",
5348
"statuses": "^2.0.1",
54-
"toidentifier": "^1.0.0"
49+
"toidentifier": "^1.0.1"
5550
},
5651
"devDependencies": {
57-
"@commitlint/cli": "^11.0.0",
58-
"@commitlint/config-conventional": "^11.0.0",
59-
"@koa/router": "^10.0.0",
60-
"ava": "3.11.1",
61-
"codecov": "^3.8.1",
52+
"@commitlint/cli": "^17.0.2",
53+
"@commitlint/config-conventional": "^17.0.2",
54+
"@koa/router": "^10.1.1",
55+
"ava": "^4.3.0",
56+
"codecov": "^3.8.2",
6257
"cross-env": "^7.0.3",
63-
"eslint-config-xo-lass": "^1.0.5",
58+
"eslint-config-xo-lass": "^1.0.6",
6459
"fixpack": "^4.0.0",
65-
"husky": "^5.0.9",
66-
"koa": "^2.13.1",
67-
"koa-404-handler": "^0.0.2",
60+
"get-port": "5",
61+
"husky": "^8.0.1",
62+
"koa": "^2.13.4",
63+
"koa-404-handler": "^0.1.0",
6864
"koa-basic-auth": "^4.0.0",
6965
"koa-connect-flash": "^0.1.2",
7066
"koa-convert": "^2.0.0",
71-
"koa-generic-session": "^2.1.1",
67+
"koa-generic-session": "^2.3.0",
7268
"koa-redis": "^4.0.1",
73-
"lint-staged": "^10.5.4",
74-
"lodash": "^4.17.20",
69+
"lint-staged": "^13.0.0",
7570
"nyc": "^15.1.0",
76-
"redis": "^3.0.2",
77-
"remark-cli": "^9.0.0",
78-
"remark-preset-github": "^4.0.1",
71+
"redis": "^4.1.0",
72+
"remark-cli": "^10.0.1",
73+
"remark-preset-github": "^4.0.2",
7974
"rimraf": "^3.0.2",
80-
"supertest": "^6.1.3",
81-
"xo": "^0.37.1"
75+
"supertest": "^6.2.3",
76+
"xo": "^0.49.0"
8277
},
8378
"engines": {
84-
"node": ">= 10.14"
79+
"node": ">= 14"
8580
},
81+
"files": [
82+
"index.js"
83+
],
8684
"homepage": "https://github.com/ladjs/koa-better-error-handler",
87-
"husky": {
88-
"hooks": {
89-
"pre-commit": "lint-staged && npm test",
90-
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
91-
}
92-
},
9385
"keywords": [
9486
"404",
9587
"500",
@@ -114,60 +106,14 @@
114106
"stripe"
115107
],
116108
"license": "MIT",
117-
"lint-staged": {
118-
"*.js": [
119-
"xo --fix",
120-
"git add"
121-
],
122-
"*.md": [
123-
"remark . -qfo",
124-
"git add"
125-
],
126-
"package.json": [
127-
"fixpack",
128-
"git add"
129-
]
130-
},
131109
"main": "index.js",
132-
"prettier": {
133-
"singleQuote": true,
134-
"bracketSpacing": true,
135-
"trailingComma": "none"
136-
},
137-
"remarkConfig": {
138-
"plugins": [
139-
"preset-github"
140-
]
141-
},
142110
"repository": "ladjs/koa-better-error-handler",
143111
"scripts": {
144-
"coverage": "nyc report --reporter=text-lcov > coverage.lcov",
145-
"lint": "npm run lint:js",
146-
"lint:js": "xo",
147-
"lint:md": "remark . -qfo",
148-
"postcoverage": "codecov",
149-
"precommit": "lint-staged && npm tes-ci",
150-
"pretest-ci": "npm run lint",
151-
"pretest-cov": "rimraf .nyc_output",
152-
"test": "ava",
153-
"test-ci": "npm run test-cov",
154-
"test-cov": "cross-env NODE_ENV=test nyc npm run test"
155-
},
156-
"xo": {
157-
"prettier": true,
158-
"space": true,
159-
"extends": [
160-
"xo-lass"
161-
],
162-
"ignore": [
163-
"examples",
164-
"test"
165-
],
166-
"rules": {
167-
"no-negated-condition": "off",
168-
"unicorn/prevent-abbreviations": "off",
169-
"unicorn/prefer-number-properties": "off",
170-
"unicorn/prefer-set-has": "off"
171-
}
112+
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
113+
"lint": "xo && remark . -qfo",
114+
"precommit": "lint-staged && npm test",
115+
"prepare": "husky install",
116+
"test": "npm run lint && npm run test-coverage",
117+
"test-coverage": "cross-env NODE_ENV=test nyc ava"
172118
}
173119
}

0 commit comments

Comments
 (0)