Skip to content

Consistent inner errors for validation errors #135

@oxisto

Description

@oxisto

With PR #125 we can finally unwrap the Inner error of an ValidationError, for example to check for its underlying error, such as expiration or mis-formed token, e.g. using error.Is

However, we are not consistent in setting our inner errors. For example, for an expired map claim we set it to:

vErr.Inner = errors.New("Token is expired")

However, for our RegisteredClaims and StandardClaims we are setting it to:

jwt/claims.go

Line 59 in c435f38

vErr.Inner = fmt.Errorf("token is expired by %v", delta)

In other cases we are actually using constant error strings, such as:

ErrInvalidKey = errors.New("key is invalid")

I would propose to harmonize this, possibly introducing an ErrExpiredClaim as a constant error string in this example to allow something like

token, err := jwt.Parse(...)
if err != nil && errors.Is(err, ErrExpiredClaim) {
  fmt.Printf("Your token is expired")
}

We could also potentially override Is(err) on validation error to check for the error using our flags rather than the error string.

I know that there is already a PR #100 open, but it introduces a lot of other changes and seems to be stale.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions