Skip to content

Commit

Permalink
Pass Facebook errors through to facebookHandler failure func
Browse files Browse the repository at this point in the history
* Format and include Facebook errors for facebookHandler's failure
handler. Previously, facebookService.Me() errors were swallowed by
validateReponse returning a generic ErrUnableToGetFacebookUser
  • Loading branch information
dghubble committed Jul 29, 2018
1 parent 7ed3a47 commit 9737459
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
3 changes: 2 additions & 1 deletion facebook/login.go
Expand Up @@ -2,6 +2,7 @@ package facebook

import (
"errors"
"fmt"
"net/http"

"github.com/dghubble/gologin"
Expand Down Expand Up @@ -76,7 +77,7 @@ func facebookHandler(config *oauth2.Config, success, failure http.Handler) http.
// http.Response, or error are unexpected. Returns nil if they are valid.
func validateResponse(user *User, resp *http.Response, err error) error {
if err != nil || resp.StatusCode != http.StatusOK {
return ErrUnableToGetFacebookUser
return fmt.Errorf("facebook: unable to get Facebook user: %v", err)
}
if user == nil || user.ID == "" {
return ErrUnableToGetFacebookUser
Expand Down
8 changes: 3 additions & 5 deletions facebook/login_test.go
Expand Up @@ -81,9 +81,7 @@ func TestFacebookHandler_ErrorGettingUser(t *testing.T) {
failure := func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
err := gologin.ErrorFromContext(ctx)
if assert.NotNil(t, err) {
assert.Equal(t, ErrUnableToGetFacebookUser, err)
}
assert.Error(t, err)
fmt.Fprintf(w, "failure handler called")
}

Expand All @@ -102,7 +100,7 @@ func TestValidateResponse(t *testing.T) {
validResponse := &http.Response{StatusCode: 200}
invalidResponse := &http.Response{StatusCode: 500}
assert.Equal(t, nil, validateResponse(validUser, validResponse, nil))
assert.Equal(t, ErrUnableToGetFacebookUser, validateResponse(validUser, validResponse, fmt.Errorf("Server error")))
assert.Equal(t, ErrUnableToGetFacebookUser, validateResponse(validUser, invalidResponse, nil))
assert.Error(t, validateResponse(validUser, validResponse, fmt.Errorf("Server error")))
assert.Error(t, validateResponse(validUser, invalidResponse, nil))
assert.Equal(t, ErrUnableToGetFacebookUser, validateResponse(&User{}, validResponse, nil))
}

0 comments on commit 9737459

Please sign in to comment.