MCPcopy
hub / github.com/golang-jwt/jwt / ExampleParse_errorChecking

Function ExampleParse_errorChecking

example_test.go:165–189  ·  view source on GitHub ↗

An example of parsing the error types using errors.Is.

()

Source from the content-addressed store, hash-verified

163
164// An example of parsing the error types using errors.Is.
165func ExampleParse_errorChecking() {
166 // Token from another example. This token is expired
167 var tokenString = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJleHAiOjE1MDAwLCJpc3MiOiJ0ZXN0In0.HE7fK0xOQwFEr4WDgRWj4teRPZ6i3GLwD5YCm6Pwu_c"
168
169 token, err := jwt.Parse(tokenString, func(token *jwt.Token) (any, error) {
170 return []byte("AllYourBase"), nil
171 })
172
173 switch {
174 case token.Valid:
175 fmt.Println("You look nice today")
176 case errors.Is(err, jwt.ErrTokenMalformed):
177 fmt.Println("That's not even a token")
178 case errors.Is(err, jwt.ErrTokenSignatureInvalid):
179 // Invalid signature
180 fmt.Println("Invalid signature")
181 case errors.Is(err, jwt.ErrTokenExpired) || errors.Is(err, jwt.ErrTokenNotValidYet):
182 // Token is either expired or not active yet
183 fmt.Println("Timing is everything")
184 default:
185 fmt.Println("Couldn't handle this token:", err)
186 }
187
188 // Output: Timing is everything
189}

Callers

nothing calls this directly

Calls 1

ParseMethod · 0.80

Tested by

no test coverage detected