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

Improved request errors #286

Merged
merged 13 commits into from
Oct 23, 2019
Merged

Conversation

biotom
Copy link
Contributor

@biotom biotom commented Oct 17, 2019

Description

Errors returned in the file "Requests.go" were wrapped with useful error messages, and defer statements were added to http requests. Some extra error handling was added to "Requests_test.go".

Motivation and Context

Requests.go returns unwrapped errors (often from common 3rd party libraries), so adding wrapping to those errors might provide more context and make troubleshooting easier. Additionally,
deferred calls to Close() were added after http requests to prevent resource leakage. Require.Noerror statements were added to requests_test.go to handle unhandled errors.

How Has This Been Tested?

Existing unit tests passed, and the code shouldn't cause changes to public behaviour. Error messages are wrapped to provide context, but the testing suite does not rely on literal matching of error messages.

Checklist:

  • My change requires a change to the documentation or CHANGELOG.
  • I have updated the documentation/CHANGELOG accordingly.
  • I have created a feature (non-master) branch for my PR.

@biotom biotom requested a review from a team October 17, 2019 20:16
JoelSpeed
JoelSpeed previously approved these changes Oct 17, 2019
Copy link
Member

@JoelSpeed JoelSpeed left a comment

Choose a reason for hiding this comment

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

LGTM, would you mind adding a note to the changelog and then we can get that merged 👍

Copy link
Contributor

@loshz loshz left a comment

Choose a reason for hiding this comment

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

Couple of formatting changes.

pkg/requests/requests.go Outdated Show resolved Hide resolved
pkg/requests/requests_test.go Outdated Show resolved Hide resolved
pkg/requests/requests.go Outdated Show resolved Hide resolved
@loshz
Copy link
Contributor

loshz commented Oct 17, 2019

While I agree with making these errors more verbose is a good idea, what advantage do we get over just doing:

fmt.Errorf("something bad happened: %v", err)

Am I right in thinking the callers of these functions would then need to parse the "new" errors to check for Cause()?

@loshz
Copy link
Contributor

loshz commented Oct 17, 2019

We could also be super fancy and start to implement some of the new go1.13 error functions https://blog.golang.org/go1.13-errors

Co-Authored-By: Dan Bond <pm@danbond.io>
@biotom
Copy link
Contributor Author

biotom commented Oct 21, 2019

While I agree with making these errors more verbose is a good idea, what advantage do we get over just doing:

fmt.Errorf("something bad happened: %v", err)

Am I right in thinking the callers of these functions would then need to parse the "new" errors to check for Cause()?

For me, the big advantage of Wrap is that you're automatically putting together the same sort of useful error string that you'd get with Errorf, but you can easily string a bunch of them together, making it easier to work out what went wrong for debugging. You also get a stack trace from the point at which Wrap was called. This extra context is useful when you're dealing with errors from 3rd party libraries, which might return a generic and unhelpful error message. You'd want to call Cause() further up the error chain if you want to do something based on what the original error was- but in the spirit of using opaque errors, and handling them based on behaviour, I haven't used it very often.

I know error handling in Go is a bit of a can of worms, so I could be wrong about all this...

@biotom
Copy link
Contributor Author

biotom commented Oct 21, 2019

Apologies- I'm not really sure what I've broken here. Travis is failing with "go: golang.org/x/oauth2@v0.0.0-20190604053449-0f29369cfe45: unrecognized import path "golang.org/x/oauth2" (https fetch: Get https://golang.org/x/oauth2?go-get=1: dial tcp 74.125.69.141:443: i/o timeout)", but go mod tidy adds it to go.sum again when it is removed.

@loshz
Copy link
Contributor

loshz commented Oct 21, 2019

so I could be wrong about all this...

You are definitely not wrong! I think this PR and your feedback is extremely useful.

My only "concern" is that http://github.com/pkg/errors was just a stop gap until we had better error handling in the Go std lib. For example, their roadmap says they won't cut anymore releases after v1.0

1.0. Final release.

As of Go 1.13, there is a new Unwrap() func in the std lib errors package. Here is a playground with an example.

My thoughts are, we could start implementing this new functionality instead of importing a new 3rd party package. WDYT @biotom @JoelSpeed?

@biotom
Copy link
Contributor Author

biotom commented Oct 21, 2019

My thoughts are, we could start implementing this new functionality instead of importing a new 3rd party package. WDYT @biotom @JoelSpeed?

I think this makes a lot of sense! It looks to me like the new error handling is mostly the best wrapping bits from the errors library at any rate- I'd be happy to look into it, if people feel like that's a good idea.

@loshz
Copy link
Contributor

loshz commented Oct 21, 2019

I'd be happy to implement this new functionality with you if you want to work on it together? I can create an issue which outlines our intentions and a list of changes we need to make.

@biotom
Copy link
Contributor Author

biotom commented Oct 22, 2019

I'd be happy to implement this new functionality with you if you want to work on it together? I can create an issue which outlines our intentions and a list of changed we need to make.

Sounds great!

@JoelSpeed
Copy link
Member

As of Go 1.13, there is a new Unwrap() func in the std lib errors package. Here is a playground with an example.

Out of interest, if this is in 1.13, can we not just use 1.13? 🤔

@loshz
Copy link
Contributor

loshz commented Oct 22, 2019

Out of interest, if this is in 1.13, can we not just use 1.13? 🤔

@JoelSpeed I'm not sure what you mean by this?

There is now a new %w verb that can be passed to fmt.Errorf which automatically wraps errors, so we would need to implement this.

https://golang.org/src/fmt/errors.go

@biotom biotom force-pushed the improved_request_errors branch from cec0e87 to 5f3f14d Compare October 22, 2019 18:15
Copy link
Contributor

@loshz loshz left a comment

Choose a reason for hiding this comment

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

Couple of small nitpicks.

We will also need to drop support for Go 1.12 in .travis.yml. Removing this line will do the trick.

pkg/requests/requests.go Outdated Show resolved Hide resolved
pkg/requests/requests.go Outdated Show resolved Hide resolved
pkg/requests/requests_test.go Outdated Show resolved Hide resolved
pkg/requests/requests_test.go Outdated Show resolved Hide resolved
@biotom biotom force-pushed the improved_request_errors branch from 6e3c111 to 95b958e Compare October 23, 2019 09:35
@biotom biotom force-pushed the improved_request_errors branch from bc74d89 to 1c418fe Compare October 23, 2019 09:49
Copy link
Contributor

@loshz loshz left a comment

Choose a reason for hiding this comment

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

Thank you for you patience and input on this PR! Congrats on your first contribution 🎉

@loshz loshz merged commit 35f2ae9 into oauth2-proxy:master Oct 23, 2019
@biotom biotom deleted the improved_request_errors branch October 23, 2019 17:09
@biotom
Copy link
Contributor Author

biotom commented Oct 23, 2019

Thank you for you patience and input on this PR! Congrats on your first contribution 🎉

Thanks! And thanks for all the support with my first open source contribution! I'd be happy to help check out error handling in the rest of the code, if you'd still be interested.

@loshz
Copy link
Contributor

loshz commented Oct 23, 2019

@biotom I plan on creating an issue for a general refactor of errors as mentioned above. I'll add you as an assignee and you can help out whenever suits you best 😄

Also, we have an #oauth2_proxy channel in Gophers Slack if you'd like to get involved in other conversations.

Jing-ze pushed a commit to Jing-ze/oauth2-proxy that referenced this pull request Nov 19, 2024
Signed-off-by: charlie <qianglin98@qq.com>
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.

3 participants