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_go_other.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //go:build !go1.20
  2. // +build !go1.20
  3. package jwt
  4. import (
  5. "errors"
  6. "fmt"
  7. )
  8. // Is implements checking for multiple errors using [errors.Is], since multiple
  9. // error unwrapping is not possible in versions less than Go 1.20.
  10. func (je joinedError) Is(err error) bool {
  11. for _, e := range je.errs {
  12. if errors.Is(e, err) {
  13. return true
  14. }
  15. }
  16. return false
  17. }
  18. // wrappedErrors is a workaround for wrapping multiple errors in environments
  19. // where Go 1.20 is not available. It basically uses the already implemented
  20. // functionality of joinedError to handle multiple errors with supplies a
  21. // custom error message that is identical to the one we produce in Go 1.20 using
  22. // multiple %w directives.
  23. type wrappedErrors struct {
  24. msg string
  25. joinedError
  26. }
  27. // Error returns the stored error string
  28. func (we wrappedErrors) Error() string {
  29. return we.msg
  30. }
  31. // newError creates a new error message with a detailed error message. The
  32. // message will be prefixed with the contents of the supplied error type.
  33. // Additionally, more errors, that provide more context can be supplied which
  34. // will be appended to the message. Since we cannot use of Go 1.20's possibility
  35. // to include more than one %w formatting directive in [fmt.Errorf], we have to
  36. // emulate that.
  37. //
  38. // For example,
  39. //
  40. // newError("no keyfunc was provided", ErrTokenUnverifiable)
  41. //
  42. // will produce the error string
  43. //
  44. // "token is unverifiable: no keyfunc was provided"
  45. func newError(message string, err error, more ...error) error {
  46. // We cannot wrap multiple errors here with %w, so we have to be a little
  47. // bit creative. Basically, we are using %s instead of %w to produce the
  48. // same error message and then throw the result into a custom error struct.
  49. var format string
  50. var args []any
  51. if message != "" {
  52. format = "%s: %s"
  53. args = []any{err, message}
  54. } else {
  55. format = "%s"
  56. args = []any{err}
  57. }
  58. errs := []error{err}
  59. for _, e := range more {
  60. format += ": %s"
  61. args = append(args, e)
  62. errs = append(errs, e)
  63. }
  64. err = &wrappedErrors{
  65. msg: fmt.Sprintf(format, args...),
  66. joinedError: joinedError{errs: errs},
  67. }
  68. return err
  69. }