Golang Consul API pulling out the StatusCode from deeply nested responses?

Is there any way to get the net/http StatusCode from a deeply nested function call?

Explicitly I’m calling…

clusterHealth, err := c.client.Operator().AutopilotServerHealth(&queryOptions)

Something I’d like to do is like…

if err != nil && clusterHealth.StatusCode != 429 {

However at the moment I’m doing:

	if err != nil && !strings.Contains(err.Error(), "Unexpected response code: 429") {

I’m wondering if there’s a better way to do this? I know the API exposes the http client options, I’m just curious if others out there have solved this in a more elegant way than checking if a string matches.

I don’t think there is any way to get those errors yet, but this PR https://github.com/hashicorp/consul/pull/8816#pullrequestreview-538736944 is looking to make some improvements in that area.

It should be possible to convert all of the API calls to use a typed error like StatusError to expose the status code.