fix: resolves problems in handling of API client errors #75
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.
Motivation
Primarily meant to resolve #73 but the underlying bug may be causing other unexpected behavior as well.
Problem
The generated client code catches errors and then creates and returns new errors using errors.Errorf(...).
The generated resources and other consumers of the generated clients catch the exceptions returned by the generated client methods and branch control flow based on the result of conditional type assertions w/
client.RequestError
andclient.NotFoundError
which are API response types. Since the errors returned by the generated clients are neverclient.RequestError
orclient.NotFoundError
the conditional type assertions always fail which prevents the expected error handling code from executing.In this case of #73, the logic to handle out-of-bounds resource deletion is there, but is never being executed due to the type assertion failing when we would want it to succeed.
Solution
At a high level this changes updates the code to use wrapped errors so that clients can add additional context to errors before they're returned without preventing consumers from accessing details from the underlying error.
fmt.Errorf
and%w
errors.Is
which supports wrapped errors.Is(error) bool
forclient.NotFoundError
to return true when the HTTP Status Code of a (potentially wrapped)client.RequestError
is 404.errors.Is
required both the message & the status code to match by default.For Reviewers
tools/
, then invokedmake generate
.errors.Errorf
and type assertion on errors that weren't updated by the the generate task and updated them manually.client/client_test.go
which did not need to be modified.make generate
again to ensure that a new diff was not producedmake test
(I did not have a Rootly API key I could use for this so was unable to run tests requiring it)fixes #73