2016-07-22 18:15:38 +00:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
2011-12-03 02:17:34 +00:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package errors implements functions to manipulate errors.
|
2019-09-12 23:22:53 +00:00
|
|
|
//
|
|
|
|
// The New function creates errors whose only content is a text message.
|
|
|
|
//
|
|
|
|
// The Unwrap, Is and As functions work on errors that may wrap other errors.
|
|
|
|
// An error wraps another error if its type has the method
|
|
|
|
//
|
|
|
|
// Unwrap() error
|
|
|
|
//
|
|
|
|
// If e.Unwrap() returns a non-nil error w, then we say that e wraps w.
|
|
|
|
//
|
2020-01-02 15:05:27 -08:00
|
|
|
// Unwrap unpacks wrapped errors. If its argument's type has an
|
|
|
|
// Unwrap method, it calls the method once. Otherwise, it returns nil.
|
|
|
|
//
|
2019-09-12 23:22:53 +00:00
|
|
|
// A simple way to create wrapped errors is to call fmt.Errorf and apply the %w verb
|
|
|
|
// to the error argument:
|
|
|
|
//
|
2020-01-02 15:05:27 -08:00
|
|
|
// errors.Unwrap(fmt.Errorf("... %w ...", ..., err, ...))
|
2019-09-12 23:22:53 +00:00
|
|
|
//
|
|
|
|
// returns err.
|
|
|
|
//
|
|
|
|
// Is unwraps its first argument sequentially looking for an error that matches the
|
|
|
|
// second. It reports whether it finds a match. It should be used in preference to
|
|
|
|
// simple equality checks:
|
|
|
|
//
|
2020-12-23 09:57:37 -08:00
|
|
|
// if errors.Is(err, fs.ErrExist)
|
2019-09-12 23:22:53 +00:00
|
|
|
//
|
|
|
|
// is preferable to
|
|
|
|
//
|
2020-12-23 09:57:37 -08:00
|
|
|
// if err == fs.ErrExist
|
2019-09-12 23:22:53 +00:00
|
|
|
//
|
2020-12-23 09:57:37 -08:00
|
|
|
// because the former will succeed if err wraps fs.ErrExist.
|
2019-09-12 23:22:53 +00:00
|
|
|
//
|
|
|
|
// As unwraps its first argument sequentially looking for an error that can be
|
|
|
|
// assigned to its second argument, which must be a pointer. If it succeeds, it
|
|
|
|
// performs the assignment and returns true. Otherwise, it returns false. The form
|
|
|
|
//
|
2020-12-23 09:57:37 -08:00
|
|
|
// var perr *fs.PathError
|
2019-09-12 23:22:53 +00:00
|
|
|
// if errors.As(err, &perr) {
|
|
|
|
// fmt.Println(perr.Path)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// is preferable to
|
|
|
|
//
|
2020-12-23 09:57:37 -08:00
|
|
|
// if perr, ok := err.(*fs.PathError); ok {
|
2019-09-12 23:22:53 +00:00
|
|
|
// fmt.Println(perr.Path)
|
|
|
|
// }
|
|
|
|
//
|
2020-12-23 09:57:37 -08:00
|
|
|
// because the former will succeed if err wraps an *fs.PathError.
|
2011-12-03 02:17:34 +00:00
|
|
|
package errors
|
|
|
|
|
|
|
|
// New returns an error that formats as the given text.
|
2019-09-12 23:22:53 +00:00
|
|
|
// Each call to New returns a distinct error value even if the text is identical.
|
2011-12-03 02:17:34 +00:00
|
|
|
func New(text string) error {
|
|
|
|
return &errorString{text}
|
|
|
|
}
|
|
|
|
|
|
|
|
// errorString is a trivial implementation of error.
|
|
|
|
type errorString struct {
|
|
|
|
s string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *errorString) Error() string {
|
|
|
|
return e.s
|
|
|
|
}
|