Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error handling on non-Error type errors #983

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading