Skip to content

fix: keep the API error body in equinix api errors - #92

Open
lbarrosby wants to merge 1 commit into
equinix:mainfrom
lbarrosby:fix/api-error-body
Open

lbarrosby wants to merge 1 commit into
equinix:mainfrom
lbarrosby:fix/api-error-body

Conversation

@lbarrosby

Copy link
Copy Markdown
Contributor

Fixes part 2 of #88. Client.Request returned API error: <status> for any status >= 400 without reading the body, so the error the Equinix APIs send back (error code, message, per-field details) was dropped:

API request failed: API error: 404 Not Found

Now the trimmed body is appended when the response has one; an empty body gives the same message as before:

API request failed: API error: 404 Not Found: [{"errorCode":"EQ-3142102","errorMessage":"Port not found"}]

The other part of #88 (--format validation) is a separate PR touching only cmd/.

Test

TestRequestKeepsErrorBody in internal/api/api_test.go points a Client at an httptest server: a 404 with a JSON error body, and a 502 with no body.

# on main
api_test.go:99: error = API error: 404 Not Found, want "API error: 404 Not Found: [{\"errorCode\":...}]"
# with the fix
$ go test ./... && go vet ./...
ok  	github.com/equinix/cli/cmd
ok  	github.com/equinix/cli/internal/api
ok  	github.com/equinix/cli/internal/casing
ok  	github.com/equinix/cli/internal/naming

Client.Request turned any status >= 400 into an error built from the
status line and never read the body, so the structured error the
Equinix APIs return (error code, message, field details) never reached
the user. Include the trimmed body in the error when there is one.

Refs equinix#88

Signed-off-by: Lucas Barros <lucasbarros.kam@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant