Skip to content

Commit

Permalink
Ensure any error metadata is appended to target grpc response
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Ayers <[email protected]>
  • Loading branch information
smaye81 committed Jan 24, 2025
1 parent 134f9ca commit 97c4622
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/connect/src/protocol-grpc/trailer-status.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,36 @@ describe("setTrailerStatus()", function () {
"CAgSDHNvaXLDqWUg8J+OiRo0Ci50eXBlLmdvb2dsZWFwaXMuY29tL2dvb2dsZS5wcm90b2J1Zi5JbnQzMlZhbHVlEgIIew",
);
});
it("should append any error metadata", function () {
const t = new Headers();
setTrailerStatus(
t,
new ConnectError("soirée 🎉", Code.ResourceExhausted, { foo: "bar" }),
);
let count = 0;
t.forEach(() => count++);
expect(count).toBe(3);
expect(t.get("grpc-status")).toBe("8"); // resource_exhausted
expect(t.get("grpc-message")).toBe("soir%C3%A9e%20%F0%9F%8E%89");
expect(t.get("foo")).toBe("bar");
});
it("should overwrite error metadata that uses reserved protocol headers", function () {
const t = new Headers();
setTrailerStatus(
t,
new ConnectError("soirée 🎉", Code.ResourceExhausted, {
foo: "bar",
"grpc-status": "foo-status",
"grpc-message": "foo-message",
}),
);
let count = 0;
t.forEach(() => count++);
expect(count).toBe(3);
expect(t.get("grpc-status")).toBe("8"); // resource_exhausted
expect(t.get("grpc-message")).toBe("soir%C3%A9e%20%F0%9F%8E%89");
expect(t.get("foo")).toBe("bar");
});
});

describe("findTrailerError()", function () {
Expand Down
7 changes: 7 additions & 0 deletions packages/connect/src/protocol-grpc/trailer-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ export function setTrailerStatus(
error: ConnectError | undefined,
): Headers {
if (error) {
// Copy any metadata specified in the error into the target Headers
// Note that if a protocol header happens to be specified in metadata, it
// its value will be overridden below by the official protocol headers.
error.metadata.forEach((value, key) => {
target.append(key, value);
});

target.set(headerGrpcStatus, error.code.toString(10));
target.set(headerGrpcMessage, encodeURIComponent(error.rawMessage));
if (error.details.length > 0) {
Expand Down

0 comments on commit 97c4622

Please sign in to comment.