-
Notifications
You must be signed in to change notification settings - Fork 17
fix(api): surface backend error body in upload failures #250
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
+124
−6
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package util | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| ) | ||
|
|
||
| // FormatHTTPError reads a non-2xx HTTP response and returns an error that | ||
| // preserves whatever the server actually said. | ||
| // | ||
| // The Zeabur API returns JSON bodies of the form | ||
| // | ||
| // {"code": "REQUIRE_PAID_PLAN", "error": "Upload size ... exceeds ..."} | ||
| // | ||
| // for client-visible failures. When that shape is present, the message and | ||
| // (if available) code are surfaced. Otherwise the raw body is included so | ||
| // the user still has something to act on instead of a bare status code. | ||
| // | ||
| // action is a short verb phrase describing what failed, e.g. "create upload session". | ||
| func FormatHTTPError(action string, resp *http.Response) error { | ||
| body, _ := io.ReadAll(resp.Body) | ||
|
|
||
| var errResp struct { | ||
| Code string `json:"code"` | ||
| Error string `json:"error"` | ||
| } | ||
| if err := json.Unmarshal(body, &errResp); err == nil && errResp.Error != "" { | ||
| if errResp.Code != "" { | ||
| return fmt.Errorf("%s: %s (code: %s, status: %d)", action, errResp.Error, errResp.Code, resp.StatusCode) | ||
| } | ||
| return fmt.Errorf("%s: %s (status: %d)", action, errResp.Error, resp.StatusCode) | ||
| } | ||
|
|
||
| if len(body) > 0 { | ||
| return fmt.Errorf("%s: status code %d, body: %s", action, resp.StatusCode, string(body)) | ||
| } | ||
| return fmt.Errorf("%s: status code %d", action, resp.StatusCode) | ||
|
canyugs marked this conversation as resolved.
|
||
| } | ||
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,76 @@ | ||
| package util_test | ||
|
|
||
| import ( | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/zeabur/cli/pkg/util" | ||
| ) | ||
|
|
||
| func TestFormatHTTPError(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| statusCode int | ||
| body string | ||
| wantSubstr []string | ||
| }{ | ||
| { | ||
| name: "structured error with code (REQUIRE_PAID_PLAN)", | ||
| statusCode: 400, | ||
| body: `{"code":"REQUIRE_PAID_PLAN","error":"Upload size 53048320 bytes exceeds your plan's limit of 52428800 bytes.","limit":52428800,"content_length":53048320}`, | ||
| wantSubstr: []string{ | ||
| "do thing", | ||
| "Upload size 53048320 bytes exceeds", | ||
| "code: REQUIRE_PAID_PLAN", | ||
| "status: 400", | ||
| }, | ||
| }, | ||
| { | ||
| name: "structured error without code", | ||
| statusCode: 403, | ||
| body: `{"error":"forbidden"}`, | ||
| wantSubstr: []string{"do thing", "forbidden", "status: 403"}, | ||
| }, | ||
| { | ||
| name: "non-JSON body falls back to raw body", | ||
| statusCode: 502, | ||
| body: "<html>Bad Gateway</html>", | ||
| wantSubstr: []string{"do thing", "status code 502", "<html>Bad Gateway</html>"}, | ||
| }, | ||
| { | ||
| name: "empty body falls back to status code only", | ||
| statusCode: 504, | ||
| body: "", | ||
| wantSubstr: []string{"do thing", "status code 504"}, | ||
| }, | ||
| { | ||
| name: "JSON without error field falls back to raw body", | ||
| statusCode: 400, | ||
| body: `{"foo":"bar"}`, | ||
| wantSubstr: []string{"status code 400", `{"foo":"bar"}`}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| tc := tc | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| resp := &http.Response{ | ||
| StatusCode: tc.statusCode, | ||
| Body: io.NopCloser(strings.NewReader(tc.body)), | ||
| } | ||
|
|
||
| err := util.FormatHTTPError("do thing", resp) | ||
| assert.Error(t, err) | ||
| for _, s := range tc.wantSubstr { | ||
| assert.Contains(t, err.Error(), s) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.