-
Notifications
You must be signed in to change notification settings - Fork 10
fix: Preserve original gRPC error cause in rewindInstance and restartOrchestration #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
YunchuWang
wants to merge
1
commit into
main
Choose a base branch
from
copilot-finds/bug/preserve-grpc-error-cause-in-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
215 changes: 215 additions & 0 deletions
215
packages/durabletask-js/test/client-error-cause.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import * as grpc from "@grpc/grpc-js"; | ||
| import { TaskHubGrpcClient } from "../src"; | ||
|
|
||
| /** | ||
| * Creates a mock gRPC ServiceError with the specified status code and details. | ||
| */ | ||
| function createGrpcError(code: grpc.status, details: string = ""): grpc.ServiceError { | ||
| const error = new Error(details) as grpc.ServiceError; | ||
| error.code = code; | ||
| error.details = details; | ||
| error.metadata = new grpc.Metadata(); | ||
| return error; | ||
| } | ||
|
|
||
| /** | ||
| * Accesses the internal _stub on a TaskHubGrpcClient for test mocking. | ||
| */ | ||
| function getStub(client: TaskHubGrpcClient): any { | ||
| return (client as any)._stub; | ||
| } | ||
|
|
||
| describe("TaskHubGrpcClient error cause preservation", () => { | ||
| let client: TaskHubGrpcClient; | ||
|
|
||
| beforeEach(() => { | ||
| client = new TaskHubGrpcClient({ hostAddress: "localhost:4001" }); | ||
| }); | ||
|
|
||
| describe("rewindInstance", () => { | ||
| it("should preserve error cause for NOT_FOUND gRPC errors", async () => { | ||
| const grpcError = createGrpcError(grpc.status.NOT_FOUND, "Instance not found"); | ||
|
|
||
| getStub(client).rewindInstance = (_req: any, _metadata: any, callback: any) => { | ||
| callback(grpcError, null); | ||
| return {} as grpc.ClientUnaryCall; | ||
| }; | ||
|
|
||
| try { | ||
| await client.rewindInstance("test-instance", "test reason"); | ||
| fail("Expected an error to be thrown"); | ||
| } catch (e: unknown) { | ||
| expect(e).toBeInstanceOf(Error); | ||
| const error = e as Error; | ||
| expect(error.message).toContain("test-instance"); | ||
| expect(error.message).toContain("was not found"); | ||
| expect(error.cause).toBe(grpcError); | ||
| } | ||
| }); | ||
|
|
||
| it("should preserve error cause for FAILED_PRECONDITION gRPC errors", async () => { | ||
| const grpcError = createGrpcError( | ||
| grpc.status.FAILED_PRECONDITION, | ||
| "Orchestration is running", | ||
| ); | ||
|
|
||
| getStub(client).rewindInstance = (_req: any, _metadata: any, callback: any) => { | ||
| callback(grpcError, null); | ||
| return {} as grpc.ClientUnaryCall; | ||
| }; | ||
|
|
||
| try { | ||
| await client.rewindInstance("test-instance", "test reason"); | ||
| fail("Expected an error to be thrown"); | ||
| } catch (e: unknown) { | ||
| expect(e).toBeInstanceOf(Error); | ||
| const error = e as Error; | ||
| expect(error.message).toBe("Orchestration is running"); | ||
| expect(error.cause).toBe(grpcError); | ||
| } | ||
| }); | ||
|
|
||
| it("should preserve error cause for UNIMPLEMENTED gRPC errors", async () => { | ||
| const grpcError = createGrpcError(grpc.status.UNIMPLEMENTED, ""); | ||
|
|
||
| getStub(client).rewindInstance = (_req: any, _metadata: any, callback: any) => { | ||
| callback(grpcError, null); | ||
| return {} as grpc.ClientUnaryCall; | ||
| }; | ||
|
|
||
| try { | ||
| await client.rewindInstance("test-instance", "test reason"); | ||
| fail("Expected an error to be thrown"); | ||
| } catch (e: unknown) { | ||
| expect(e).toBeInstanceOf(Error); | ||
| const error = e as Error; | ||
| expect(error.message).toContain("not supported by the backend"); | ||
| expect(error.cause).toBe(grpcError); | ||
| } | ||
| }); | ||
|
|
||
| it("should preserve error cause for CANCELLED gRPC errors", async () => { | ||
| const grpcError = createGrpcError(grpc.status.CANCELLED, "Cancelled"); | ||
|
|
||
| getStub(client).rewindInstance = (_req: any, _metadata: any, callback: any) => { | ||
| callback(grpcError, null); | ||
| return {} as grpc.ClientUnaryCall; | ||
| }; | ||
|
|
||
| try { | ||
| await client.rewindInstance("test-instance", "test reason"); | ||
| fail("Expected an error to be thrown"); | ||
| } catch (e: unknown) { | ||
| expect(e).toBeInstanceOf(Error); | ||
| const error = e as Error; | ||
| expect(error.message).toContain("was cancelled"); | ||
| expect(error.cause).toBe(grpcError); | ||
| } | ||
| }); | ||
|
|
||
| it("should rethrow unrecognized gRPC errors without wrapping", async () => { | ||
| const grpcError = createGrpcError(grpc.status.INTERNAL, "Internal server error"); | ||
|
|
||
| getStub(client).rewindInstance = (_req: any, _metadata: any, callback: any) => { | ||
| callback(grpcError, null); | ||
| return {} as grpc.ClientUnaryCall; | ||
| }; | ||
|
|
||
| try { | ||
| await client.rewindInstance("test-instance", "test reason"); | ||
| fail("Expected an error to be thrown"); | ||
| } catch (e: unknown) { | ||
| // Unrecognized gRPC status codes should be rethrown as-is | ||
| expect(e).toBe(grpcError); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("restartOrchestration", () => { | ||
| it("should preserve error cause for NOT_FOUND gRPC errors", async () => { | ||
| const grpcError = createGrpcError(grpc.status.NOT_FOUND, "Instance not found"); | ||
|
|
||
| getStub(client).restartInstance = (_req: any, _metadata: any, callback: any) => { | ||
| callback(grpcError, null); | ||
| return {} as grpc.ClientUnaryCall; | ||
| }; | ||
|
|
||
| try { | ||
| await client.restartOrchestration("test-instance"); | ||
| fail("Expected an error to be thrown"); | ||
| } catch (e: unknown) { | ||
| expect(e).toBeInstanceOf(Error); | ||
| const error = e as Error; | ||
| expect(error.message).toContain("test-instance"); | ||
| expect(error.message).toContain("was not found"); | ||
| expect(error.cause).toBe(grpcError); | ||
| } | ||
| }); | ||
|
|
||
| it("should preserve error cause for FAILED_PRECONDITION gRPC errors", async () => { | ||
| const grpcError = createGrpcError( | ||
| grpc.status.FAILED_PRECONDITION, | ||
| "Orchestration is still running", | ||
| ); | ||
|
|
||
| getStub(client).restartInstance = (_req: any, _metadata: any, callback: any) => { | ||
| callback(grpcError, null); | ||
| return {} as grpc.ClientUnaryCall; | ||
| }; | ||
|
|
||
| try { | ||
| await client.restartOrchestration("test-instance"); | ||
| fail("Expected an error to be thrown"); | ||
| } catch (e: unknown) { | ||
| expect(e).toBeInstanceOf(Error); | ||
| const error = e as Error; | ||
| expect(error.message).toContain("test-instance"); | ||
| expect(error.message).toContain("cannot be restarted"); | ||
| expect(error.cause).toBe(grpcError); | ||
| } | ||
| }); | ||
|
|
||
| it("should preserve error cause for CANCELLED gRPC errors", async () => { | ||
| const grpcError = createGrpcError(grpc.status.CANCELLED, "Cancelled"); | ||
|
|
||
| getStub(client).restartInstance = (_req: any, _metadata: any, callback: any) => { | ||
| callback(grpcError, null); | ||
| return {} as grpc.ClientUnaryCall; | ||
| }; | ||
|
|
||
| try { | ||
| await client.restartOrchestration("test-instance"); | ||
| fail("Expected an error to be thrown"); | ||
| } catch (e: unknown) { | ||
| expect(e).toBeInstanceOf(Error); | ||
| const error = e as Error; | ||
| expect(error.message).toContain("was canceled"); | ||
| expect(error.cause).toBe(grpcError); | ||
| } | ||
| }); | ||
|
|
||
| it("should rethrow unrecognized gRPC errors without wrapping", async () => { | ||
| const grpcError = createGrpcError(grpc.status.INTERNAL, "Internal server error"); | ||
|
|
||
| getStub(client).restartInstance = (_req: any, _metadata: any, callback: any) => { | ||
| callback(grpcError, null); | ||
| return {} as grpc.ClientUnaryCall; | ||
| }; | ||
|
|
||
| try { | ||
| await client.restartOrchestration("test-instance"); | ||
| fail("Expected an error to be thrown"); | ||
| } catch (e: unknown) { | ||
| // Unrecognized gRPC status codes should be rethrown as-is | ||
| expect(e).toBe(grpcError); | ||
| } | ||
| }); | ||
|
|
||
| it("should validate instanceId parameter", async () => { | ||
| await expect(client.restartOrchestration("")).rejects.toThrow("instanceId cannot be null or empty"); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this file, cancellation is spelled both as "canceled" (e.g., restartOrchestration/getOrchestrationHistory) and "cancelled" (rewindInstance). Since these are user-facing error messages, please standardize on a single spelling for consistency (and update the corresponding test expectation).