diff --git a/integration/handle-error-in-default-service/handle-error-test.ts b/integration/handle-error-in-default-service/handle-error-test.ts index fcec1e510..ffc6a8aac 100644 --- a/integration/handle-error-in-default-service/handle-error-test.ts +++ b/integration/handle-error-in-default-service/handle-error-test.ts @@ -52,6 +52,22 @@ describe("before-after-request", () => { decodeSpy.mockRestore(); }); + it("performs handleError even if error is not an Error instance", async () => { + const errString = "some error"; + const decodeSpy = jest.spyOn(GetBasicResponse, "decode").mockImplementation(() => { + throw errString; + }); + 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", errString); + } + decodeSpy.mockRestore(); + }); + it("doesn't perform handleError if it is not specified", async () => { const req = GetBasicRequest.create(exampleData); client = new BasicServiceClientImpl(rpc); diff --git a/integration/handle-error-in-default-service/simple.ts b/integration/handle-error-in-default-service/simple.ts index 7dda0512b..0e20b503f 100644 --- a/integration/handle-error-in-default-service/simple.ts +++ b/integration/handle-error-in-default-service/simple.ts @@ -148,7 +148,7 @@ export class BasicServiceClientImpl implements BasicService { return Promise.reject(error); } }).catch((error) => { - if (error instanceof Error && this.rpc.handleError) { + if (this.rpc.handleError) { return Promise.reject(this.rpc.handleError(this.service, "GetBasic", error)); } return Promise.reject(error); diff --git a/integration/handle-error-with-after-response/handle-error-test.ts b/integration/handle-error-with-after-response/handle-error-test.ts index 5cd51d0ff..61c599917 100644 --- a/integration/handle-error-with-after-response/handle-error-test.ts +++ b/integration/handle-error-with-after-response/handle-error-test.ts @@ -52,6 +52,22 @@ describe("before-after-request", () => { decodeSpy.mockRestore(); }); + it("performs handleError if error is not an Error instance", async () => { + const errString = "some error"; + const decodeSpy = jest.spyOn(GetBasicResponse, "decode").mockImplementation(() => { + throw errString; + }); + 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", errString); + } + decodeSpy.mockRestore(); + }); + it("performs handleError if error occurs when calling afterResponse", async () => { const decodeSpy = jest.spyOn(GetBasicResponse, "decode").mockReturnValue(exampleData); const req = GetBasicRequest.create(exampleData); diff --git a/integration/handle-error-with-after-response/simple.ts b/integration/handle-error-with-after-response/simple.ts index b0bc76c5a..74d0ff9f1 100644 --- a/integration/handle-error-with-after-response/simple.ts +++ b/integration/handle-error-with-after-response/simple.ts @@ -152,7 +152,7 @@ export class BasicServiceClientImpl implements BasicService { return Promise.reject(error); } }).catch((error) => { - if (error instanceof Error && this.rpc.handleError) { + if (this.rpc.handleError) { return Promise.reject(this.rpc.handleError(this.service, "GetBasic", error)); } return Promise.reject(error); diff --git a/src/generate-services.ts b/src/generate-services.ts index d81594337..2a1d9f33d 100644 --- a/src/generate-services.ts +++ b/src/generate-services.ts @@ -123,7 +123,7 @@ function generateRegularRpcMethod(ctx: Context, methodDesc: MethodDescriptorProt let errorHandler; if (options.rpcErrorHandler) { errorHandler = code` - if (error instanceof Error && this.rpc.handleError) { + if (this.rpc.handleError) { return Promise.reject(this.rpc.handleError(this.service, "${methodDesc.name}", error)); } return Promise.reject(error);