equinix-labs / metal-go

[Deprecated] Golang client for Equinix Metal
https://deploy.equinix.com/labs/equinix-sdk-go/
MIT License
3 stars 2 forks source link

feat: include API messages in errors #169

Closed ctreatma closed 1 year ago

ctreatma commented 1 year ago

This updates the formatErrorMessage helper so that, if the API responded with an Error model, the messages from that Error model are appended to the status code & status description. This makes it easier for users to get visibility into why an API call failed.

I chose to leave the existing code to support rfc7807 messages in place, just in case we ever migrate to that standard.

Fixes #168

displague commented 1 year ago

In 0.14 of metal-cli (with packngo):

$ metal  device create  -O goo -m goo --hostname goo --plan goo
Error: Could not create Device: POST https://api.equinix.com/metal/v1/projects/{uuid}/devices: 422 goo is not a valid metro, goo is not a valid operating system, goo is not a valid plan, You are not allowed to provision goo servers 

With this patch:

$ go run ./cmd/metal/ device create  -O goo -m goo --hostname goo --plan goo
Error: could not create Device: 422 Unprocessable Entity goo is not a valid metro, goo is not a valid operating system, goo is not a valid plan, You are not allowed to provision goo servers
exit status 1

Without this patch:

$ metal  device create  -O goo -m goo --hostname goo --plan goo
Error: could not create Device: 422 Unprocessable Entity
displague commented 1 year ago

nit: The older versions included {VERB} {URL}: {CODE} as a prefix. I'm not saying that is better, but perhaps a : prefix would help to separate the HTTP status from the error messages.

Error: could not create Device: 422 Unprocessable: Entity goo is not a valid metro, goo is not a valid operating system, goo is not a valid plan, You are not allowed to provision goo servers

github-actions[bot] commented 1 year ago

This PR is included in version 0.26.0 :tada:

displague commented 1 year ago

Noticed this in a Metal CLI build based on this version of metal-go:

404 Not Found Not found