aidantwoods / go-paseto

Platform-Agnostic Security Tokens implementation in Golang.
https://pkg.go.dev/aidanwoods.dev/go-paseto
MIT License
307 stars 17 forks source link

use predefined errors #11

Closed bokunodev closed 1 year ago

bokunodev commented 1 year ago

probably something insignificant and can be done by user. but, having errors that you can assert would be great.

lafriks commented 1 year ago

also would be nice to fix error messages to have first lowercase letter (as per go recommendations)

aidantwoods commented 1 year ago

Thanks for bringing this up! 👍 are you looking specifically for the rules the have assertable error messages? Or would it also be useful to export some of the errors from errors.go?

bokunodev commented 1 year ago

My request is to have sentinel error type, so that we can check if error come from paseto-go, regardless of the message.

type TokenError struct{
  e error
}

func (te *TokenError) Error() string {
  return te.e.Error()
}

func (te *TokenError) Is(e error) bool {
   _,ok:=e.(*TokenError)
  return ok
}

func (te *TokenError) Unwrap() error {
  return te.e
}

To use it

var e error
if errors.Is(e, (*paseto.TokenError) {
  log.Fatal("invalid token")
}
log.Fatal("some other fatal error")

But there is 2 PR still on process. This request will break both of them. So I wait instead of submitting a PR.

aidantwoods commented 1 year ago

No need to wait for those PRs if you'd like to open one, they can always be rebased 🙂

I'll try to find some time to get this out soon

lafriks commented 1 year ago

My use case would probably be to be able to easy recognize such cases:

aidantwoods commented 1 year ago

That makes complete sense. Once I've got something up, can I tag you both to take a look to make sure it's fitting your desired use cases? 🙂

bokunodev commented 1 year ago

Sure

Though, Now, I doubt my request. Such a feature can be implemented in user code. Implementing it in the library means I'm stuck with one lib. But the same thing can be said for named errors.

aidantwoods commented 1 year ago

I've opened https://github.com/aidantwoods/go-paseto/pull/12 to address this 🙂

I'd appreciate a review at your leisure to make sure that this is going to catch things you're interested in. I've intentionally left some things as generic errors (like failing to construct a key), since I'm not sure that use case really warrants a custom type (only one thing can really go wrong when calling into those functions).

lafriks commented 1 year ago

Looks good for my use case

aidantwoods commented 1 year ago

Now available on latest release v1.2.0! 😄