@@ -46,9 +46,11 @@ async function errorHandlingMiddleware(
4646 uid : monkeyError . uid ?? req . ctx ?. decodedToken ?. uid ,
4747 } ;
4848 let message = "Unknown error" ;
49+ let isDbError = false ;
4950
5051 if ( / E C O N N R E F U S E D .* 2 7 0 1 7 / i. test ( error . message ) ) {
5152 message = "Could not connect to the database. It may be down." ;
53+ isDbError = true ;
5254 } else if ( error instanceof URIError || error instanceof SyntaxError ) {
5355 status = 400 ;
5456 message = "Unprocessable request" ;
@@ -73,27 +75,30 @@ async function errorHandlingMiddleware(
7375 errorId : string ;
7476 } ;
7577
76- try {
77- await addLog (
78- "system_error" ,
79- `${ status } ${ errorId } ${ error . message } ${ error . stack } ` ,
80- uid
81- ) ;
82- await db . collection < DBError > ( "errors" ) . insertOne ( {
83- _id : errorId ,
84- timestamp : Date . now ( ) ,
85- status : status ,
86- uid,
87- message : error . message ,
88- stack : error . stack ,
89- endpoint : req . originalUrl ,
90- method : req . method ,
91- url : req . url ,
92- } ) ;
93- } catch ( e ) {
94- Logger . error ( "Logging to db failed." ) ;
95- Logger . error ( getErrorMessage ( e ) ?? "Unknown error" ) ;
96- console . error ( e ) ;
78+ if ( ! isDbError ) {
79+ try {
80+ await addLog (
81+ "system_error" ,
82+ `${ status } ${ errorId } ${ error . message } ${ error . stack } ` ,
83+ uid
84+ ) ;
85+
86+ await db . collection < DBError > ( "errors" ) . insertOne ( {
87+ _id : errorId ,
88+ timestamp : Date . now ( ) ,
89+ status : status ,
90+ uid,
91+ message : error . message ,
92+ stack : error . stack ,
93+ endpoint : req . originalUrl ,
94+ method : req . method ,
95+ url : req . url ,
96+ } ) ;
97+ } catch ( e ) {
98+ Logger . error ( "Logging to db failed." ) ;
99+ Logger . error ( getErrorMessage ( e ) ?? "Unknown error" ) ;
100+ console . error ( e ) ;
101+ }
97102 }
98103 } else {
99104 Logger . error ( `Error: ${ error . message } Stack: ${ error . stack } ` ) ;
0 commit comments