@@ -45,18 +45,27 @@ server.register(compression, {
4545
4646// Global error handler with correlation ID
4747server . setErrorHandler ( ( error , request , reply ) => {
48+ const alreadySent = reply . sent || reply . raw . headersSent || reply . raw . writableEnded ;
49+ const errorCode = error . statusCode || 500 ;
4850 const errorLog = {
4951 reqId : request . id ,
5052 correlationId : request . correlationId ,
5153 url : request . url ,
5254 method : request . method ,
53- statusCode : error . statusCode || 500 ,
55+ statusCode : errorCode ,
5456 error : error . message ,
55- stack : process . env . NODE_ENV === 'development' ? error . stack : undefined
57+ stack : error . stack
5658 } ;
5759 request . log . error ( errorLog ) ;
60+ if ( alreadySent ) {
61+ // the api already set the appropriate error message. we shouldnt do anything now.
62+ return ;
63+ }
5864
59- reply . status ( error . statusCode || 500 ) . send ( 'Internal Server Error' ) ;
65+ const errorMessage = errorCode === 500 ?
66+ 'Internal Server Error' : // if 500, we dont want to expose internal error to user
67+ error . message || 'Internal Server Error' ;
68+ reply . status ( errorCode ) . send ( errorMessage ) ;
6069} ) ;
6170
6271// Add request validation hook
@@ -230,6 +239,19 @@ server.get('/helloAuth', {
230239 return hello ( request , reply ) ;
231240} ) ;
232241
242+ server . setNotFoundHandler ( ( request , reply ) => {
243+ request . log . info ( {
244+ message : 'Route not found (404)' ,
245+ reqId : request . id ,
246+ correlationId : request . correlationId ,
247+ url : request . url ,
248+ method : request . method
249+ } ) ;
250+
251+ // Return 404 page using EJS view
252+ reply . status ( 404 ) . send ( 'notFound' ) ;
253+ } ) ;
254+
233255/**
234256 * Starts the server and listens on the port specified in the configs
235257 */
0 commit comments