Skip to content

Commit 176ddaf

Browse files
committed
Handle grpc headers in error path.
Right now the grpc headers are ignored but they contain useful information even for grpc errors. This change augments the error with a headers field and fills it in with the grpc headers if they are available.
1 parent fd5660a commit 176ddaf

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

src/client.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type GrpcEventPayload =
5959
error: string;
6060
code?: number;
6161
trailers?: GrpcMetadata;
62+
headers?: GrpcMetadata;
6263
} | {
6364
type: 'headers';
6465
payload: GrpcMetadata;
@@ -150,11 +151,18 @@ export class GrpcClient {
150151
this.#deferredMap.delete(event.id);
151152
break;
152153
case 'error':
153-
const error = new GrpcError(event.error, event.code, event.trailers);
154-
155-
deferred.response?.reject(error);
156-
deferred.data?.noitfyError(error);
157-
154+
// TODO: handle the grpc trailers (and not just the grpc headers).
155+
// This are slightly trickier as they come after the error event and
156+
// may or may not come as opposed to the headers which are always
157+
// resolved by the time we get the error event.
158+
let handleError = (hdrs?: GrpcMetadata) => {
159+
const error = new GrpcError(event.error, event.code, event.trailers, hdrs);
160+
deferred.response?.reject(error);
161+
deferred.data?.noitfyError(error);
162+
}
163+
deferred.headers?.promise
164+
.then((h) => { handleError(h); })
165+
.catch(() => { handleError(undefined); })
158166
break;
159167
}
160168
}

src/errors.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export class GrpcError extends Error {
44
constructor(
55
public error: string,
66
public code?: number,
7-
public trailers?: GrpcMetadata
7+
public trailers?: GrpcMetadata,
8+
public headers?: GrpcMetadata
89
) {
910
super(error);
1011
}

0 commit comments

Comments
 (0)