Skip to content

Commit 7e230bd

Browse files
committed
- fix id counter
- throw error in rpc client
1 parent a1883c5 commit 7e230bd

File tree

2 files changed

+8
-28
lines changed

2 files changed

+8
-28
lines changed

src/client-proxy.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ClientProxy } from "@nestjs/microservices";
22
import axios from "axios";
33
import { resolve } from "dns";
4+
import { CodedRpcException } from ".";
45

56
export class JSONRPCClient extends ClientProxy {
67
constructor(private readonly url: string) {
@@ -29,7 +30,7 @@ export class JSONRPCClient extends ClientProxy {
2930
}
3031
getService<SvcInterface>(namespace: string): ServiceClient<SvcInterface> {
3132
let url = this.url;
32-
let id = this.counter;
33+
let id = ++this.counter;
3334
let jsonrpc = this.jsonrpc;
3435
return new Proxy(
3536
{},
@@ -41,14 +42,13 @@ export class JSONRPCClient extends ClientProxy {
4142
method: namespace + "." + prop.toString(),
4243
params,
4344
jsonrpc: "2.0",
44-
id: ++id
45+
id
4546
})
4647
.then(res => ({ jsonrpc, result: res, id }))
4748
.catch(err => {
4849
const { code, message, data } = err.response.data;
49-
let resp = { code, message, data };
5050

51-
return { jsonrpc, error: resp, id };
51+
throw new CodedRpcException(message, code, data);
5252
});
5353
};
5454
}

src/index.spec.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Test } from "@nestjs/testing";
44
import { INestMicroservice, ServiceUnavailableException } from "@nestjs/common";
55

66
import { TestService } from "./test-handler";
7-
import { JSONRPCServer } from ".";
7+
import { JSONRPCServer, CodedRpcException } from ".";
88
import { JSONRPCClient } from "./client-proxy";
99

1010
describe("json-rpc-e2e", () => {
@@ -37,27 +37,7 @@ describe("json-rpc-e2e", () => {
3737
.then(res => expect(res.result.data).toStrictEqual({ data: "hi" }));
3838
});
3939

40-
it(`should check error object properties from JSONRPCClient call`, () => {
41-
const jsonRpcErrorObj = {
42-
id: expect.stringMatching,
43-
jsonrpc: "",
44-
error: {
45-
message: "",
46-
code: 403,
47-
data: {
48-
fromService: "",
49-
params: { data: "" }
50-
}
51-
}
52-
};
53-
return service.testError({ data: "hi" }).then(res => {
54-
expect(res).toHaveProperty("id");
55-
expect(res).toHaveProperty("jsonrpc");
56-
expect(res).toHaveProperty("error");
57-
});
58-
});
59-
60-
it(`should return an error and check error data from JSONRPCClient call`, () => {
40+
it(`should return an error and check error data from JSONRPCClient call`, async () => {
6141
const errorObj = {
6242
message: "RPC EXCEPTION",
6343
code: 403,
@@ -66,8 +46,8 @@ describe("json-rpc-e2e", () => {
6646
params: { data: "hi" }
6747
}
6848
};
69-
70-
return service.testError({ data: "hi" }).then(res => expect(res.error).toStrictEqual(errorObj));
49+
const resp = service.testError({ data: "hi" });
50+
return expect(resp).rejects.toThrowError();
7151
});
7252

7353
afterAll(async () => {

0 commit comments

Comments
 (0)