Skip to content

Commit ae305f7

Browse files
committed
[futurepress#97] Patches the "Stop in background" feature to avoid possible unhandled promise rejections
1 parent 640b6fc commit ae305f7

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/index.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,29 @@ class StaticServer {
409409
await this._stop(details);
410410
}
411411

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+
}
415435
}
416436
}
417437

0 commit comments

Comments
 (0)