errors_go1_20.go

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