Skip to content

Commit

Permalink
Fix passing the error for a promise reject.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasny committed Mar 10, 2023
1 parent 62a3414 commit 2f8c4ff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
13 changes: 8 additions & 5 deletions src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ export default class Listener {

constructor(private readonly methods: {[fn: string]: (...args: any[]) => any}) {}

private send(event: MessageEvent<RPCRequest>, resultOrPromise: any): void {
Promise.resolve(resultOrPromise)
.then(result => this.sendResult(event, result))
.catch(error => this.sendError(event, error));
}

private sendResult(event: MessageEvent<RPCRequest>, result: any): void {
const {channel, id} = event.data;
const source: MessageTarget = (event.source as MessageTarget) || this.fallbackSource;
const targetOrigin = event.origin && event.origin !== "null" ? event.origin : "*";

Promise.resolve(result).then(r => {
source.postMessage({'@rpc': RESPONSE_TYPE, channel, id, result: r}, targetOrigin);
});
source.postMessage({'@rpc': RESPONSE_TYPE, channel, id, result}, targetOrigin);
}

private sendError(event: MessageEvent<RPCRequest>, error: any): void {
Expand All @@ -39,8 +43,7 @@ export default class Listener {
}

try {
const response = this.methods[fn](...args);
this.sendResult(event, response);
this.send(event, this.methods[fn](...args));
} catch (error) {
this.sendError(event, error);
}
Expand Down
34 changes: 18 additions & 16 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {connect, Listener} from "../src";
type MathRPC = {
add: (a: number, b: number) => Promise<number>;
sub: (a: number, b: number) => Promise<number>;
err: () => Promise<void>;
err: () => Promise<never>;
reject: () => Promise<never>;
unknown: () => Promise<never>;
}

Expand All @@ -20,7 +21,8 @@ describe("simple-iframe-rpc", () => {
client = new Listener({
add: (a, b) => a + b,
sub: (a, b) => Promise.resolve(a - b),
err: () => { throw new Error("Oops"); }
err: () => { throw new Error("Oops"); },
reject: () => Promise.reject("Denied"),
});
client.listen(child, "*");
});
Expand All @@ -46,22 +48,22 @@ describe("simple-iframe-rpc", () => {
assert.equal(result, 1);
});

it("throws an error", async () => {
try {
await rpc.err();
assert.fail("No error was thrown");
} catch (e) {
assert.equal(e.message || e, 'Oops');
}
it("throws an error", () => {
rpc.err()
.then(() => assert.fail("No error was thrown"))
.catch(e => assert.equal(e.message, 'Oops'));
});

it("throws an error if an undefined method is called", async () => {
try {
await rpc.unknown();
assert.fail("No error was thrown");
} catch (e) {
assert.equal(e.message || e, "RPC method 'unknown' is not defined");
}
it("throws an error for a rejected promise", () => {
rpc.reject()
.then(() => assert.fail("No error was thrown"))
.catch(e => assert.equal(e, 'Denied'));
});

it("throws an error if an undefined method is called", () => {
rpc.unknown()
.then(() => assert.fail("No error was thrown"))
.catch(e => assert.equal(e.message, "RPC method 'unknown' is not defined"));
});

it("gives a timeout when there's no response", async () => {
Expand Down

0 comments on commit 2f8c4ff

Please sign in to comment.