From 517905c5bd1c1bcb8b7aba66a8fa259cb3134365 Mon Sep 17 00:00:00 2001 From: Emanoel Xavier Date: Thu, 31 Dec 2015 07:48:39 -0800 Subject: [PATCH] Adding inner error in the ValidationError type --- errors.go | 1 + parser.go | 2 +- parser_test.go | 15 ++++++++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/errors.go b/errors.go index e9e788f..eb30a93 100644 --- a/errors.go +++ b/errors.go @@ -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 } diff --git a/parser.go b/parser.go index 3fc27bf..ff751b3 100644 --- a/parser.go +++ b/parser.go @@ -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 diff --git a/parser_test.go b/parser_test.go index 9115017..4cf9751 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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 ) @@ -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 == "" {