@@ -29,6 +29,7 @@ interface WithIdentifiedSession {
2929
3030interface ErrorWithStatus extends Error {
3131 status ?: number ;
32+ expected_error ?: boolean ;
3233}
3334
3435function getBasicInfo ( req : Request ) : [ string , Record < string , string | number > ] {
@@ -84,11 +85,15 @@ function finishLog<SLocals extends AnyServiceLocals = ServiceLocals<Configuratio
8485 endLog . u = res . locals . user . id ;
8586 }
8687
88+ let unexpectedError = false ;
8789 if ( error ) {
8890 endLog . e = error . message ;
8991 if ( ! ( error instanceof ServiceError ) || error . log_stack ) {
9092 endLog . st = error . stack ;
9193 }
94+ if ( ! ( error as ErrorWithStatus ) . expected_error ) {
95+ unexpectedError = true ;
96+ }
9297 }
9398
9499 if ( prefs . logRequests ) {
@@ -110,7 +115,11 @@ function finishLog<SLocals extends AnyServiceLocals = ServiceLocals<Configuratio
110115 }
111116
112117 const msg = service . getLogFields ?.( req as RequestWithApp < SLocals > , endLog ) || url ;
113- logger . info ( endLog , msg ) ;
118+ if ( unexpectedError ) {
119+ logger . error ( endLog , msg ) ;
120+ } else {
121+ logger . info ( endLog , msg ) ;
122+ }
114123}
115124
116125export function loggerMiddleware <
@@ -122,7 +131,7 @@ export function loggerMiddleware<
122131) : RequestHandler {
123132 const nonProd = getNodeEnv ( ) !== 'production' ;
124133 const { logger, service } = app . locals ;
125- return function gblogger ( req , res , next ) {
134+ return function serviceLogger ( req , res , next ) {
126135 const logResponse =
127136 config ?. logResponseBody || ( nonProd && req . headers [ 'x-log' ] ?. includes ( 'res' ) ) ;
128137 const logRequest = config ?. logRequestBody || ( nonProd && req . headers [ 'x-log' ] ?. includes ( 'req' ) ) ;
@@ -176,7 +185,7 @@ export function loggerMiddleware<
176185export function errorHandlerMiddleware <
177186 SLocals extends AnyServiceLocals = ServiceLocals < ConfigurationSchema > ,
178187> ( app : ServiceExpress < SLocals > , histogram : Histogram , unnest ?: boolean , returnError ?: boolean ) {
179- const gbErrorHandler : ErrorRequestHandler = ( error , req , res , next ) => {
188+ const svcErrorHandler : ErrorRequestHandler = ( error , req , res , next ) => {
180189 let loggable : Partial < ServiceError > = error ;
181190 const body = error . response ?. body || error . body ;
182191 if ( unnest && body ?. domain && body ?. code && body ?. message ) {
@@ -204,17 +213,17 @@ export function errorHandlerMiddleware<
204213 next ( error ) ;
205214 }
206215 } ;
207- return gbErrorHandler ;
216+ return svcErrorHandler ;
208217}
209218
210219export function notFoundMiddleware ( ) {
211- const gbNotFoundHandler : ServiceHandler = ( req , res , next ) => {
220+ const serviceNotFoundHandler : ServiceHandler = ( req , res , next ) => {
212221 const error = new ServiceError ( req . app , `Cannot ${ req . method } ${ req . path } ` , {
213222 status : 404 ,
214223 code : 'NotFound' ,
215224 domain : 'http' ,
216225 } ) ;
217226 next ( error ) ;
218227 } ;
219- return gbNotFoundHandler as RequestHandler ;
228+ return serviceNotFoundHandler as RequestHandler ;
220229}
0 commit comments