Skip to content

Commit

Permalink
Adding inner error in the ValidationError type
Browse files Browse the repository at this point in the history
  • Loading branch information
emanoelxavier committed Dec 31, 2015
1 parent 2240de7 commit 517905c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
// The error from Parse if token is not valid
type ValidationError struct {
err string
Inner error // stores the error returned by external dependencies, i.e.: KeyFunc
Errors uint32 // bitfield. see ValidationError... constants
}

Expand Down
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
}
if key, err = keyFunc(token); err != nil {
// keyFunc returned an error
return token, &ValidationError{err: err.Error(), Errors: ValidationErrorUnverifiable}
return token, &ValidationError{err: err.Error(), Errors: ValidationErrorUnverifiable, Inner: err}
}

// Check expiration times
Expand Down
15 changes: 12 additions & 3 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ package jwt_test
import (
"encoding/json"
"fmt"
"github.com/dgrijalva/jwt-go"
"io/ioutil"
"net/http"
"reflect"
"testing"
"time"

"github.com/dgrijalva/jwt-go"
)

var keyFuncError error = fmt.Errorf("error loading key")

var (
jwtTestDefaultKey []byte
defaultKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return jwtTestDefaultKey, nil }
emptyKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return nil, nil }
errorKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return nil, fmt.Errorf("error loading key") }
errorKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return nil, keyFuncError }
nilKeyFunc jwt.Keyfunc = nil
)

Expand Down Expand Up @@ -180,10 +183,16 @@ func TestParser_Parse(t *testing.T) {
if err == nil {
t.Errorf("[%v] Expecting error. Didn't get one.", data.name)
} else {

ve := err.(*jwt.ValidationError)
// compare the bitfield part of the error
if e := err.(*jwt.ValidationError).Errors; e != data.errors {
if e := ve.Errors; e != data.errors {
t.Errorf("[%v] Errors don't match expectation. %v != %v", data.name, e, data.errors)
}

if err.Error() == keyFuncError.Error() && ve.Inner != keyFuncError {
t.Errorf("[%v] Inner error does not match expectation. %v != %v", data.name, ve.Inner, keyFuncError)
}
}
}
if data.valid && token.Signature == "" {
Expand Down

0 comments on commit 517905c

Please sign in to comment.