-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(contact): emit typed error envelopes across the contact domain #1287
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
Merged
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
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
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,78 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package contact | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/larksuite/cli/errs" | ||
| ) | ||
|
|
||
| const contactFanoutRetryHint = "retry the command; if it persists, narrow --queries to a single term to isolate the failing input" | ||
|
|
||
| func contactInvalidResponseError(format string, args ...any) *errs.InternalError { | ||
| return errs.NewInternalError(errs.SubtypeInvalidResponse, format, args...) | ||
| } | ||
|
|
||
| func contactFanoutErrorSummary(err error) string { | ||
| if p, ok := errs.ProblemOf(err); ok { | ||
| if p.Code >= 100 && p.Code < 600 { | ||
| prefix := fmt.Sprintf("HTTP %d:", p.Code) | ||
| body := strings.TrimSpace(strings.TrimPrefix(p.Message, prefix)) | ||
| msg := fmt.Sprintf("HTTP %d %s", p.Code, http.StatusText(p.Code)) | ||
| if body != "" { | ||
| msg = fmt.Sprintf("%s: %s", msg, contactTruncateError(body, 200)) | ||
| } | ||
| return msg | ||
| } | ||
| if p.Code != 0 { | ||
| return fmt.Sprintf("API %d: %s", p.Code, p.Message) | ||
| } | ||
| return p.Message | ||
| } | ||
| return err.Error() | ||
| } | ||
|
|
||
| // contactFanoutAllFailedError builds the top-level error returned when every | ||
| // fanout query fails. It mirrors the representative (first) failure's | ||
| // classification — category, subtype, code, log_id, retryable, hint — so the | ||
| // exit-code classifier still sees the real signal, while carrying the aggregate | ||
| // message. The representative error is copied (never mutated) and kept as the | ||
| // cause, so a single-query problem object is not rewritten into an aggregate one. | ||
| func contactFanoutAllFailedError(err error, msg string) error { | ||
| var ( | ||
| apiErr *errs.APIError | ||
| netErr *errs.NetworkError | ||
| intErr *errs.InternalError | ||
| ) | ||
| switch { | ||
| case errors.As(err, &apiErr): | ||
| c := *apiErr | ||
| c.Message = msg | ||
| c.Cause = err | ||
| return &c | ||
| case errors.As(err, &netErr): | ||
| c := *netErr | ||
| c.Message = msg | ||
| c.Cause = err | ||
| return &c | ||
| case errors.As(err, &intErr): | ||
| c := *intErr | ||
| c.Message = msg | ||
| c.Cause = err | ||
| return &c | ||
| } | ||
| return errs.NewInternalError(errs.SubtypeUnknown, "%s", msg).WithHint(contactFanoutRetryHint).WithCause(err) | ||
| } | ||
|
|
||
| func contactTruncateError(s string, maxRunes int) string { | ||
| r := []rune(s) | ||
| if len(r) <= maxRunes { | ||
| return s | ||
| } | ||
| return string(r[:maxRunes]) + "..." | ||
| } | ||
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,81 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package contact | ||
|
|
||
| import ( | ||
| "errors" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/larksuite/cli/errs" | ||
| ) | ||
|
|
||
| func TestContactFanoutErrorSummary_HTTPStatus(t *testing.T) { | ||
| err := errs.NewNetworkError(errs.SubtypeNetworkServer, `HTTP 503: {"reason":"upstream_unavailable"}`). | ||
| WithCode(503). | ||
| WithRetryable() | ||
|
|
||
| got := contactFanoutErrorSummary(err) | ||
| if !strings.HasPrefix(got, "HTTP 503 Service Unavailable: ") { | ||
| t.Fatalf("summary: got %q", got) | ||
| } | ||
| if !strings.Contains(got, "upstream_unavailable") { | ||
| t.Fatalf("summary should include truncated body details, got %q", got) | ||
| } | ||
| } | ||
|
|
||
| func TestContactInvalidResponseError_TypedInternal(t *testing.T) { | ||
| got := contactInvalidResponseError("decode contact response failed") | ||
| p, ok := errs.ProblemOf(got) | ||
| if !ok { | ||
| t.Fatalf("expected typed problem, got %T", got) | ||
| } | ||
| if p.Category != errs.CategoryInternal || p.Subtype != errs.SubtypeInvalidResponse { | ||
| t.Fatalf("problem type: got %s/%s", p.Category, p.Subtype) | ||
| } | ||
| } | ||
|
|
||
| func TestContactFanoutAllFailedError_PreservesTypedProblem(t *testing.T) { | ||
| err := errs.NewAPIError(errs.SubtypeRateLimit, "rate limit"). | ||
| WithCode(99991663). | ||
| WithLogID("log-contact-1"). | ||
| WithRetryable() | ||
|
|
||
| got := contactFanoutAllFailedError(err, "all 2 queries failed; first: API 99991663: rate limit (query=\"alice\")") | ||
| p, ok := errs.ProblemOf(got) | ||
| if !ok { | ||
| t.Fatalf("expected typed problem, got %T", got) | ||
| } | ||
| if p.Category != errs.CategoryAPI || p.Subtype != errs.SubtypeRateLimit { | ||
| t.Fatalf("problem type: got %s/%s", p.Category, p.Subtype) | ||
| } | ||
| if p.Code != 99991663 || p.LogID != "log-contact-1" || !p.Retryable { | ||
| t.Fatalf("problem metadata not preserved: %+v", p) | ||
| } | ||
| if !strings.Contains(p.Message, "all 2 queries failed") { | ||
| t.Fatalf("problem message not decorated: %q", p.Message) | ||
| } | ||
| // The representative error must not be mutated: it stays a single-query | ||
| // failure, while the aggregate is a distinct value carrying it as cause. | ||
| if err.Message != "rate limit" { | ||
| t.Fatalf("representative error message was mutated: %q", err.Message) | ||
| } | ||
| if !errors.Is(got, err) { | ||
| t.Fatalf("aggregate error should keep the representative failure as its cause") | ||
| } | ||
| } | ||
|
evandance marked this conversation as resolved.
|
||
|
|
||
| func TestContactFanoutAllFailedError_UntypedGetsActionableHint(t *testing.T) { | ||
| got := contactFanoutAllFailedError(nil, "all 2 queries failed; first: internal error (query=\"alice\")") | ||
| p, ok := errs.ProblemOf(got) | ||
| if !ok { | ||
| t.Fatalf("expected typed problem, got %T", got) | ||
| } | ||
| if p.Category != errs.CategoryInternal || p.Subtype != errs.SubtypeUnknown { | ||
| t.Fatalf("problem type: got %s/%s", p.Category, p.Subtype) | ||
| } | ||
| if !strings.Contains(p.Hint, "narrow --queries") { | ||
| t.Fatalf("hint should guide recovery, got %q", p.Hint) | ||
| } | ||
| } | ||
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.