@@ -409,9 +409,29 @@ class StaticServer {
409
409
await this . _stop ( details ) ;
410
410
}
411
411
412
- _handleAppStateChange ( appState : AppStateStatus ) {
413
- if ( appState === 'active' ) this . start ( 'App entered foreground' ) ;
414
- else this . _stop ( 'App entered background' ) ;
412
+ async _handleAppStateChange ( appState : AppStateStatus ) {
413
+ try {
414
+ if ( appState === 'active' ) await this . start ( 'App entered foreground' ) ;
415
+ else await this . _stop ( 'App entered background' ) ;
416
+ } catch ( e ) {
417
+ // If anything goes wrong within .start() or ._stop() calls, those methods
418
+ // will move the server into the "CRASHED" state, and they'll notify all
419
+ // server state listeners (see .addStateListener()) about the error, with
420
+ // all related details.
421
+ //
422
+ // Thus, if any state listener is connected to the server, we assume it
423
+ // handles possible errors, and we just silently ignore the error here
424
+ // (otherwise some instrumentation, like e.g. Sentry, may catch and report
425
+ // it as unhandled, which is confusing when the error has been handled
426
+ // within the listener).
427
+ //
428
+ // However, if no listeners are connected, we re-throw the error to allow
429
+ // the instrumentation, if any, to detect and report it as unhandled.
430
+ //
431
+ // In either case this very function is used internally, thus no way for
432
+ // the library consumer to directly handle its possible rejections.
433
+ if ( ! this . _stateChangeEmitter . hasListeners ) throw e ;
434
+ }
415
435
}
416
436
}
417
437
0 commit comments