Skip to content

Commit e79b547

Browse files
authored
Merge pull request #8 from hfour/goce/H4C-246_error_handling_rpc_module
H4C-246: Error handling in the JSONRpc module
2 parents 4568784 + 34f3e16 commit e79b547

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

src/index.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ describe("json-rpc-e2e", () => {
3434
});
3535
});
3636

37+
it(`should throw an error on /rpc/v1/ test.testError (POST)`, () => {
38+
const errorObj = {
39+
message: "RPC EXCEPTION",
40+
code: 403,
41+
data: {
42+
fromService: "Test Service",
43+
params: { data: "hi" }
44+
}
45+
};
46+
return request(server.server)
47+
.post("/rpc/v1")
48+
.send({ method: "test.testError", params: { data: "hi" } })
49+
.expect(403)
50+
.expect(errorObj);
51+
});
52+
3753
afterAll(async () => {
3854
await app.close();
3955
});

src/index.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as express from "express";
22
import * as http from "http";
33

4-
import { Server, CustomTransportStrategy } from "@nestjs/microservices";
4+
import { Server, CustomTransportStrategy, RpcException } from "@nestjs/microservices";
55
import { Injectable, Controller } from "@nestjs/common";
66
import { MessagePattern } from "@nestjs/microservices";
77

@@ -21,8 +21,8 @@ export interface JSONRPCServerOptions {
2121
*/
2222
hostname?: string;
2323
/*
24-
* The path at which the JSON RPC endpoint should be mounted
25-
*/
24+
* The path at which the JSON RPC endpoint should be mounted
25+
*/
2626
path: string;
2727
}
2828

@@ -74,7 +74,10 @@ export class JSONRPCServer extends Server implements CustomTransportStrategy {
7474
);
7575

7676
if ("error" in response) {
77-
res.status(500).json({ error: response.error.message })
77+
let resp = { code: 500, message: response.error.message, data: undefined };
78+
if ("code" in response.error) resp.code = response.error.code;
79+
if ("data" in response.error) resp.data = response.error.data;
80+
res.status(resp.code).json(resp);
7881
} else {
7982
res.status(200).json(response.value);
8083
}
@@ -96,3 +99,9 @@ export class JSONRPCServer extends Server implements CustomTransportStrategy {
9699
// do nothing, maybe block further requests
97100
}
98101
}
102+
103+
export class CodedRpcException extends RpcException {
104+
constructor(message: string, public code: number = 500, public data: any = {}) {
105+
super({ message, code, data });
106+
}
107+
}

src/test-handler.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
Scope
1313
} from "@nestjs/common";
1414

15-
import { JSONRpcService } from ".";
15+
import { JSONRpcService, CodedRpcException } from ".";
1616

1717
const initialModuleState = {
1818
pipeCalled: false,
@@ -77,4 +77,12 @@ export class TestService {
7777
console.log("Invoke WAS called");
7878
return params;
7979
}
80+
81+
@UsePipes(TestPipe)
82+
@UseInterceptors(TestInterceptor)
83+
@UseGuards(TestGuard)
84+
public async testError(params: any) {
85+
// construct the error object with some data inside
86+
throw new CodedRpcException("RPC EXCEPTION", 403, { fromService: "Test Service", params });
87+
}
8088
}

0 commit comments

Comments
 (0)