Skip to content

Commit

Permalink
fix: properly set error status on messages when catching an api error (
Browse files Browse the repository at this point in the history
…#217)

* fix: properly set error status when catching an api error

Signed-off-by: axel7083 <[email protected]>

* fix: racing issue and unit tests

Signed-off-by: axel7083 <[email protected]>

* fix: error propagation

Signed-off-by: axel7083 <[email protected]>

* fix: resolving comments

Signed-off-by: axel7083 <[email protected]>

---------

Signed-off-by: axel7083 <[email protected]>
  • Loading branch information
axel7083 authored Feb 5, 2024
1 parent 24802ec commit 83fb35b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
11 changes: 11 additions & 0 deletions packages/shared/src/messages/MessageProxy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,14 @@ test('Test register instance with async', async () => {
const proxy = rpcBrowser.getProxy<Dummy>();
expect(await proxy.ping()).toBe('pong');
});

test('Test raising exception', async () => {
const rpcExtension = new RpcExtension(webview);
const rpcBrowser = new RpcBrowser(window, api);

rpcExtension.register('raiseError', () => {
throw new Error('big error');
});

await expect(rpcBrowser.invoke('raiseError')).rejects.toThrow('big error');
});
23 changes: 18 additions & 5 deletions packages/shared/src/messages/MessageProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,23 @@ export class RpcExtension {
body: result,
status: 'success',
} as IMessageResponse);
} catch (e) {
} catch (err: unknown) {
let errorMessage: string;
// Depending on the object throw we try to extract the error message
if (err instanceof Error) {
errorMessage = err.message;
} else if (typeof err === 'string') {
errorMessage = err;
} else {
errorMessage = String(err);
}

await this.webview.postMessage({
id: message.id,
channel: message.channel,
body: undefined,
error: `Something went wrong on channel ${message.channel}: ${String(e)}`,
status: 'error',
error: errorMessage,
} as IMessageResponse);
}
});
Expand Down Expand Up @@ -174,6 +185,10 @@ export class RpcBrowser {
// Generate a unique id for the request
const requestId = this.getUniqueId();

const promise = new Promise((resolve, reject) => {
this.promises.set(requestId, { resolve, reject });
});

// Post the message
this.api.postMessage({
id: requestId,
Expand All @@ -190,9 +205,7 @@ export class RpcBrowser {
}, 5000);

// Create a Promise
return new Promise((resolve, reject) => {
this.promises.set(requestId, { resolve, reject });
});
return promise;
}

// TODO(feloy) need to subscribe several times?
Expand Down

0 comments on commit 83fb35b

Please sign in to comment.