Skip to content

Commit

Permalink
fix: error handling on non-Error type errors (#983)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukealvoeiro committed Dec 31, 2023
1 parent cd28979 commit 8c567fc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
16 changes: 16 additions & 0 deletions integration/handle-error-in-default-service/handle-error-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion integration/handle-error-in-default-service/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions integration/handle-error-with-after-response/handle-error-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion integration/handle-error-with-after-response/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/generate-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8c567fc

Please sign in to comment.