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' ) ;
5
5
6
6
const co = require ( 'co' ) ;
7
7
const Boom = require ( '@hapi/boom' ) ;
@@ -124,7 +124,7 @@ function errorHandler(
124
124
125
125
if ( ! type ) {
126
126
err . status = 406 ;
127
- err . message = translate ( Boom . notAcceptable ( ) . output . payload ) ;
127
+ err . message = translate ( Boom . notAcceptable ( ) . output . payload . message ) ;
128
128
}
129
129
130
130
const val = Number . parseInt ( err . message , 10 ) ;
@@ -141,7 +141,7 @@ function errorHandler(
141
141
) {
142
142
// redis errors (e.g. ioredis' MaxRetriesPerRequestError)
143
143
err . status = 408 ;
144
- err . message = translate ( Boom . clientTimeout ( ) . output . payload ) ;
144
+ err . message = translate ( Boom . clientTimeout ( ) . output . payload . message ) ;
145
145
} else if ( passportLocalMongooseErrorNames . has ( err . name ) ) {
146
146
// passport-local-mongoose support
147
147
if ( ! err . no_translate ) err . message = translate ( err . message ) ;
@@ -152,13 +152,26 @@ function errorHandler(
152
152
} else if ( err . name === 'MongooseError' ) {
153
153
// parse mongoose (and mongodb connection errors)
154
154
err . status = 408 ;
155
- err . message = translate ( Boom . clientTimeout ( ) . output . payload ) ;
155
+ err . message = translate ( Boom . clientTimeout ( ) . output . payload . message ) ;
156
156
} else if (
157
157
err . name === 'ValidationError' &&
158
158
Object . getPrototypeOf ( err . constructor ) . name === 'MongooseError'
159
159
) {
160
160
// parse mongoose validation errors
161
161
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 ) ;
162
175
}
163
176
164
177
// check if we have a boom error that specified
@@ -169,7 +182,7 @@ function errorHandler(
169
182
// check if this was a DNS error and if so
170
183
// then set status code for retries appropriately
171
184
err . status = 408 ;
172
- err . message = translate ( Boom . clientTimeout ( ) . output . payload ) ;
185
+ err . message = translate ( Boom . clientTimeout ( ) . output . payload . message ) ;
173
186
}
174
187
175
188
if ( ! _isNumber ( err . status ) ) err . status = 500 ;
@@ -210,7 +223,7 @@ function errorHandler(
210
223
} ;
211
224
212
225
switch ( type ) {
213
- case 'html' :
226
+ case 'html' : {
214
227
this . type = 'html' ;
215
228
216
229
if ( this . status === 404 ) {
@@ -291,14 +304,19 @@ function errorHandler(
291
304
}
292
305
293
306
break ;
294
- case 'json' :
307
+ }
308
+
309
+ case 'json' : {
295
310
this . type = 'json' ;
296
311
this . body = stringify ( this . body , null , 2 ) ;
297
312
break ;
298
- default :
313
+ }
314
+
315
+ default : {
299
316
this . type = this . api ? 'json' : 'text' ;
300
317
this . body = stringify ( this . body , null , 2 ) ;
301
318
break ;
319
+ }
302
320
}
303
321
304
322
this . length = Buffer . byteLength ( this . body ) ;
@@ -310,14 +328,12 @@ function makeAPIFriendly(ctx, message) {
310
328
return ctx . api
311
329
? convert ( message , {
312
330
wordwrap : false ,
313
- hideLinkHrefIfSameAsText : true ,
314
331
selectors : [
315
332
{
316
333
selector : 'a' ,
317
334
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 || ''
321
337
}
322
338
} ,
323
339
{ selector : 'img' , format : 'skip' }
0 commit comments