Skip to content

Commit

Permalink
Update ConnectError.from to keep the cause (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm authored Jul 18, 2023
1 parent 29fdc72 commit 5930ac1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/connect-web-bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ it like a web server would usually do.

| code generator | bundle size | minified | compressed |
|----------------|-------------------:|-----------------------:|---------------------:|
| connect | 113,630 b | 49,881 b | 13,372 b |
| connect | 113,771 b | 49,913 b | 13,375 b |
| grpc-web | 414,906 b | 301,127 b | 53,279 b |
3 changes: 3 additions & 0 deletions packages/connect/src/connect-error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,22 @@ describe("ConnectError", () => {
expect(got as unknown).toBe(error);
expect(got.code).toBe(Code.PermissionDenied);
expect(got.rawMessage).toBe("Not permitted");
expect(got.cause).toBeUndefined();
});
it("accepts any Error", () => {
const error: unknown = new Error("Not permitted");
const got = ConnectError.from(error);
expect(got as unknown).not.toBe(error);
expect(got.code).toBe(Code.Unknown);
expect(got.rawMessage).toBe("Not permitted");
expect(got.cause).toBe(error);
});
it("accepts string value", () => {
const error: unknown = "Not permitted";
const got = ConnectError.from(error);
expect(got.code).toBe(Code.Unknown);
expect(got.rawMessage).toBe("Not permitted");
expect(got.cause).toBe(error);
});
});
});
Expand Down
12 changes: 10 additions & 2 deletions packages/connect/src/connect-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export class ConnectError extends Error {
* - For other Errors, return the error message with code Unknown by default.
* - For other values, return the values String representation as a message,
* with the code Unknown by default.
* The original value will be used for the "cause" property for the new
* ConnectError.
*/
static from(reason: unknown, code = Code.Unknown): ConnectError {
if (reason instanceof ConnectError) {
Expand All @@ -117,9 +119,15 @@ export class ConnectError extends Error {
// error object, and translate to the appropriate status code.
return new ConnectError(reason.message, Code.Canceled);
}
return new ConnectError(reason.message, code);
return new ConnectError(
reason.message,
code,
undefined,
undefined,
reason
);
}
return new ConnectError(String(reason), code);
return new ConnectError(String(reason), code, undefined, undefined, reason);
}

/**
Expand Down

0 comments on commit 5930ac1

Please sign in to comment.