Skip to content

Commit 1b08f20

Browse files
committed
Catch errors that occur when a handler is invoked, but before it returns a Promise
1 parent 90ba895 commit 1b08f20

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/shared/protocol.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,14 @@ export abstract class Protocol<
190190
return;
191191
}
192192

193-
handler(notification).catch((error) =>
194-
this._onerror(
195-
new Error(`Uncaught error in notification handler: ${error}`),
196-
),
197-
);
193+
// Starting with Promise.resolve() puts any synchronous errors into the monad as well.
194+
Promise.resolve()
195+
.then(() => handler(notification))
196+
.catch((error) =>
197+
this._onerror(
198+
new Error(`Uncaught error in notification handler: ${error}`),
199+
),
200+
);
198201
}
199202

200203
private _onrequest(request: JSONRPCRequest): void {
@@ -222,7 +225,9 @@ export abstract class Protocol<
222225
const abortController = new AbortController();
223226
this._requestHandlerAbortControllers.set(request.id, abortController);
224227

225-
handler(request, { signal: abortController.signal })
228+
// Starting with Promise.resolve() puts any synchronous errors into the monad as well.
229+
Promise.resolve()
230+
.then(() => handler(request, { signal: abortController.signal }))
226231
.then(
227232
(result) => {
228233
if (abortController.signal.aborted) {

0 commit comments

Comments
 (0)