xerrors: minimize diffs for implementation CL
- update package names
- remove duplicate copied code
Change-Id: If650247fb68364d0932ad85985bcb076b1d7ec7e
Reviewed-on: https://go-review.googlesource.com/c/158759
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/xerrors/adaptor.go b/xerrors/adaptor.go
index 6be666e..e5db137 100644
--- a/xerrors/adaptor.go
+++ b/xerrors/adaptor.go
@@ -2,104 +2,13 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package fmt
+package xerrors
import (
"bytes"
- "strings"
-
- "golang.org/x/exp/errors"
- "golang.org/x/exp/errors/internal"
+ "fmt"
)
-// 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 errors.Wrapper
-// with an Unwrap method returning it.
-func Errorf(format string, a ...interface{}) error {
- err, wrap := lastError(format, a)
- if err == nil {
- return &noWrapError{Sprintf(format, a...), nil, errors.Caller(1)}
- }
-
- // TODO: this is not entirely correct. The error value could be
- // printed elsewhere in format if it mixes numbered with unnumbered
- // substitutions. With relatively small changes to doPrintf we can
- // have it optionally ignore extra arguments and pass the argument
- // list in its entirety.
- msg := Sprintf(format[:len(format)-len(": %s")], a[:len(a)-1]...)
- frame := errors.Frame{}
- if internal.EnableTrace {
- frame = errors.Caller(1)
- }
- if wrap {
- return &wrapError{msg, err, frame}
- }
- return &noWrapError{msg, err, frame}
-}
-
-func lastError(format string, a []interface{}) (err error, wrap bool) {
- wrap = strings.HasSuffix(format, ": %w")
- if !wrap &&
- !strings.HasSuffix(format, ": %s") &&
- !strings.HasSuffix(format, ": %v") {
- return nil, false
- }
-
- if len(a) == 0 {
- return nil, false
- }
-
- err, ok := a[len(a)-1].(error)
- if !ok {
- return nil, false
- }
-
- return err, wrap
-}
-
-type noWrapError struct {
- msg string
- err error
- frame errors.Frame
-}
-
-func (e *noWrapError) Error() string {
- return Sprint(e)
-}
-
-func (e *noWrapError) FormatError(p errors.Printer) (next error) {
- p.Print(e.msg)
- e.frame.Format(p)
- return e.err
-}
-
-type wrapError struct {
- msg string
- err error
- frame errors.Frame
-}
-
-func (e *wrapError) Error() string {
- return Sprint(e)
-}
-
-func (e *wrapError) FormatError(p errors.Printer) (next error) {
- p.Print(e.msg)
- e.frame.Format(p)
- return e.err
-}
-
-func (e *wrapError) Unwrap() error {
- return e.err
-}
-
func fmtError(p *pp, verb rune, err error) (handled bool) {
var (
sep = " " // separator before next error
@@ -147,9 +56,9 @@
for {
w.fmt.inDetail = false
switch v := err.(type) {
- case errors.Formatter:
- err = v.FormatError((*errPP)(w))
case Formatter:
+ err = v.FormatError((*errPP)(w))
+ case fmt.Formatter:
if w.fmt.plusV {
v.Format((*errPPState)(w), 'v') // indent new lines
} else {
@@ -217,7 +126,7 @@
return len(b), nil
}
-// errPP wraps a pp to implement an errors.Printer.
+// errPP wraps a pp to implement a Printer.
type errPP pp
func (p *errPP) Print(args ...interface{}) {
diff --git a/xerrors/errors.go b/xerrors/errors.go
index 4d8d5ce..61943db 100644
--- a/xerrors/errors.go
+++ b/xerrors/errors.go
@@ -8,7 +8,7 @@
// https://go.googlesource.com/proposal/+/master/design/go2draft.md
//
// This is an EXPERIMENTAL package, and may change in arbitrary ways without notice.
-package errors
+package xerrors
// errorString is a trivial implementation of error.
type errorString struct {
diff --git a/xerrors/errors_test.go b/xerrors/errors_test.go
index 6c0d793..e33179b 100644
--- a/xerrors/errors_test.go
+++ b/xerrors/errors_test.go
@@ -2,40 +2,40 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package errors_test
+package xerrors_test
import (
"fmt"
"testing"
- "golang.org/x/exp/errors"
+ "golang.org/x/exp/xerrors"
)
func TestNewEqual(t *testing.T) {
// Different allocations should not be equal.
- if errors.New("abc") == errors.New("abc") {
+ if xerrors.New("abc") == xerrors.New("abc") {
t.Errorf(`New("abc") == New("abc")`)
}
- if errors.New("abc") == errors.New("xyz") {
+ if xerrors.New("abc") == xerrors.New("xyz") {
t.Errorf(`New("abc") == New("xyz")`)
}
// Same allocation should be equal to itself (not crash).
- err := errors.New("jkl")
+ err := xerrors.New("jkl")
if err != err {
t.Errorf(`err != err`)
}
}
func TestErrorMethod(t *testing.T) {
- err := errors.New("abc")
+ err := xerrors.New("abc")
if err.Error() != "abc" {
t.Errorf(`New("abc").Error() = %q, want %q`, err.Error(), "abc")
}
}
func ExampleNew() {
- err := errors.New("emit macho dwarf: elf header corrupted")
+ err := xerrors.New("emit macho dwarf: elf header corrupted")
if err != nil {
fmt.Print(err)
}
diff --git a/xerrors/example_test.go b/xerrors/example_test.go
index e3b07cf..f07d092 100644
--- a/xerrors/example_test.go
+++ b/xerrors/example_test.go
@@ -2,14 +2,14 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package errors_test
+package xerrors_test
import (
"fmt"
"os"
"time"
- "golang.org/x/exp/errors"
+ "golang.org/x/exp/xerrors"
)
// MyError is an error implementation that includes a time and message.
@@ -40,7 +40,7 @@
_, err := os.Open("non-existing")
if err != nil {
var pathError *os.PathError
- if errors.As(err, &pathError) {
+ if xerrors.As(err, &pathError) {
fmt.Println("Failed at path:", pathError.Path)
}
}
diff --git a/xerrors/fmt.go b/xerrors/fmt.go
index f17ddb1..931c04d 100644
--- a/xerrors/fmt.go
+++ b/xerrors/fmt.go
@@ -2,14 +2,12 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package fmt
+package xerrors
import (
- "bytes"
"strings"
- "golang.org/x/exp/errors"
- "golang.org/x/exp/errors/internal"
+ "golang.org/x/exp/xerrors/internal"
)
// Errorf formats according to a format specifier and returns the string as a
@@ -19,12 +17,12 @@
// 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 errors.Wrapper
+// format string ends with ": %w", the returned error implements Wrapper
// with an Unwrap method returning it.
func Errorf(format string, a ...interface{}) error {
err, wrap := lastError(format, a)
if err == nil {
- return &noWrapError{Sprintf(format, a...), nil, errors.Caller(1)}
+ return &noWrapError{Sprintf(format, a...), nil, Caller(1)}
}
// TODO: this is not entirely correct. The error value could be
@@ -33,9 +31,9 @@
// have it optionally ignore extra arguments and pass the argument
// list in its entirety.
msg := Sprintf(format[:len(format)-len(": %s")], a[:len(a)-1]...)
- frame := errors.Frame{}
+ frame := Frame{}
if internal.EnableTrace {
- frame = errors.Caller(1)
+ frame = Caller(1)
}
if wrap {
return &wrapError{msg, err, frame}
@@ -66,14 +64,14 @@
type noWrapError struct {
msg string
err error
- frame errors.Frame
+ frame Frame
}
func (e *noWrapError) Error() string {
return Sprint(e)
}
-func (e *noWrapError) FormatError(p errors.Printer) (next error) {
+func (e *noWrapError) FormatError(p Printer) (next error) {
p.Print(e.msg)
e.frame.Format(p)
return e.err
@@ -82,14 +80,14 @@
type wrapError struct {
msg string
err error
- frame errors.Frame
+ frame Frame
}
func (e *wrapError) Error() string {
return Sprint(e)
}
-func (e *wrapError) FormatError(p errors.Printer) (next error) {
+func (e *wrapError) FormatError(p Printer) (next error) {
p.Print(e.msg)
e.frame.Format(p)
return e.err
@@ -98,148 +96,3 @@
func (e *wrapError) Unwrap() error {
return e.err
}
-
-func fmtError(p *pp, verb rune, err error) (handled bool) {
- var (
- sep = " " // separator before next error
- w = p // print buffer where error text is written
- )
- switch {
- // Note that this switch must match the preference order
- // for ordinary string printing (%#v before %+v, and so on).
-
- case p.fmt.sharpV:
- if stringer, ok := p.arg.(GoStringer); ok {
- // Print the result of GoString unadorned.
- p.fmt.fmtS(stringer.GoString())
- return true
- }
- return false
-
- case p.fmt.plusV:
- sep = "\n - "
- w.fmt.fmtFlags = fmtFlags{plusV: p.fmt.plusV} // only keep detail flag
-
- // The width or precision of a detailed view could be the number of
- // errors to print from a list.
-
- default:
- // Use an intermediate buffer in the rare cases that precision,
- // truncation, or one of the alternative verbs (q, x, and X) are
- // specified.
- switch verb {
- case 's', 'v':
- if (!w.fmt.widPresent || w.fmt.wid == 0) && !w.fmt.precPresent {
- break
- }
- fallthrough
- case 'q', 'x', 'X':
- w = newPrinter()
- defer w.free()
- default:
- w.badVerb(verb)
- return true
- }
- }
-
-loop:
- for {
- w.fmt.inDetail = false
- switch v := err.(type) {
- case errors.Formatter:
- err = v.FormatError((*errPP)(w))
- case Formatter:
- if w.fmt.plusV {
- v.Format((*errPPState)(w), 'v') // indent new lines
- } else {
- v.Format(w, 'v') // do not indent new lines
- }
- break loop
- default:
- w.fmtString(v.Error(), 's')
- break loop
- }
- if err == nil {
- break
- }
- if !w.fmt.inDetail || !p.fmt.plusV {
- w.buf.WriteByte(':')
- }
- // Strip last newline of detail.
- if bytes.HasSuffix([]byte(w.buf), detailSep) {
- w.buf = w.buf[:len(w.buf)-len(detailSep)]
- }
- w.buf.WriteString(sep)
- w.fmt.inDetail = false
- }
-
- if w != p {
- p.fmtString(string(w.buf), verb)
- }
- return true
-}
-
-var detailSep = []byte("\n ")
-
-// errPPState wraps a pp to implement State with indentation. It is used
-// for errors implementing fmt.Formatter.
-type errPPState pp
-
-func (p *errPPState) Width() (wid int, ok bool) { return (*pp)(p).Width() }
-func (p *errPPState) Precision() (prec int, ok bool) { return (*pp)(p).Precision() }
-func (p *errPPState) Flag(c int) bool { return (*pp)(p).Flag(c) }
-
-func (p *errPPState) Write(b []byte) (n int, err error) {
- if !p.fmt.inDetail || p.fmt.plusV {
- if len(b) == 0 {
- return 0, nil
- }
- if p.fmt.inDetail && p.fmt.needNewline {
- p.fmt.needNewline = false
- p.buf.WriteByte(':')
- p.buf.Write(detailSep)
- if b[0] == '\n' {
- b = b[1:]
- }
- }
- k := 0
- for i, c := range b {
- if c == '\n' {
- p.buf.Write(b[k:i])
- p.buf.Write(detailSep)
- k = i + 1
- }
- }
- p.buf.Write(b[k:])
- p.fmt.needNewline = !p.fmt.inDetail
- }
- return len(b), nil
-}
-
-// errPP wraps a pp to implement an errors.Printer.
-type errPP pp
-
-func (p *errPP) Print(args ...interface{}) {
- if !p.fmt.inDetail || p.fmt.plusV {
- if p.fmt.plusV {
- Fprint((*errPPState)(p), args...)
- } else {
- (*pp)(p).doPrint(args)
- }
- }
-}
-
-func (p *errPP) Printf(format string, args ...interface{}) {
- if !p.fmt.inDetail || p.fmt.plusV {
- if p.fmt.plusV {
- Fprintf((*errPPState)(p), format, args...)
- } else {
- (*pp)(p).doPrintf(format, args)
- }
- }
-}
-
-func (p *errPP) Detail() bool {
- p.fmt.inDetail = true
- return p.fmt.plusV
-}
diff --git a/xerrors/fmt_test.go b/xerrors/fmt_test.go
index 116ddc4..f874103 100644
--- a/xerrors/fmt_test.go
+++ b/xerrors/fmt_test.go
@@ -2,10 +2,10 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package fmt_test
+package xerrors_test
import (
- gofmt "fmt"
+ "fmt"
"io"
"os"
"path"
@@ -15,8 +15,7 @@
"strings"
"testing"
- "golang.org/x/exp/errors"
- "golang.org/x/exp/errors/fmt"
+ "golang.org/x/exp/xerrors"
)
func TestErrorf(t *testing.T) {
@@ -31,39 +30,39 @@
got error
want []string
}{{
- fmt.Errorf("no args"),
+ xerrors.Errorf("no args"),
chain("no args/path.TestErrorf/path.go:xxx"),
}, {
- fmt.Errorf("no args: %s"),
+ xerrors.Errorf("no args: %s"),
chain("no args: %!s(MISSING)/path.TestErrorf/path.go:xxx"),
}, {
- fmt.Errorf("nounwrap: %s", "simple"),
+ xerrors.Errorf("nounwrap: %s", "simple"),
chain(`nounwrap: simple/path.TestErrorf/path.go:xxx`),
}, {
- fmt.Errorf("nounwrap: %v", "simple"),
+ xerrors.Errorf("nounwrap: %v", "simple"),
chain(`nounwrap: simple/path.TestErrorf/path.go:xxx`),
}, {
- fmt.Errorf("%s failed: %v", "foo", chained),
+ xerrors.Errorf("%s failed: %v", "foo", chained),
chain("foo failed/path.TestErrorf/path.go:xxx",
"chained/somefile.go:xxx"),
}, {
- fmt.Errorf("no wrap: %s", chained),
+ xerrors.Errorf("no wrap: %s", chained),
chain("no wrap/path.TestErrorf/path.go:xxx",
"chained/somefile.go:xxx"),
}, {
- fmt.Errorf("%s failed: %w", "foo", chained),
+ xerrors.Errorf("%s failed: %w", "foo", chained),
chain("wraps:foo failed/path.TestErrorf/path.go:xxx",
"chained/somefile.go:xxx"),
}, {
- fmt.Errorf("nowrapv: %v", chained),
+ xerrors.Errorf("nowrapv: %v", chained),
chain("nowrapv/path.TestErrorf/path.go:xxx",
"chained/somefile.go:xxx"),
}, {
- fmt.Errorf("wrapw: %w", chained),
+ xerrors.Errorf("wrapw: %w", chained),
chain("wraps:wrapw/path.TestErrorf/path.go:xxx",
"chained/somefile.go:xxx"),
}, {
- fmt.Errorf("not wrapped: %+v", chained),
+ xerrors.Errorf("not wrapped: %+v", chained),
chain("not wrapped: chained: somefile.go:123/path.TestErrorf/path.go:xxx"),
}}
for i, tc := range testCases {
@@ -95,10 +94,10 @@
fallback = &wrapped{"fallback", os.ErrNotExist}
oldAndNew = &wrapped{"new style", formatError("old style")}
framed = &withFrameAndMore{
- frame: errors.Caller(0),
+ frame: xerrors.Caller(0),
}
opaque = &wrapped{"outer",
- errors.Opaque(&wrapped{"mid",
+ xerrors.Opaque(&wrapped{"mid",
&wrapped{"inner", nil}})}
)
testCases := []struct {
@@ -135,13 +134,13 @@
}, {
err: simple,
fmt: "%#v",
- want: "&fmt_test.wrapped{msg:\"simple\", err:error(nil)}",
+ want: "&xerrors_test.wrapped{msg:\"simple\", err:error(nil)}",
}, {
err: framed,
fmt: "%+v",
want: "something:" +
- "\n golang.org/x/exp/errors/fmt_test.TestErrorFormatter" +
- "\n .+/golang.org/x/exp/errors/fmt/errors_test.go:98" +
+ "\n golang.org/x/exp/xerrors_test.TestErrorFormatter" +
+ "\n .+/golang.org/x/exp/xerrors/fmt_test.go:98" +
"\n something more",
regexp: true,
}, {
@@ -265,20 +264,20 @@
}, {
err: simple,
fmt: "%T",
- want: "*fmt_test.wrapped",
+ want: "*xerrors_test.wrapped",
}, {
err: simple,
fmt: "%🤪",
- want: "%!🤪(*fmt_test.wrapped=&{simple <nil>})",
+ want: "%!🤪(*xerrors_test.wrapped=&{simple <nil>})",
}, {
err: formatError("use fmt.Formatter"),
fmt: "%#v",
want: "use fmt.Formatter",
}, {
- err: wrapped{"using errors.Formatter",
+ err: wrapped{"using xerrors.Formatter",
formatError("use fmt.Formatter")},
fmt: "%#v",
- want: "fmt_test.wrapped{msg:\"using errors.Formatter\", err:\"use fmt.Formatter\"}",
+ want: "xerrors_test.wrapped{msg:\"using xerrors.Formatter\", err:\"use fmt.Formatter\"}",
}, {
err: fmtTwice("%s %s", "ok", panicValue{}),
fmt: "%s",
@@ -345,7 +344,7 @@
}}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d/%s", i, tc.fmt), func(t *testing.T) {
- got := gofmt.Sprintf(tc.fmt, tc.err)
+ got := fmt.Sprintf(tc.fmt, tc.err)
if got != tc.want {
t.Errorf("\n got: %q\nwant: %q", got, tc.want)
}
@@ -353,7 +352,7 @@
}
}
-var _ errors.Formatter = wrapped{}
+var _ xerrors.Formatter = wrapped{}
type wrapped struct {
msg string
@@ -362,20 +361,20 @@
func (e wrapped) Error() string { return "should call Format" }
-func (e wrapped) FormatError(p errors.Printer) (next error) {
+func (e wrapped) FormatError(p xerrors.Printer) (next error) {
p.Print(e.msg)
p.Detail()
p.Print("somefile.go:123")
return e.err
}
-var _ errors.Formatter = detailed{}
+var _ xerrors.Formatter = detailed{}
type detailed struct{}
func (e detailed) Error() string { return fmt.Sprint(e) }
-func (detailed) FormatError(p errors.Printer) (next error) {
+func (detailed) FormatError(p xerrors.Printer) (next error) {
p.Printf("out of %s", "peanuts")
p.Detail()
p.Print("the elephant is on strike\n")
@@ -384,12 +383,12 @@
}
type withFrameAndMore struct {
- frame errors.Frame
+ frame xerrors.Frame
}
func (e *withFrameAndMore) Error() string { return fmt.Sprint(e) }
-func (e *withFrameAndMore) FormatError(p errors.Printer) (next error) {
+func (e *withFrameAndMore) FormatError(p xerrors.Printer) (next error) {
p.Print("something")
if p.Detail() {
e.frame.Format(p)
@@ -406,7 +405,7 @@
panic("should never be called by one of the tests")
}
-func (e spurious) FormatError(p errors.Printer) (next error) {
+func (e spurious) FormatError(p xerrors.Printer) (next error) {
p.Print("spurious")
p.Detail() // Call detail even if we don't print anything
if e == "" {
@@ -425,17 +424,17 @@
func (e adapted) Error() string { return string(e.msg) }
func (e adapted) Format(s fmt.State, verb rune) {
- fmt.FormatError(s, verb, e)
+ xerrors.FormatError(s, verb, e)
}
-func (e adapted) FormatError(p errors.Printer) error {
+func (e adapted) FormatError(p xerrors.Printer) error {
p.Print(e.msg)
p.Detail()
p.Print("detail")
return e.err
}
-// formatError is an error implementing Format instead of errors.Formatter.
+// formatError is an error implementing Format instead of xerrors.Formatter.
// The implementation mimics the implementation of github.com/pkg/errors.
type formatError string
@@ -473,7 +472,7 @@
func (e fmtTwiceErr) Error() string { return fmt.Sprint(e) }
-func (e fmtTwiceErr) FormatError(p errors.Printer) (next error) {
+func (e fmtTwiceErr) FormatError(p xerrors.Printer) (next error) {
p.Printf(e.format, e.args...)
p.Print("/")
p.Printf(e.format, e.args...)
@@ -488,7 +487,7 @@
func (panicValue) String() string { panic("panic") }
-var rePath = regexp.MustCompile(`( [^ ]*)fmt.*test\.`)
+var rePath = regexp.MustCompile(`( [^ ]*)xerrors.*test\.`)
var reLine = regexp.MustCompile(":[0-9]*\n?$")
func cleanPath(s string) string {
@@ -502,10 +501,10 @@
func errToParts(err error) (a []string) {
for err != nil {
var p testPrinter
- if errors.Unwrap(err) != nil {
+ if xerrors.Unwrap(err) != nil {
p.str += "wraps:"
}
- f, ok := err.(errors.Formatter)
+ f, ok := err.(xerrors.Formatter)
if !ok {
a = append(a, err.Error())
break
diff --git a/xerrors/format.go b/xerrors/format.go
index 12deed3..7af63fb 100644
--- a/xerrors/format.go
+++ b/xerrors/format.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package errors
+package xerrors
// A Formatter formats error messages.
type Formatter interface {
diff --git a/xerrors/frame.go b/xerrors/frame.go
index a5369e5..ae7ee9d 100644
--- a/xerrors/frame.go
+++ b/xerrors/frame.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package errors
+package xerrors
import (
"runtime"
diff --git a/xerrors/go.mod b/xerrors/go.mod
index c2e2976..dbf4488 100644
--- a/xerrors/go.mod
+++ b/xerrors/go.mod
@@ -1 +1 @@
-module golang.org/x/exp/errors
+module golang.org/x/exp/xerrors
diff --git a/xerrors/stack_test.go b/xerrors/stack_test.go
index 3d11769..839adcd 100644
--- a/xerrors/stack_test.go
+++ b/xerrors/stack_test.go
@@ -2,17 +2,16 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package errors_test
+package xerrors_test
import (
"bytes"
- fmtcore "fmt"
+ "fmt"
"math/big"
"testing"
- "golang.org/x/exp/errors"
- "golang.org/x/exp/errors/fmt"
- "golang.org/x/exp/errors/internal"
+ "golang.org/x/exp/xerrors"
+ "golang.org/x/exp/xerrors/internal"
)
type myType struct{}
@@ -22,7 +21,7 @@
}
func BenchmarkErrorf(b *testing.B) {
- err := errors.New("foo")
+ err := xerrors.New("foo")
// pi := big.NewFloat(3.14) // Something expensive.
num := big.NewInt(5)
args := func(a ...interface{}) []interface{} { return a }
@@ -40,7 +39,7 @@
b.Run(bc.name, func(b *testing.B) {
b.Run("ExpWithTrace", func(b *testing.B) {
for i := 0; i < b.N; i++ {
- fmt.Errorf(bc.format, bc.args...)
+ xerrors.Errorf(bc.format, bc.args...)
}
})
b.Run("ExpNoTrace", func(b *testing.B) {
@@ -48,12 +47,12 @@
defer func() { internal.EnableTrace = true }()
for i := 0; i < b.N; i++ {
- fmt.Errorf(bc.format, bc.args...)
+ xerrors.Errorf(bc.format, bc.args...)
}
})
b.Run("Core", func(b *testing.B) {
for i := 0; i < b.N; i++ {
- fmtcore.Errorf(bc.format, bc.args...)
+ fmt.Errorf(bc.format, bc.args...)
}
})
})
diff --git a/xerrors/wrap.go b/xerrors/wrap.go
index e26f749..4024b08 100644
--- a/xerrors/wrap.go
+++ b/xerrors/wrap.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package errors
+package xerrors
import (
"reflect"
diff --git a/xerrors/wrap_test.go b/xerrors/wrap_test.go
index 4d0ec54..c132727 100644
--- a/xerrors/wrap_test.go
+++ b/xerrors/wrap_test.go
@@ -2,24 +2,23 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package errors_test
+package xerrors_test
import (
+ "fmt"
"os"
"testing"
- "golang.org/x/exp/errors"
- "golang.org/x/exp/errors/fmt"
+ "golang.org/x/exp/xerrors"
)
func TestIs(t *testing.T) {
- err1 := errors.New("1")
- erra := fmt.Errorf("wrap 2: %w", err1)
- errb := fmt.Errorf("wrap 3: %w", erra)
- erro := errors.Opaque(err1)
- errco := fmt.Errorf("opaque: %w", erro)
-
- err3 := errors.New("3")
+ err1 := xerrors.New("1")
+ erra := xerrors.Errorf("wrap 2: %w", err1)
+ errb := xerrors.Errorf("wrap 3: %w", erra)
+ erro := xerrors.Opaque(err1)
+ errco := xerrors.Errorf("opaque: %w", erro)
+ err3 := xerrors.New("3")
poser := &poser{"either 1 or 3", func(err error) bool {
return err == err1 || err == err3
@@ -50,7 +49,7 @@
}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
- if got := errors.Is(tc.err, tc.target); got != tc.match {
+ if got := xerrors.Is(tc.err, tc.target); got != tc.match {
t.Errorf("Is(%v, %v) = %v, want %v", tc.err, tc.target, got, tc.match)
}
})
@@ -89,7 +88,7 @@
target interface{}
match bool
}{{
- fmt.Errorf("pittied the fool: %w", errorT{}),
+ xerrors.Errorf("pittied the fool: %w", errorT{}),
&errT,
true,
}, {
@@ -97,7 +96,7 @@
&errP,
true,
}, {
- errors.Opaque(errT),
+ xerrors.Opaque(errT),
&errT,
false,
}, {
@@ -128,7 +127,7 @@
for _, tc := range testCases {
name := fmt.Sprintf("As(Errorf(..., %v), %v)", tc.err, tc.target)
t.Run(name, func(t *testing.T) {
- match := errors.As(tc.err, tc.target)
+ match := xerrors.As(tc.err, tc.target)
if match != tc.match {
t.Fatalf("match: got %v; want %v", match, tc.match)
}
@@ -143,9 +142,9 @@
}
func TestUnwrap(t *testing.T) {
- err1 := errors.New("1")
- erra := fmt.Errorf("wrap 2: %w", err1)
- erro := errors.Opaque(err1)
+ err1 := xerrors.New("1")
+ erra := xerrors.Errorf("wrap 2: %w", err1)
+ erro := xerrors.Opaque(err1)
testCases := []struct {
err error
@@ -155,26 +154,26 @@
{wrapped{nil}, nil},
{err1, nil},
{erra, err1},
- {fmt.Errorf("wrap 3: %w", erra), erra},
+ {xerrors.Errorf("wrap 3: %w", erra), erra},
{erro, nil},
- {fmt.Errorf("opaque: %w", erro), erro},
+ {xerrors.Errorf("opaque: %w", erro), erro},
}
for _, tc := range testCases {
- if got := errors.Unwrap(tc.err); got != tc.want {
+ if got := xerrors.Unwrap(tc.err); got != tc.want {
t.Errorf("Unwrap(%v) = %v, want %v", tc.err, got, tc.want)
}
}
}
func TestOpaque(t *testing.T) {
- got := fmt.Errorf("foo: %v", errors.Opaque(errorT{}))
+ got := xerrors.Errorf("foo: %v", xerrors.Opaque(errorT{}))
want := "foo: errorT"
if got.Error() != want {
t.Errorf("error without Format: got %v; want %v", got, want)
}
- got = fmt.Errorf("foo: %v", errors.Opaque(errorD{}))
+ got = xerrors.Errorf("foo: %v", xerrors.Opaque(errorD{}))
want = "foo: errorD"
if got.Error() != want {
t.Errorf("error with Format: got %v; want %v", got, want)
@@ -189,7 +188,7 @@
func (errorD) Error() string { return "errorD" }
-func (errorD) FormatError(p errors.Printer) error {
+func (errorD) FormatError(p xerrors.Printer) error {
p.Print("errorD")
p.Detail()
p.Print("detail")