Skip to content

Commit

Permalink
add test for error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
petehunt committed Nov 27, 2019
1 parent d7daf44 commit 5015eda
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"license": "ISC",
"dependencies": {
"invariant": "^2.2.4",
"ts-json-rpc-server": "0.0.4"
"ts-json-rpc-server": "0.0.5"
},
"devDependencies": {
"@types/express": "^4.17.0",
Expand All @@ -28,5 +28,8 @@
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
"files": ["src/*.js", "src/*.d.ts"]
"files": [
"src/*.js",
"src/*.d.ts"
]
}
10 changes: 10 additions & 0 deletions src/rpc-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ test("integration", async t => {
},
async goodbye() {
return "goodbye";
},
async err() {
throw new Error("this was an error");
}
};
const server = await runDefaultServer(methods, 2288);
Expand All @@ -22,5 +25,12 @@ test("integration", async t => {
t.equal(rv, "hello, pete!");
t.equal(await client.goodbye(), "goodbye");

try {
await client.err();
t.fail();
} catch (e) {
t.equal(e.message, "Error: this was an error");
}

server.close();
});
2 changes: 1 addition & 1 deletion src/rpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function createRpcClient<T extends RpcMethods>(
invariant(responseJson.jsonrpc === "2.0", "invalid jsonrpc version");
invariant(responseJson.id === id, "invalid response id");
if (responseJson.error) {
throw new Error(responseJson.message);
throw new Error(responseJson.error.message);
}
return responseJson.result;
};
Expand Down

0 comments on commit 5015eda

Please sign in to comment.