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
66const  co  =  require ( 'co' ) ; 
77const  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'  } 
0 commit comments