Skip to content

Commit 58a073a

Browse files
committed
feat: improved error parsing, reduced dep size
1 parent ac5699d commit 58a073a

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

index.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ const fastSafeStringify = require('fast-safe-stringify');
1010
const humanize = require('humanize-string');
1111
const statuses = require('statuses');
1212
const toIdentifier = require('toidentifier');
13-
const { RedisError } = require('redis-errors');
1413
const { convert } = require('html-to-text');
15-
const MongooseError = require('mongoose/lib/error');
1614

1715
// lodash
1816
const _isError = require('lodash.iserror');
@@ -132,7 +130,7 @@ function errorHandler(
132130
// check if we threw just a status code in order to keep it simple
133131
err = Boom[camelCase(toIdentifier(statuses.message[val]))]();
134132
err.message = translate(err.message);
135-
} else if (err instanceof RedisError) {
133+
} else if (err.name === 'RedisError') {
136134
// redis errors (e.g. ioredis' MaxRetriesPerRequestError)
137135
err.status = 408;
138136
err.message = translate(Boom.clientTimeout().output.payload);
@@ -143,18 +141,16 @@ function errorHandler(
143141
err.status = 400;
144142
// 429 = too many requests
145143
if (passportLocalMongooseTooManyRequests.has(err.name)) err.status = 429;
146-
} else if (err.name === 'ValidationError') {
147-
// parse mongoose validation errors
148-
err = parseValidationError(this, err, translate);
149-
} else if (
150-
err instanceof MongooseError &&
151-
!(err instanceof MongooseError.ValidationError) &&
152-
!(err instanceof MongooseError.ValidatorError) &&
153-
!(err instanceof MongooseError.VersionError)
154-
) {
144+
} else if (err.name === 'MongooseError') {
155145
// parse mongoose (and mongodb connection errors)
156146
err.status = 408;
157147
err.message = translate(Boom.clientTimeout().output.payload);
148+
} else if (
149+
err.name === 'ValidationError' &&
150+
Object.getPrototypeOf(err.constructor).name === 'MongooseError'
151+
) {
152+
// parse mongoose validation errors
153+
err = parseValidationError(this, err, translate);
158154
}
159155

160156
// check if we have a boom error that specified

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
"lodash.isstring": "^4.0.1",
4545
"lodash.map": "^4.6.0",
4646
"lodash.values": "^4.3.0",
47-
"mongoose": "^6.4.2",
48-
"redis-errors": "^1.2.0",
4947
"statuses": "^2.0.1",
5048
"toidentifier": "^1.0.1"
5149
},
@@ -67,8 +65,10 @@
6765
"koa-generic-session": "^2.3.0",
6866
"koa-redis": "^4.0.1",
6967
"lint-staged": "^13.0.3",
68+
"mongoose": "^6.4.2",
7069
"nyc": "^15.1.0",
7170
"redis": "^4.1.1",
71+
"redis-errors": "^1.2.0",
7272
"remark-cli": "^11.0.0",
7373
"remark-preset-github": "^4.0.4",
7474
"rimraf": "^3.0.2",

0 commit comments

Comments
 (0)