diff --git a/src/client.ts b/src/client.ts index cf92a21..d083153 100644 --- a/src/client.ts +++ b/src/client.ts @@ -59,6 +59,7 @@ type GrpcEventPayload = error: string; code?: number; trailers?: GrpcMetadata; + headers?: GrpcMetadata; } | { type: 'headers'; payload: GrpcMetadata; @@ -150,11 +151,27 @@ export class GrpcClient { this.#deferredMap.delete(event.id); break; case 'error': - const error = new GrpcError(event.error, event.code, event.trailers); - - deferred.response?.reject(error); - deferred.data?.noitfyError(error); - + // TODO: handle the grpc trailers (and not just the grpc headers). + // This are slightly trickier as they come after the error event and + // may or may not come as opposed to the headers which are always + // resolved by the time we get the error event. + let handleError = (hdrs?: GrpcMetadata) => { + const error = new GrpcError( + event.error, + event.code, + event.trailers, + hdrs + ); + deferred.response?.reject(error); + deferred.data?.noitfyError(error); + }; + deferred.headers?.promise + .then((h) => { + handleError(h); + }) + .catch(() => { + handleError(undefined); + }); break; } } diff --git a/src/errors.ts b/src/errors.ts index fc8841c..7a60eaa 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -4,7 +4,8 @@ export class GrpcError extends Error { constructor( public error: string, public code?: number, - public trailers?: GrpcMetadata + public trailers?: GrpcMetadata, + public headers?: GrpcMetadata ) { super(error); }