From 012c02d6928db7818e1b5098caacaeec82653e5d Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sat, 31 Dec 2022 12:24:22 +0000 Subject: [PATCH] Return more specific error types --- errors.go | 26 +++++++++++++------------- message.go | 4 ++-- parser.go | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/errors.go b/errors.go index 22dc479..a93f449 100644 --- a/errors.go +++ b/errors.go @@ -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") } diff --git a/message.go b/message.go index 8219baa..c0fe61c 100644 --- a/message.go +++ b/message.go @@ -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) diff --git a/parser.go b/parser.go index f4b9e32..95ea197 100644 --- a/parser.go +++ b/parser.go @@ -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} } }