Skip to content

Commit

Permalink
Return more specific error types
Browse files Browse the repository at this point in the history
  • Loading branch information
aidantwoods committed Dec 31, 2022
1 parent e873409 commit 012c02d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
26 changes: 13 additions & 13 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,32 @@ func errorSeedLength(expected, given int) error {
return fmt.Errorf("seed length incorrect (%d), expected %d", given, expected)
}

func errorMessageParts(given int) error {
return fmt.Errorf("invalid number of message parts in token (%d)", given)
func errorMessageParts(given int) *TokenError {
return &TokenError{fmt.Errorf("invalid number of message parts in token (%d)", given)}
}

func errorMessageHeader(expected Protocol, givenHeader string) error {
return fmt.Errorf("message header `%s' is not valid, expected `%s'", givenHeader, expected.Header())
func errorMessageHeader(expected Protocol, givenHeader string) *TokenError {
return &TokenError{fmt.Errorf("message header `%s' is not valid, expected `%s'", givenHeader, expected.Header())}
}

func errorMessageHeaderDecrypt(expected Protocol, givenHeader string) error {
return fmt.Errorf("cannot decrypt message: %w", errorMessageHeader(expected, givenHeader))
func errorMessageHeaderDecrypt(expected Protocol, givenHeader string) *TokenError {
return errorMessageHeader(expected, givenHeader).wrapWith("cannot decrypt message")
}

func errorMessageHeaderVerify(expected Protocol, givenHeader string) error {
return fmt.Errorf("cannot verify message: %w", errorMessageHeader(expected, givenHeader))
func errorMessageHeaderVerify(expected Protocol, givenHeader string) *TokenError {
return errorMessageHeader(expected, givenHeader).wrapWith("cannot verify message")
}

var unsupportedPasetoVersion = fmt.Errorf("unsupported PASETO version")
var unsupportedPasetoPurpose = fmt.Errorf("unsupported PASETO purpose")
var unsupportedPayload = fmt.Errorf("unsupported payload")

var errorPayloadShort = fmt.Errorf("payload is not long enough to be a valid PASETO message")
var errorBadSignature = fmt.Errorf("bad signature")
var errorBadMAC = fmt.Errorf("bad message authentication code")
var errorPayloadShort = &TokenError{fmt.Errorf("payload is not long enough to be a valid PASETO message")}
var errorBadSignature = &TokenError{fmt.Errorf("bad signature")}
var errorBadMAC = &TokenError{fmt.Errorf("bad message authentication code")}

var errorKeyInvalid = fmt.Errorf("key was not valid")

func errorDecrypt(err error) error {
return fmt.Errorf("the message could not be decrypted: %w", err)
func errorDecrypt(err error) *TokenError {
return (&TokenError{err}).wrapWith("the message could not be decrypted")
}
4 changes: 2 additions & 2 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func newMessage(protocol Protocol, token string) (message, error) {

payloadBytes, err := encoding.Decode(encodedPayload)
if err != nil {
return message{}, err
return message{}, &TokenError{err}
}

footer, err := encoding.Decode(encodedFooter)
if err != nil {
return message{}, err
return message{}, &TokenError{err}
}

payload, err := protocol.newPayload(payloadBytes)
Expand Down
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (p *Parser) AddRule(rule ...Rule) {
func (p Parser) validate(token Token) (*Token, error) {
for _, rule := range p.rules {
if err := rule(token); err != nil {
return nil, err
return nil, &RuleError{err}
}
}

Expand Down

0 comments on commit 012c02d

Please sign in to comment.