package xerrors
Import Path
golang.org/x/xerrors (on go.dev)
Dependency Relation
imports 11 packages, and imported by 5 packages
Involved Source Files
adaptor.go
Package xerrors implements functions to manipulate errors.
This package is based on the Go 2 proposal for error values:
https://go.dev/design/29934-error-values
These functions were incorporated into the standard library's errors package
in Go 1.13:
- Is
- As
- Unwrap
Also, Errorf's %w verb was incorporated into fmt.Errorf.
No other features of this package were included in Go 1.13, and at present
there are no plans to include any of them.
errors.go
fmt.go
format.go
frame.go
wrap.go
Code Examples
package main
import (
"fmt"
"time"
)
// MyError is an error implementation that includes a time and message.
type MyError struct {
When time.Time
What string
}
func (e MyError) Error() string {
return fmt.Sprintf("%v: %v", e.When, e.What)
}
func oops() error {
return MyError{
time.Date(1989, 3, 15, 22, 30, 0, 0, time.UTC),
"the file system has gone away",
}
}
func main() {
if err := oops(); err != nil {
fmt.Println(err)
}
}
package main
import (
"fmt"
"os"
"golang.org/x/xerrors"
)
func main() {
_, err := os.Open("non-existing")
if err != nil {
var pathError *os.PathError
if xerrors.As(err, &pathError) {
fmt.Println("Failed at path:", pathError.Path)
}
}
}
package main
import (
"fmt"
"golang.org/x/xerrors"
)
type MyError2 struct {
Message string
frame xerrors.Frame
}
func (m *MyError2) Error() string {
return m.Message
}
func (m *MyError2) Format(f fmt.State, c rune) { // implements fmt.Formatter
xerrors.FormatError(m, f, c)
}
func (m *MyError2) FormatError(p xerrors.Printer) error { // implements xerrors.Formatter
p.Print(m.Message)
if p.Detail() {
m.frame.Format(p)
}
return nil
}
func main() {
err := &MyError2{Message: "oops", frame: xerrors.Caller(1)}
fmt.Printf("%v\n", err)
fmt.Println()
fmt.Printf("%+v\n", err)
}
Package-Level Type Names (total 4)
A Formatter formats error messages.
( Formatter) Error() builtin.string
FormatError prints the receiver's first error and returns the next error in
the error chain, if any.
Formatter : error
func FormatError(f Formatter, s fmt.State, verb rune)
A Frame contains part of a call stack.
Format prints the stack as error detail.
It should be called from an error's Format implementation
after printing any other error detail.
func Caller(skip int) Frame
A Printer formats error messages.
The most common implementation of Printer is the one provided by package fmt
during Printf (as of Go 1.13). Localization packages such as golang.org/x/text/message
typically provide their own implementations.
Detail reports whether error detail is requested.
After the first call to Detail, all text written to the Printer
is formatted as additional detail, or ignored when
detail has not been requested.
If Detail returns false, the caller can avoid printing the detail at all.
Print appends args to the message output.
Printf writes a formatted string.
Printer : github.com/go-sql-driver/mysql.Logger
Printer : go.uber.org/fx.Printer
Printer : gorm.io/gorm/logger.Writer
func Formatter.FormatError(p Printer) (next error)
func Frame.Format(p Printer)
A Wrapper provides context around another error.
Unwrap returns the next error in the error chain.
If there is no next error, Unwrap returns nil.
*golang.org/x/crypto/ssh.BannerError
*crypto/tls.CertificateVerificationError
crypto/x509.SystemRootsError
*encoding/csv.ParseError
*encoding/json.MarshalerError
*github.com/cenkalti/backoff/v5.PermanentError
github.com/decred/dcrd/dcrec/secp256k1/v4.Error
github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa.Error
*github.com/dop251/goja.Exception
*github.com/dop251/goja.InterruptedError
*github.com/dop251/goja.StackOverflowError
github.com/failsafe-go/failsafe-go/retrypolicy.ExceededError
*github.com/go-viper/mapstructure/v2.DecodeError
*github.com/goccy/go-json/internal/errors.MarshalerError
*github.com/hibiken/asynq/internal/errors.Error
*github.com/hibiken/asynq/internal/errors.RedisCommandError
github.com/ipfs/go-cid.ErrInvalidCid
*github.com/libp2p/go-libp2p/p2p/host/resource-manager.ErrMemoryLimitExceeded
*github.com/libp2p/go-libp2p/p2p/host/resource-manager.ErrStreamOrConnLimitExceeded
*github.com/libp2p/go-libp2p/p2p/net/swarm.TransportError
github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client.ReservationError
*github.com/miekg/dns.ParseError
*github.com/pion/dtls/v2/pkg/protocol.FatalError
*github.com/pion/dtls/v2/pkg/protocol.HandshakeError
*github.com/pion/dtls/v2/pkg/protocol.InternalError
*github.com/pion/dtls/v2/pkg/protocol.TemporaryError
*github.com/pion/dtls/v2/pkg/protocol.TimeoutError
*github.com/pion/dtls/v3/pkg/protocol.FatalError
*github.com/pion/dtls/v3/pkg/protocol.HandshakeError
*github.com/pion/dtls/v3/pkg/protocol.InternalError
*github.com/pion/dtls/v3/pkg/protocol.TemporaryError
*github.com/pion/dtls/v3/pkg/protocol.TimeoutError
*github.com/pion/webrtc/v4/pkg/rtcerr.InvalidAccessError
*github.com/pion/webrtc/v4/pkg/rtcerr.InvalidModificationError
*github.com/pion/webrtc/v4/pkg/rtcerr.InvalidStateError
*github.com/pion/webrtc/v4/pkg/rtcerr.NotReadableError
*github.com/pion/webrtc/v4/pkg/rtcerr.NotSupportedError
*github.com/pion/webrtc/v4/pkg/rtcerr.OperationError
*github.com/pion/webrtc/v4/pkg/rtcerr.RangeError
*github.com/pion/webrtc/v4/pkg/rtcerr.SyntaxError
*github.com/pion/webrtc/v4/pkg/rtcerr.TypeError
*github.com/pion/webrtc/v4/pkg/rtcerr.UnknownError
*github.com/quic-go/quic-go/internal/qerr.ApplicationError
*github.com/quic-go/quic-go/internal/qerr.HandshakeTimeoutError
*github.com/quic-go/quic-go/internal/qerr.IdleTimeoutError
*github.com/quic-go/quic-go/internal/qerr.StatelessResetError
*github.com/quic-go/quic-go/internal/qerr.VersionNegotiationError
github.com/redis/go-redis/v9/internal/pool.BadConnError
github.com/reeflective/console.Err
github.com/reeflective/console.ExecutionError
github.com/reeflective/console.LineHookError
github.com/reeflective/console.ParseError
github.com/reeflective/console.PreReadError
*github.com/reeflective/readline/inputrc.ParseError
github.com/tetratelabs/wazero/experimental/sys.Errno
google.golang.org/grpc/internal/transport.ConnectionError
*io/fs.PathError
*net.DNSConfigError
*net.DNSError
*net.OpError
*net/url.Error
*os.LinkError
*os.SyscallError
*os/exec.Error
*strconv.NumError
text/template.ExecError
Package-Level Functions (total 8)
As finds the first error in err's tree that matches target, and if one is found,
sets target to that error value and returns true. Otherwise, it returns false.
The tree consists of err itself, followed by the errors obtained by repeatedly
calling its Unwrap() error or Unwrap() []error method. When err wraps multiple
errors, As examines err followed by a depth-first traversal of its children.
An error matches target if the error's concrete value is assignable to the value
pointed to by target, or if the error has a method As(any) bool such that
As(target) returns true. In the latter case, the As method is responsible for
setting target.
An error type might provide an As method so it can be treated as if it were a
different error type.
As panics if target is not a non-nil pointer to either a type that implements
error, or to any interface type.
Deprecated: As of Go 1.13, this function simply calls [errors.As].
Caller returns a Frame that describes a frame on the caller's stack.
The argument skip is the number of frames to skip over.
Caller(0) returns the frame for the caller of Caller.
Errorf formats according to a format specifier and returns the string as a
value that satisfies error.
The returned error includes the file and line number of the caller when
formatted with additional detail enabled. If the last argument is an error
the returned error's Format method will return it if the format string ends
with ": %s", ": %v", or ": %w". If the last argument is an error and the
format string ends with ": %w", the returned error implements an Unwrap
method returning it.
If the format specifier includes a %w verb with an error operand in a
position other than at the end, the returned error will still implement an
Unwrap method returning the operand, but the error's Format method will not
return the wrapped error.
It is invalid to include more than one %w verb or to supply it with an
operand that does not implement the error interface. The %w verb is otherwise
a synonym for %v.
Note that as of Go 1.13, the fmt.Errorf function will do error formatting,
but it will not capture a stack backtrace.
FormatError calls the FormatError method of f with an errors.Printer
configured according to s and verb, and writes the result to s.
Is reports whether any error in err's tree matches target.
The tree consists of err itself, followed by the errors obtained by repeatedly
calling its Unwrap() error or Unwrap() []error method. When err wraps multiple
errors, Is examines err followed by a depth-first traversal of its children.
An error is considered to match a target if it is equal to that target or if
it implements a method Is(error) bool such that Is(target) returns true.
An error type might provide an Is method so it can be treated as equivalent
to an existing error. For example, if MyError defines
func (m MyError) Is(target error) bool { return target == fs.ErrExist }
then Is(MyError{}, fs.ErrExist) returns true. See [syscall.Errno.Is] for
an example in the standard library. An Is method should only shallowly
compare err and the target and not call [Unwrap] on either.
Deprecated: As of Go 1.13, this function simply calls [errors.Is].
New returns an error that formats as the given text.
The returned error contains a Frame set to the caller's location and
implements Formatter to show this information when printed with details.
Opaque returns an error with the same error formatting as err
but that does not match err and cannot be unwrapped.
Unwrap returns the result of calling the Unwrap method on err, if err implements
Unwrap. Otherwise, Unwrap returns nil.
Unwrap only calls a method of the form "Unwrap() error".
In particular Unwrap does not unwrap errors returned by [errors.Join].
Deprecated: As of Go 1.13, this function simply calls [errors.Unwrap].
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |