@@ -17,6 +17,18 @@ const _500 = fs.readFileSync(path.join(__dirname, '..', '500.html'), opts);
17
17
18
18
const debug = new Debug ( 'koa-better-error-handler' ) ;
19
19
20
+ const passportLocalMongooseErrorNames = [
21
+ 'AuthenticationError' ,
22
+ 'MissingPasswordError' ,
23
+ 'AttemptTooSoonError' ,
24
+ 'TooManyAttemptsError' ,
25
+ 'NoSaltValueStoredError' ,
26
+ 'IncorrectPasswordError' ,
27
+ 'IncorrectUsernameError' ,
28
+ 'MissingUsernameError' ,
29
+ 'UserExistsError'
30
+ ] ;
31
+
20
32
// initialize try/catch error handling right away
21
33
// adapted from: https://github.com/koajs/onerror/blob/master/index.js
22
34
// https://github.com/koajs/examples/issues/20#issuecomment-31568401
@@ -202,6 +214,21 @@ async function errorHandler(err) {
202
214
}
203
215
204
216
function parseValidationError ( ctx , err ) {
217
+ // translate messages
218
+ const translate = message =>
219
+ _ . isFunction ( ctx . request . t ) ? ctx . request . t ( message ) : message ;
220
+
221
+ // passport-local-mongoose support
222
+ if ( passportLocalMongooseErrorNames . includes ( err . name ) ) {
223
+ err . message = translate ( err . message ) ;
224
+ // this ensures the error shows up client-side
225
+ err . status = 400 ;
226
+ // 429 = too many requests
227
+ if ( [ 'AttemptTooSoonError' , 'TooManyAttemptsError' ] . includes ( err . name ) )
228
+ err . status = 429 ;
229
+ return err ;
230
+ }
231
+
205
232
// inspired by https://github.com/syntagma/mongoose-error-helper
206
233
if ( err . name !== 'ValidationError' ) return err ;
207
234
@@ -221,10 +248,6 @@ function parseValidationError(ctx, err) {
221
248
return error ;
222
249
} ) ;
223
250
224
- // translate messages
225
- const translate = message =>
226
- _ . isFunction ( ctx . request . t ) ? ctx . request . t ( message ) : message ;
227
-
228
251
// loop over the errors object of the Validation Error
229
252
// with support for HTML error lists
230
253
if ( _ . values ( err . errors ) . length === 1 ) {
@@ -238,7 +261,6 @@ function parseValidationError(ctx, err) {
238
261
239
262
// this ensures the error shows up client-side
240
263
err . status = 400 ;
241
- err . statusCode = 400 ;
242
264
243
265
return err ;
244
266
}
0 commit comments