@@ -139,13 +139,13 @@ export async function reset(): Promise<void> {
139139 * Report an error to Checkend
140140 */
141141export function notify ( error : Error , options : NotifyOptions = { } ) : void {
142- if ( ! shouldNotify ( ) ) return
142+ if ( ! shouldNotify ( ) || ! config ) return
143143
144144 const errorClass = error . name || 'Error'
145145 const message = error . message || 'Unknown error'
146146 const code = ( error as NodeJS . ErrnoException ) . code
147147
148- if ( config ! . shouldIgnore ( errorClass , message , code ) ) {
148+ if ( config . shouldIgnore ( errorClass , message , code ) ) {
149149 log ( `Ignoring error: ${ errorClass } ` )
150150 return
151151 }
@@ -154,18 +154,18 @@ export function notify(error: Error, options: NotifyOptions = {}): void {
154154
155155 // Build context with optional environment data
156156 let contextData = { ...globalContext , ...localContext , ...options . context }
157- if ( config ! . sendEnvironmentData ) {
157+ if ( config . sendEnvironmentData ) {
158158 contextData = { ...contextData , env : sanitize ( { ...process . env } ) }
159159 }
160160 const mergedContext = sanitize ( contextData )
161161
162162 // Include user data based on config
163- const mergedUser = config ! . sendUserData
163+ const mergedUser = config . sendUserData
164164 ? sanitize ( { ...globalUser , ...localUser , ...options . user } )
165165 : { }
166166
167167 // Include request data based on config
168- const mergedRequest = config ! . sendRequestData
168+ const mergedRequest = config . sendRequestData
169169 ? sanitize ( { ...localRequest , ...options . request } )
170170 : { }
171171
@@ -175,10 +175,10 @@ export function notify(error: Error, options: NotifyOptions = {}): void {
175175 user : mergedUser ,
176176 fingerprint : options . fingerprint ,
177177 tags : options . tags ,
178- environment : config ! . environment ,
179- rootPath : config ! . rootPath ,
180- appName : config ! . appName ,
181- revision : config ! . revision ,
178+ environment : config . environment ,
179+ rootPath : config . rootPath ,
180+ appName : config . appName ,
181+ revision : config . revision ,
182182 } )
183183
184184 if ( ! runBeforeNotifyCallbacks ( notice ) ) {
@@ -192,24 +192,24 @@ export function notify(error: Error, options: NotifyOptions = {}): void {
192192 * Report an error synchronously (returns promise)
193193 */
194194export async function notifySync ( error : Error , options : NotifyOptions = { } ) : Promise < ApiResponse | null > {
195- if ( ! shouldNotify ( ) ) return null
195+ if ( ! shouldNotify ( ) || ! config || ! client ) return null
196196
197197 const { context : localContext , user : localUser , request : localRequest } = getLocalStorage ( )
198198
199199 // Build context with optional environment data
200200 let contextData = { ...globalContext , ...localContext , ...options . context }
201- if ( config ! . sendEnvironmentData ) {
201+ if ( config . sendEnvironmentData ) {
202202 contextData = { ...contextData , env : sanitize ( { ...process . env } ) }
203203 }
204204 const mergedContext = sanitize ( contextData )
205205
206206 // Include user data based on config
207- const mergedUser = config ! . sendUserData
207+ const mergedUser = config . sendUserData
208208 ? sanitize ( { ...globalUser , ...localUser , ...options . user } )
209209 : { }
210210
211211 // Include request data based on config
212- const mergedRequest = config ! . sendRequestData
212+ const mergedRequest = config . sendRequestData
213213 ? sanitize ( { ...localRequest , ...options . request } )
214214 : { }
215215
@@ -219,17 +219,17 @@ export async function notifySync(error: Error, options: NotifyOptions = {}): Pro
219219 user : mergedUser ,
220220 fingerprint : options . fingerprint ,
221221 tags : options . tags ,
222- environment : config ! . environment ,
223- rootPath : config ! . rootPath ,
224- appName : config ! . appName ,
225- revision : config ! . revision ,
222+ environment : config . environment ,
223+ rootPath : config . rootPath ,
224+ appName : config . appName ,
225+ revision : config . revision ,
226226 } )
227227
228228 if ( ! runBeforeNotifyCallbacks ( notice ) ) {
229229 return null
230230 }
231231
232- return client ! . sendNotice ( notice )
232+ return client . sendNotice ( notice )
233233}
234234
235235// ========== Context Management ==========
@@ -344,25 +344,25 @@ function uninstallUnhandledRejectionHandler(): void {
344344}
345345
346346function handleUncaughtException ( error : Error , origin : NodeJS . UncaughtExceptionOrigin ) : void {
347- if ( ! shouldNotify ( ) ) return
347+ if ( ! shouldNotify ( ) || ! config ) return
348348
349349 const code = ( error as NodeJS . ErrnoException ) . code
350- if ( config ! . shouldIgnore ( error . name , error . message , code ) ) {
350+ if ( config . shouldIgnore ( error . name , error . message , code ) ) {
351351 return
352352 }
353353
354354 const notice = createNotice ( error , {
355355 context : sanitize ( { ...globalContext , unhandled : true , origin } ) ,
356356 tags : [ 'unhandled' , 'uncaughtException' ] ,
357- environment : config ! . environment ,
358- rootPath : config ! . rootPath ,
359- appName : config ! . appName ,
360- revision : config ! . revision ,
357+ environment : config . environment ,
358+ rootPath : config . rootPath ,
359+ appName : config . appName ,
360+ revision : config . revision ,
361361 } )
362362
363363 // Send synchronously for uncaught exceptions
364- client ?. sendNotice ( notice ) . catch ( ( ) => {
365- // Ignore errors when reporting errors
364+ client ?. sendNotice ( notice ) . catch ( ( err ) => {
365+ config ?. logger . debug ( `Failed to send uncaught exception notice: ${ err instanceof Error ? err . message : err } ` )
366366 } )
367367
368368 // Call original handler if it exists
@@ -372,7 +372,7 @@ function handleUncaughtException(error: Error, origin: NodeJS.UncaughtExceptionO
372372}
373373
374374function handleUnhandledRejection ( reason : unknown , promise : Promise < unknown > ) : void {
375- if ( ! shouldNotify ( ) ) return
375+ if ( ! shouldNotify ( ) || ! config ) return
376376
377377 let error : Error
378378 if ( reason instanceof Error ) {
@@ -386,17 +386,17 @@ function handleUnhandledRejection(reason: unknown, promise: Promise<unknown>): v
386386 }
387387
388388 const code = ( error as NodeJS . ErrnoException ) . code
389- if ( config ! . shouldIgnore ( error . name , error . message , code ) ) {
389+ if ( config . shouldIgnore ( error . name , error . message , code ) ) {
390390 return
391391 }
392392
393393 const notice = createNotice ( error , {
394394 context : sanitize ( { ...globalContext , unhandled : true , rejection : true } ) ,
395395 tags : [ 'unhandled' , 'unhandledRejection' ] ,
396- environment : config ! . environment ,
397- rootPath : config ! . rootPath ,
398- appName : config ! . appName ,
399- revision : config ! . revision ,
396+ environment : config . environment ,
397+ rootPath : config . rootPath ,
398+ appName : config . appName ,
399+ revision : config . revision ,
400400 } )
401401
402402 // Queue for async sending
@@ -408,11 +408,15 @@ function handleUnhandledRejection(reason: unknown, promise: Promise<unknown>): v
408408 }
409409}
410410
411+ let isShuttingDown = false
412+
411413function installShutdownHooks ( ) : void {
412414 if ( shutdownHooksInstalled ) return
413415 shutdownHooksInstalled = true
414416
415417 const shutdown = async ( ) => {
418+ if ( isShuttingDown ) return
419+ isShuttingDown = true
416420 await stop ( )
417421 }
418422
@@ -441,7 +445,8 @@ function runBeforeNotifyCallbacks(notice: Notice): boolean {
441445 return false
442446 }
443447 } catch ( e ) {
444- config . logger . warn ( `beforeNotify callback failed: ${ e } ` )
448+ const errorMsg = e instanceof Error ? e . message : String ( e )
449+ config . logger . warn ( `beforeNotify callback failed: ${ errorMsg } ` )
445450 }
446451 }
447452
@@ -452,8 +457,8 @@ function sendNotice(notice: Notice): void {
452457 if ( worker ) {
453458 worker . push ( notice )
454459 } else {
455- client ?. sendNotice ( notice ) . catch ( ( ) => {
456- // Ignore errors when reporting errors
460+ client ?. sendNotice ( notice ) . catch ( ( err ) => {
461+ config ?. logger . debug ( `Failed to send notice: ${ err instanceof Error ? err . message : err } ` )
457462 } )
458463 }
459464}
0 commit comments