Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolves problems in handling of API client errors #75

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ermul
Copy link

@ermul ermul commented Feb 26, 2025

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 and client.NotFoundError which are API response types. Since the errors returned by the generated clients are never client.RequestError or client.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.

  • Update generated client code to wrap errors with fmt.Errorf and %w
  • Update consumers to use errors.Is which supports wrapped errors.
  • Implemented Is(error) bool for client.NotFoundError to return true when the HTTP Status Code of a (potentially wrapped) client.RequestError is 404.
    • Added this because the errors.Is required both the message & the status code to match by default.

For Reviewers

  • Made changes to the template files under tools/, then invoked make generate.
  • Searched through the codebase for other uses of errors.Errorf and type assertion on errors that weren't updated by the the generate task and updated them manually.
    • One exception being th type assertions on errors in client/client_test.go which did not need to be modified.
  • Committed the changes
  • Ran make generate again to ensure that a new diff was not produced
  • Ran make test (I did not have a Rootly API key I could use for this so was unable to run tests requiring it)

fixes #73

  Motivation
Primarily meant to resolve rootlyhq#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`
and `client.NotFoundError` which are API response types.
Since the errors returned by the generated clients are never
`client.RequestError` or `client.NotFoundError` the conditional type assertions
always fail which prevents the expected error handling code from executing.

In this case of rootlyhq#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.

- Update generated client code to wrap errors with `fmt.Errorf` and `%w`
- Update consumers to use `errors.Is` which supports wrapped errors.
- Implemented `Is(error) bool` for `client.NotFoundError` to return true
  when the HTTP Status Code of a (potentially wrapped) `client.RequestError`
  is 404.

fixes rootlyhq#73
@kwent kwent requested a review from alexmingoia February 26, 2025 22:25
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.

resources deleted out-of-bounds are not recreated
1 participant