Skip to content

Commit

Permalink
Override instanceof for ConnectError (#974)
Browse files Browse the repository at this point in the history
  • Loading branch information
srikrsna-buf authored Jan 5, 2024
1 parent 3e92153 commit 5b51759
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
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 | 117,288 b | 50,936 b | 13,659 b |
| connect | 117,695 b | 51,217 b | 13,743 b |
| grpc-web | 415,212 b | 300,936 b | 53,420 b |
40 changes: 40 additions & 0 deletions packages/connect/src/connect-error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ import { node16FetchHeadersPolyfill } from "./node16-polyfill-helper.spec.js";

node16FetchHeadersPolyfill();

class ConnectError2 extends Error {
readonly code: Code;
readonly metadata: Headers;
details: Message[];
readonly rawMessage: string;
override name = "ConnectError";
cause: unknown;

constructor(
message: string,
code: Code = Code.Unknown,
metadata?: HeadersInit,
outgoingDetails?: Message[],
cause?: unknown,
) {
super(message);
// see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#example
Object.setPrototypeOf(this, new.target.prototype);
this.rawMessage = message;
this.code = code;
this.metadata = new Headers(metadata ?? {});
this.details = outgoingDetails ?? [];
this.cause = cause;
}
}

describe("ConnectError", () => {
describe("constructor", () => {
it("should have status unknown by default", () => {
Expand Down Expand Up @@ -137,6 +163,20 @@ describe("ConnectError", () => {
expect(got.cause).toBe(error);
});
});
describe("instanceof", () => {
it("works for the actual ConnectError", () => {
expect(new ConnectError("foo")).toBeInstanceOf(ConnectError);
});
it("works for ConnectError like errors", () => {
expect(new ConnectError2("foo")).toBeInstanceOf(ConnectError);
});
it("fails for other errors", () => {
expect(new Error("foo")).not.toBeInstanceOf(ConnectError);
expect(null).not.toBeInstanceOf(ConnectError);
expect(undefined).not.toBeInstanceOf(ConnectError);
expect("err").not.toBeInstanceOf(ConnectError);
});
});
});

describe("assertConnectError() example", () => {
Expand Down
20 changes: 20 additions & 0 deletions packages/connect/src/connect-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ export class ConnectError extends Error {
return new ConnectError(String(reason), code, undefined, undefined, reason);
}

static [Symbol.hasInstance](v: unknown): boolean {
if (!(v instanceof Error)) {
return false;
}
if (Object.getPrototypeOf(v) === ConnectError.prototype) {
return true;
}
return (
v.name === "ConnectError" &&
"code" in v &&
typeof v.code === "number" &&
"metadata" in v &&
"details" in v &&
Array.isArray(v.details) &&
"rawMessage" in v &&
typeof v.rawMessage == "string" &&
"cause" in v
);
}

/**
* Retrieve error details from a ConnectError. On the wire, error details are
* wrapped with google.protobuf.Any, so that a server or middleware can attach
Expand Down

0 comments on commit 5b51759

Please sign in to comment.