Skip to content

Remove error code types#2904

Merged
mandre merged 2 commits intogophercloud:masterfrom
sapcc:remove-error-code-types
Apr 17, 2024
Merged

Remove error code types#2904
mandre merged 2 commits intogophercloud:masterfrom
sapcc:remove-error-code-types

Conversation

@majewsky
Copy link
Copy Markdown
Contributor

@majewsky majewsky commented Feb 9, 2024

This is a breaking change intended for the v2.0 release, spun off from the discussion in #2902. I am posting this as a bit of a hot take. If this ends up being a discussion about why the ErrDefaultXXX types and the ErrorContext field are actually useful, and we just end up with actual real documentation on these, that would be a good outcome as well.

Overview and rationale

The ErrDefault series of types (ErrDefault400, ErrDefault401, etc.) serve the apparent purpose of allowing to assert on a specific response code (e.g. to handle 404 responses differently from other 4xx/5xx responses). However, because of their design, they often tend to obscure the real underlying error message in many cases, reporting just something like "Method not allowed" instead of the default error string on ErrUnexpectedResponseCode that explains which request failed and includes the actual error message returned by the OpenStack service. A minimal change to fix that would be to make them all generate the same error string as ErrUnexpectedResponseCode, but there is a much easier solution if we allow the change to be properly breaking.

This commit removes the types, and all of the associated boilerplate, and replaces them with a single small helper function for quickly asserting that an error is an ErrUnexpectedResponseCode with a specific response code. A similar design has been in productive use in my Swift client library for several years. The changes to tests demonstrate how to use this function.

This also gets rid of the undocumented and untested ProviderClient.ErrorContext interface that I have legitimately no idea what it's supposed to be good for, besides maybe allowing users to skip the generation of ErrDefaultXXX-type errors. If that's the only purpose, that would be yet more incentive to just get rid of them.

Finally, as a small (but still breaking) change, this removes some newlines from the error messages generated by ErrUnexpectedResponseCode. Newlines in error messages cause several log aggregators to choke (e.g. I have seen problems with this myself in syslog and fluentd), so we now try a bit harder to avoid them if possible.

Changelog

As far as I can tell, you compile the changelog as part of the release process, so I did not touch it in the PR. Here's a suggested wording for the respective changelog entries if this gets merged:

BREAKING CHANGES:

* The error types for specific response codes (`ErrDefault400`, `ErrDefault401`, etc.) have been removed. All unexpected response codes will now return `ErrUnexpectedResponseCode` instead. For quickly checking whether a request resulted in a specific response code, use the new `ResponseCodeIs` function:
  ```go
  result, err := SomeRequest().Extract()
  // before
  if _, ok := err.(gophercloud.ErrDefault404); ok {
    handleNotFound()
  }
  // after
  if gophercloud.ResponseCodeIs(err, http.StatusNotFound) {
    handleNotFound()
  }
  ```
* The error messages returned by ErrUnexpectedResponseCode now include less newlines than before. If you match on error messages using regexes, please double-check your regexes.

@github-actions github-actions bot added the semver:major Breaking change label Feb 9, 2024
@majewsky majewsky force-pushed the remove-error-code-types branch from 3f93c44 to e3c77f4 Compare February 9, 2024 12:43
@github-actions github-actions bot added semver:major Breaking change and removed semver:major Breaking change labels Feb 9, 2024
@coveralls
Copy link
Copy Markdown

coveralls commented Feb 9, 2024

Coverage Status

coverage: 78.706% (+0.4%) from 78.32%
when pulling c4d6bce on sapcc:remove-error-code-types
into 9aef383 on gophercloud:master.

@pierreprinetti
Copy link
Copy Markdown
Member

I am working on leveraging errors.Unwrap, which should bring the same benefits: #2906

@pierreprinetti
Copy link
Copy Markdown
Member

pierreprinetti commented Feb 26, 2024

I completely agree that (RequestOpts).ContextError must go, since error unwrapping should provide the same capability in a more idiomatic way.

I am finding myself in deeper a rabbit hole than I can handle with #2906. I am abandoning it.

If this change fits your needs, then I'd be inclined to merge your pull request (this one).

@pierreprinetti
Copy link
Copy Markdown
Member

@gophercloud/core what do you think?

@dtantsur
Copy link
Copy Markdown
Contributor

dtantsur commented Mar 6, 2024

We'd have to turn some switch statements into an if-else chain, but I guess we can live with that.

mandre
mandre previously approved these changes Mar 8, 2024
Copy link
Copy Markdown
Contributor

@mandre mandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very much in favor of this change as it makes consuming gophercloud easier.

@pierreprinetti
Copy link
Copy Markdown
Member

@majewsky Can you please rebase? I think we can then merge 🚀

@majewsky
Copy link
Copy Markdown
Contributor Author

rebase done

@pierreprinetti
Copy link
Copy Markdown
Member

hmm what happened to the CI

@pierreprinetti
Copy link
Copy Markdown
Member

pierreprinetti commented Mar 18, 2024

@majewsky something wrong happened on the Github side. When checking out this branch locally, I get the old commits and not the ones you just force-pushed. Do you mind git commit --amend --no-edit && git push --force please?

@majewsky majewsky force-pushed the remove-error-code-types branch from 48f1053 to 7bc5fd0 Compare March 18, 2024 17:24
@github-actions github-actions bot added semver:major Breaking change and removed semver:major Breaking change labels Mar 18, 2024
@majewsky majewsky force-pushed the remove-error-code-types branch from 7bc5fd0 to 576938d Compare April 9, 2024 08:17
@github-actions github-actions bot removed the semver:major Breaking change label Apr 9, 2024
@majewsky
Copy link
Copy Markdown
Contributor Author

majewsky commented Apr 9, 2024

I rebased as requested three weeks ago, but apparently this was not seen (maybe because I did not also comment "done"?). Because new merge conflicts have arisen, I have rebased again.

@github-actions github-actions bot added the semver:major Breaking change label Apr 9, 2024
@majewsky majewsky force-pushed the remove-error-code-types branch from 576938d to 9d56cba Compare April 16, 2024 15:30
@github-actions github-actions bot added semver:major Breaking change and removed semver:major Breaking change labels Apr 16, 2024
@majewsky
Copy link
Copy Markdown
Contributor Author

Rebased onto current master to match 2.0.0-beta.4. Please take another look.

@mandre
Copy link
Copy Markdown
Contributor

mandre commented Apr 16, 2024

There's a failed conflict resolution in internal/acceptance/openstack/identity/v3/federation_test.go.

This is a breaking change intended for the v2.0 release. Rationale:

The ErrDefault series of types (ErrDefault400, ErrDefault401, etc.)
serve the apparent purpose of allowing to assert on a specific response
code (e.g. to handle 404 responses differently from other 4xx/5xx
responses). However, because of their design, they often tend to obscure
the real underlying error message in many cases, reporting just
something like "Method not allowed" instead of the default error string
on ErrUnexpectedResponseCode that explains which request failed and
includes the actual error message returned by the OpenStack service.

This commit removes the types, and all of the associated boilerplate,
and replaces them with a single small helper function for quickly
asserting that an error is an ErrUnexpectedResponseCode with a specific
response code.

This also gets rid of the undocumented and untested
ProviderClient.ErrorContext interface that I have legitimately no idea
what it's supposed to be good for, besides maybe skipping the generation
of ErrDefaultXXX-type errors.
This includes a backwards-incompatible change to the error message
returned by ErrUnexpectedResponseCode. Error messages are not supposed
to contain line breaks because several logging systems do not handle
them well (e.g. syslog and fluentd), so we try at least a little bit to
get rid of them.
@majewsky majewsky force-pushed the remove-error-code-types branch from 9d56cba to c4d6bce Compare April 16, 2024 16:10
@majewsky
Copy link
Copy Markdown
Contributor Author

Whoopsie, that one probably slipped through last week. Fixed now.

@github-actions github-actions bot added semver:major Breaking change and removed semver:major Breaking change labels Apr 16, 2024
@majewsky
Copy link
Copy Markdown
Contributor Author

One error remains in a testcase that I did not touch (TestBGPAgentRUD). The concrete failure does not seem to relate to my changes, so I'll have to leave it to you to decide if this is caused by the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver:major Breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants