Skip to content

Implement errors.Unwrap() on unexpected status code errors#2639

Merged
mandre merged 1 commit intogophercloud:masterfrom
shiftstack:expose_response_body
Jun 6, 2023
Merged

Implement errors.Unwrap() on unexpected status code errors#2639
mandre merged 1 commit intogophercloud:masterfrom
shiftstack:expose_response_body

Conversation

@pierreprinetti
Copy link
Copy Markdown
Member

With this patch, we add a method with signature Unwrap() error to all errors signaling an unexpected status code. All of them contain a value of type gophercloud.ErrUnexpectedStatusCode that exposes, among other useful values, the body of the response sent by the server.

To access the response body of a request that resulted in an unexpected response code, try to unwrap the returned error to gophercloud.ErrUnexpectedStatusCode:

pages, err := containers.List(client, nil).AllPages()
if err != nil {
	var responseCodeError gophercloud.ErrUnexpectedResponseCode
	if errors.As(err, &responseCodeError) {
		log.Printf("unexpected response code. Response body:\n---\n%s\n---", responseCodeError.Body)
	} else {
		log.Printf("unknown error")
	}
}

Fixes #2605

With this patch, we add a method with signature `Unwrap() error` to all
errors signaling an unexpected status code. All of them contain a value
of type `gophercloud.ErrUnexpectedStatusCode` that exposes, among other
useful values, the body of the response sent by the server.

To access the response body of a request that resulted in an unexpected
response code, try to unwrap the returned error to
`gophercloud.ErrUnexpectedStatusCode`:

```Go
pages, err := containers.List(client, nil).AllPages()
if err != nil {
	var responseCodeError gophercloud.ErrUnexpectedResponseCode
	if errors.As(err, &responseCodeError) {
		log.Printf("unexpected response code. Response body:\n---\n%s\n---", responseCodeError.Body)
	} else {
		log.Printf("unknown error")
	}
}
```
@coveralls
Copy link
Copy Markdown

Coverage Status

coverage: 79.039% (-0.07%) from 79.108% when pulling 86ed770 on shiftstack:expose_response_body into 6e1423b on gophercloud:master.

@pierreprinetti pierreprinetti requested review from EmilienM and mandre June 5, 2023 09:34
@mandre mandre merged commit 0df6b3f into gophercloud:master Jun 6, 2023
@mandre mandre deleted the expose_response_body branch June 6, 2023 13:12
@mandre mandre added this to the v1.5.0 milestone Jun 6, 2023
majewsky added a commit to sapcc/limes that referenced this pull request Jun 21, 2023
This affords simplification of our error unpacking by using
<gophercloud/gophercloud#2639>.
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.

Add convenient access to error's original body

3 participants