Repository navigation
feat(golang): every failure a guest reports is a Diagnostic that unwraps to its sentinel #1110
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
Merged
coderdan
merged 5 commits into
claude/gracious-einstein-ywrwim-guest-errors
from
claude/gracious-einstein-ywrwim-go-diagnostic
Oct 8, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ae89b1a
feat(golang): every failure a guest reports is a Diagnostic that unwr…
coderdan 6426936
fix(golang): a Diagnostic's text starts with its sentinel's
coderdan eb80fcb
test(golang): a done context still fetches a failure's detail
coderdan bbaf590
docs(golang): how a caller's fake builds a Diagnostic that matches a …
coderdan 58c7802
docs(golang): encrypt's Diagnostic docs say Error puts the sentinel's…
coderdan 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
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,107 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| // wantDiagnostic asserts that err carries a *Diagnostic with code and a | ||
| // message, and returns it. | ||
| func wantDiagnostic(t *testing.T, err error, code string) *Diagnostic { | ||
| t.Helper() | ||
| var d *Diagnostic | ||
| if !errors.As(err, &d) { | ||
| t.Fatalf("%v carries no Diagnostic", err) | ||
| } | ||
| if d.Code != code { | ||
| t.Fatalf("code = %q, want %q (%v)", d.Code, code, err) | ||
| } | ||
| if d.Message == "" { | ||
| t.Fatalf("%s has no message", code) | ||
| } | ||
| return d | ||
| } | ||
|
|
||
| // wantCode is wantDiagnostic for a test that needs only the code. | ||
| func wantCode(t *testing.T, err error, code string) { | ||
| t.Helper() | ||
| _ = wantDiagnostic(t, err, code) | ||
| } | ||
|
|
||
| // Each kind the profile store reports is its sentinel for errors.Is and a | ||
| // Diagnostic for errors.As. The strategies' kinds are asserted beside the | ||
| // tests that provoke them, in strategy_test.go. ErrInvalidFilename is not | ||
| // here: the store refuses every filename the guest would before asking it. | ||
| func TestEachStoreKindCarriesItsDiagnostic(t *testing.T) { | ||
| ctx := context.Background() | ||
| dir, s := profile(t) | ||
| ws, err := s.WorkspaceStore(ctx, wsB) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| cases := []struct { | ||
| name string | ||
| run func() error | ||
| want error | ||
| code string | ||
| }{ | ||
| {"no current workspace", func() error { | ||
| _, err := s.CurrentWorkspace(ctx) | ||
| return err | ||
| }, ErrNoCurrentWorkspace, "stack_profile::no_current_workspace"}, | ||
| {"a workspace with no directory", func() error { | ||
| return s.SetCurrentWorkspace(ctx, "CCCCCCCCCCCCCCCC") | ||
| }, ErrWorkspaceNotFound, "stack_profile::workspace_not_found"}, | ||
| {"a workspace id that is a path", func() error { | ||
| return s.SetCurrentWorkspace(ctx, "../escape") | ||
| }, ErrInvalidWorkspaceID, "stack_profile::invalid_workspace_id"}, | ||
| {"a file that is not there", func() error { | ||
| _, err := ws.Token(ctx) | ||
| return err | ||
| }, ErrNotFound, "stack_profile::not_found"}, | ||
| {"a file that is not JSON", func() error { | ||
| write(t, filepath.Join(dir, "workspaces", wsB, "auth.json"), "{not json") | ||
| _, err := ws.Token(ctx) | ||
| return err | ||
| }, ErrInvalid, "stack_profile::json"}, | ||
| {"a file that is a directory", func() error { | ||
| if err := os.MkdirAll(filepath.Join(dir, "workspaces", wsB, "secretkey.json"), 0o700); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| _, _, err := ws.SecretKey(ctx) | ||
| return err | ||
| }, ErrIO, "stack_profile::io"}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| err := tc.run() | ||
| if !errors.Is(err, tc.want) { | ||
| t.Fatalf("%v, want %v", err, tc.want) | ||
| } | ||
| wantCode(t, err, tc.code) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // A profile file that is not JSON says where: the line and column, never | ||
| // the parser's message, which could quote the file. | ||
| func TestAProfileJSONErrorNamesTheLine(t *testing.T) { | ||
| ctx := context.Background() | ||
| dir, s := profile(t) | ||
| write(t, filepath.Join(dir, "workspaces", wsB, "auth.json"), "{\n \"access_token\": 7\n}") | ||
| ws, err := s.WorkspaceStore(ctx, wsB) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| _, err = ws.Token(ctx) | ||
| d := wantDiagnostic(t, err, "stack_profile::json") | ||
| if d.Fields["line"] != uint64(2) { | ||
| t.Errorf("fields = %v, want line 2", d.Fields) | ||
| } | ||
| if d.Help == "" { | ||
| t.Error("a JSON error gives no help") | ||
| } | ||
| } |
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
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
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.