-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add error handler to rpc interface (#965)
* adding handle error functionality * improvements and additional testcases * tests wrapped up * refactors * add documentation to readme * remove jest config for debugging in vscode * fix: update with PR feedback * fix: test case had object properties out of order
- Loading branch information
1 parent
bfed59d
commit 47cd16e
Showing
19 changed files
with
688 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
outputBeforeRequest=true,outputAfterResponse=true | ||
rpcBeforeRequest=true,rpcAfterResponse=true,outputServices=default,outputServices=generic-definitions, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
integration/handle-error-in-default-service/handle-error-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { GetBasicResponse, GetBasicRequest, BasicServiceClientImpl, BasicServiceServiceName } from "./simple"; | ||
|
||
interface Rpc { | ||
request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>; | ||
handleError?(service: string, method: string, error: Error): Error; | ||
} | ||
|
||
describe("before-after-request", () => { | ||
const exampleData = { | ||
name: "test-name", | ||
}; | ||
let rpc = { | ||
request: jest.fn(() => Promise.resolve(new Uint8Array())), | ||
}; | ||
let client = new BasicServiceClientImpl(rpc); | ||
let err = new Error("error"); | ||
|
||
let modifiedError = new Error("modified error"); | ||
const handleError = jest.fn(() => modifiedError); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("doesn't perform handleError if error occurs during encode step", async () => { | ||
const encodeSpy = jest.spyOn(GetBasicRequest, "encode").mockImplementation(() => { | ||
throw err; | ||
}); | ||
const req = GetBasicRequest.create(exampleData); | ||
client = new BasicServiceClientImpl({ ...rpc, handleError: handleError }); | ||
try { | ||
await client.GetBasic(req); | ||
} catch (error) { | ||
expect(error).toBe(err); | ||
expect(handleError).not.toHaveBeenCalled(); | ||
} | ||
encodeSpy.mockRestore(); | ||
}); | ||
|
||
it("performs handleError if error occurs when decoding", async () => { | ||
const decodeSpy = jest.spyOn(GetBasicResponse, "decode").mockImplementation(() => { | ||
throw err; | ||
}); | ||
const req = GetBasicRequest.create(exampleData); | ||
client = new BasicServiceClientImpl({ ...rpc, handleError: handleError }); | ||
try { | ||
await client.GetBasic(req); | ||
} catch (error) { | ||
expect(error).toBe(modifiedError); | ||
expect(handleError).toHaveBeenCalledWith(BasicServiceServiceName, "GetBasic", err); | ||
} | ||
decodeSpy.mockRestore(); | ||
}); | ||
|
||
it("doesn't perform handleError if it is not specified", async () => { | ||
const req = GetBasicRequest.create(exampleData); | ||
client = new BasicServiceClientImpl(rpc); | ||
try { | ||
await client.GetBasic(req); | ||
} catch (error) { | ||
expect(error).toBe(err); | ||
expect(handleError).not.toHaveBeenCalled(); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
outputServices=default,rpcErrorHandler=true |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
syntax = "proto3"; | ||
package basic; | ||
|
||
message GetBasicRequest { | ||
string name = 1; | ||
} | ||
|
||
message GetBasicResponse { | ||
string name = 1; | ||
} | ||
|
||
service BasicService { | ||
rpc GetBasic (GetBasicRequest) returns (GetBasicResponse) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/* eslint-disable */ | ||
import * as _m0 from "protobufjs/minimal"; | ||
|
||
export const protobufPackage = "basic"; | ||
|
||
export interface GetBasicRequest { | ||
name: string; | ||
} | ||
|
||
export interface GetBasicResponse { | ||
name: string; | ||
} | ||
|
||
function createBaseGetBasicRequest(): GetBasicRequest { | ||
return { name: "" }; | ||
} | ||
|
||
export const GetBasicRequest = { | ||
encode(message: GetBasicRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer { | ||
if (message.name !== "") { | ||
writer.uint32(10).string(message.name); | ||
} | ||
return writer; | ||
}, | ||
|
||
decode(input: _m0.Reader | Uint8Array, length?: number): GetBasicRequest { | ||
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input); | ||
let end = length === undefined ? reader.len : reader.pos + length; | ||
const message = createBaseGetBasicRequest(); | ||
while (reader.pos < end) { | ||
const tag = reader.uint32(); | ||
switch (tag >>> 3) { | ||
case 1: | ||
if (tag !== 10) { | ||
break; | ||
} | ||
|
||
message.name = reader.string(); | ||
continue; | ||
} | ||
if ((tag & 7) === 4 || tag === 0) { | ||
break; | ||
} | ||
reader.skipType(tag & 7); | ||
} | ||
return message; | ||
}, | ||
|
||
fromJSON(object: any): GetBasicRequest { | ||
return { name: isSet(object.name) ? globalThis.String(object.name) : "" }; | ||
}, | ||
|
||
toJSON(message: GetBasicRequest): unknown { | ||
const obj: any = {}; | ||
if (message.name !== "") { | ||
obj.name = message.name; | ||
} | ||
return obj; | ||
}, | ||
|
||
create<I extends Exact<DeepPartial<GetBasicRequest>, I>>(base?: I): GetBasicRequest { | ||
return GetBasicRequest.fromPartial(base ?? ({} as any)); | ||
}, | ||
fromPartial<I extends Exact<DeepPartial<GetBasicRequest>, I>>(object: I): GetBasicRequest { | ||
const message = createBaseGetBasicRequest(); | ||
message.name = object.name ?? ""; | ||
return message; | ||
}, | ||
}; | ||
|
||
function createBaseGetBasicResponse(): GetBasicResponse { | ||
return { name: "" }; | ||
} | ||
|
||
export const GetBasicResponse = { | ||
encode(message: GetBasicResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer { | ||
if (message.name !== "") { | ||
writer.uint32(10).string(message.name); | ||
} | ||
return writer; | ||
}, | ||
|
||
decode(input: _m0.Reader | Uint8Array, length?: number): GetBasicResponse { | ||
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input); | ||
let end = length === undefined ? reader.len : reader.pos + length; | ||
const message = createBaseGetBasicResponse(); | ||
while (reader.pos < end) { | ||
const tag = reader.uint32(); | ||
switch (tag >>> 3) { | ||
case 1: | ||
if (tag !== 10) { | ||
break; | ||
} | ||
|
||
message.name = reader.string(); | ||
continue; | ||
} | ||
if ((tag & 7) === 4 || tag === 0) { | ||
break; | ||
} | ||
reader.skipType(tag & 7); | ||
} | ||
return message; | ||
}, | ||
|
||
fromJSON(object: any): GetBasicResponse { | ||
return { name: isSet(object.name) ? globalThis.String(object.name) : "" }; | ||
}, | ||
|
||
toJSON(message: GetBasicResponse): unknown { | ||
const obj: any = {}; | ||
if (message.name !== "") { | ||
obj.name = message.name; | ||
} | ||
return obj; | ||
}, | ||
|
||
create<I extends Exact<DeepPartial<GetBasicResponse>, I>>(base?: I): GetBasicResponse { | ||
return GetBasicResponse.fromPartial(base ?? ({} as any)); | ||
}, | ||
fromPartial<I extends Exact<DeepPartial<GetBasicResponse>, I>>(object: I): GetBasicResponse { | ||
const message = createBaseGetBasicResponse(); | ||
message.name = object.name ?? ""; | ||
return message; | ||
}, | ||
}; | ||
|
||
export interface BasicService { | ||
GetBasic(request: GetBasicRequest): Promise<GetBasicResponse>; | ||
} | ||
|
||
export const BasicServiceServiceName = "basic.BasicService"; | ||
export class BasicServiceClientImpl implements BasicService { | ||
private readonly rpc: Rpc; | ||
private readonly service: string; | ||
constructor(rpc: Rpc, opts?: { service?: string }) { | ||
this.service = opts?.service || BasicServiceServiceName; | ||
this.rpc = rpc; | ||
this.GetBasic = this.GetBasic.bind(this); | ||
} | ||
GetBasic(request: GetBasicRequest): Promise<GetBasicResponse> { | ||
const data = GetBasicRequest.encode(request).finish(); | ||
const promise = this.rpc.request(this.service, "GetBasic", data); | ||
return promise.then((data) => { | ||
try { | ||
return GetBasicResponse.decode(_m0.Reader.create(data)); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
}).catch((error) => { | ||
if (error instanceof Error && this.rpc.handleError) { | ||
return Promise.reject(this.rpc.handleError(this.service, "GetBasic", error)); | ||
} | ||
return Promise.reject(error); | ||
}); | ||
} | ||
} | ||
|
||
interface Rpc { | ||
request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>; | ||
handleError?(service: string, method: string, error: Error): Error; | ||
} | ||
|
||
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; | ||
|
||
export type DeepPartial<T> = T extends Builtin ? T | ||
: T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>> | ||
: T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> | ||
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> } | ||
: Partial<T>; | ||
|
||
type KeysOfUnion<T> = T extends T ? keyof T : never; | ||
export type Exact<P, I extends P> = P extends Builtin ? P | ||
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never }; | ||
|
||
function isSet(value: any): boolean { | ||
return value !== null && value !== undefined; | ||
} |
Oops, something went wrong.