You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

errors_go1_20.go 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //go:build go1.20
  2. // +build go1.20
  3. package jwt
  4. import (
  5. "fmt"
  6. )
  7. // Unwrap implements the multiple error unwrapping for this error type, which is
  8. // possible in Go 1.20.
  9. func (je joinedError) Unwrap() []error {
  10. return je.errs
  11. }
  12. // newError creates a new error message with a detailed error message. The
  13. // message will be prefixed with the contents of the supplied error type.
  14. // Additionally, more errors, that provide more context can be supplied which
  15. // will be appended to the message. This makes use of Go 1.20's possibility to
  16. // include more than one %w formatting directive in [fmt.Errorf].
  17. //
  18. // For example,
  19. //
  20. // newError("no keyfunc was provided", ErrTokenUnverifiable)
  21. //
  22. // will produce the error string
  23. //
  24. // "token is unverifiable: no keyfunc was provided"
  25. func newError(message string, err error, more ...error) error {
  26. var format string
  27. var args []any
  28. if message != "" {
  29. format = "%w: %s"
  30. args = []any{err, message}
  31. } else {
  32. format = "%w"
  33. args = []any{err}
  34. }
  35. for _, e := range more {
  36. format += ": %w"
  37. args = append(args, e)
  38. }
  39. err = fmt.Errorf(format, args...)
  40. return err
  41. }