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

httputil : wrap handlers for additional context #413

Merged

Conversation

desimone
Copy link
Contributor

@desimone desimone commented Nov 28, 2019

Summary

  1. We extend standard library's http.HandleFunc to support errors. This improves (imho) request flow to be more explicit and safer. In particular, returning an error and not another request handler func ensures that we don't accidentally forget to return from the parent func after a error request handler is called.

For example

...
		httputil.ErrorResponse(w, r, err)
		return // request will actually fall through if this return is omitted!
	}
fmt.Print("some super duper secret info")
}

vs

...
		return err
	}
fmt.Print("some super duper secret info")
}

In practice, this mostly meant replacing http.HandlerFunc with our own httputil.HandlerFunc and changing our handler function definitions to return an error type.

  1. We create a custom HTTP error type that allows us uniformly handle pomerium http error, and include additional request context where available like original request url (Improve user experience when authenticate callback session expires #383).

Related issues

See also

Screenshots

Screen Shot 2019-11-30 at 2 28 02 AM

Screen Shot 2019-11-30 at 2 27 33 AM

Screen Shot 2019-11-29 at 5 48 25 PM

Checklist:

  • add related issues
  • updated unit tests
  • ready for review

@codecov
Copy link

codecov bot commented Nov 29, 2019

Codecov Report

Merging #413 into master will decrease coverage by 0.2%.
The diff coverage is 94.4%.

@@           Coverage Diff            @@
##           master    #413     +/-   ##
========================================
- Coverage    87.8%   87.6%   -0.3%     
========================================
  Files          52      52             
  Lines        2518    2482     -36     
========================================
- Hits         2212    2175     -37     
- Misses        240     241      +1     
  Partials       66      66
Impacted Files Coverage Δ
internal/httputil/router.go 33.3% <0%> (-66.7%) ⬇️
proxy/proxy.go 96.6% <100%> (ø) ⬆️
proxy/middleware.go 100% <100%> (ø) ⬆️
authenticate/handlers.go 95.1% <100%> (+0.2%) ⬆️
internal/middleware/middleware.go 100% <100%> (ø) ⬆️
internal/httputil/handlers.go 100% <100%> (ø) ⬆️
internal/urlutil/url.go 100% <100%> (ø) ⬆️
proxy/handlers.go 100% <100%> (ø) ⬆️
internal/urlutil/signed.go 93.7% <100%> (ø) ⬆️
internal/httputil/errors.go 87.5% <92.5%> (-3.5%) ⬇️
... and 5 more

@desimone desimone added this to the v0.6.0 milestone Dec 1, 2019
Signed-off-by: Bobby DeSimone <bobbydesimone@gmail.com>
@desimone desimone force-pushed the feature/error-handler-improvements branch from 71badfc to 02c4f59 Compare December 1, 2019 22:04
@desimone desimone marked this pull request as ready for review December 1, 2019 22:04
Copy link
Contributor

@travisgroth travisgroth left a comment

Choose a reason for hiding this comment

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

One comment but LGTM

internal/httputil/router_test.go Outdated Show resolved Hide resolved
@desimone desimone merged commit b3d3159 into pomerium:master Dec 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants