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

client: implement interface{ Is(error) bool } on generated errors #1413

Open
kushuh opened this issue Feb 27, 2025 · 0 comments
Open

client: implement interface{ Is(error) bool } on generated errors #1413

kushuh opened this issue Feb 27, 2025 · 0 comments
Labels
enhancement New feature or request openapi-features OpenAPI features support issues

Comments

@kushuh
Copy link

kushuh commented Feb 27, 2025

Problem

The template for generating errors only implements the Error() string interface.

Since the generated error is a struct, errors.Is() cannot be used directly. The only way to check errors is to use the more cumbersome errors.As() alternative.

_, err := client.Method(ctx)

var ogenError *generated.UnexpectedErrorStatusCode

if errors.As(err, &ogenError) {
  if ogenError.StatusCode == http.StatusUnauthorized {
    // ...
  }
}

Proposal

Golang allows errors to implement the interface{ Is(error) bool }, which allows complex errors to be scanned with errors.Is. Something as straightforward as:

// Code generated by ogen.

func (s *UnexpectedErrorStatusCode) Error() string {
  return fmt.Sprintf("code %d: %+v", s.StatusCode, s.Response)
}

func (s *UnexpectedErrorStatusCode) Is(err error) bool {
  var cmp *UnexpectedErrorStatusCode

  if !errors.As(err, &cmp) {
    return false
  }

  if cmp.StatusCode != s.StatusCode {
    return false
  }

  return true
}

Could allow one-line checks like this:

if errors.Is(err, &generated.UnexpectedErrorStatusCode{StatusCode: http.StatusUnauthorized}) {}

Or even allow for nice switch statements:

unauthorizedError := &generated.UnexpectedErrorStatusCode{StatusCode: http.StatusUnauthorized}
forbiddenError := &generated.UnexpectedErrorStatusCode{StatusCode: http.StatusForbidden}

switch {
case errors.Is(err, unauthorizedError):
  // Unauthorized.
case errors.Is(err, forbiddenError):
  // Forbidden.
default:
  // Unhandled.
}

References

This feature would be insanely helpful for integration tests. Also it could improve code quality on applications that use the ogen client.

@kushuh kushuh added enhancement New feature or request openapi-features OpenAPI features support issues labels Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request openapi-features OpenAPI features support issues
Projects
None yet
Development

No branches or pull requests

1 participant