Remove New, replace with Wrap.
diff --git a/errors.go b/errors.go
index 18a9e68..4d0d8fb 100644
--- a/errors.go
+++ b/errors.go
@@ -1,24 +1,35 @@
// Package errors implements functions to manipulate errors.
package errors
-import "fmt"
-
-// New returns an error that formats as the given text.
-func New(text string) error {
- return Errorf(text)
+type e struct {
+ cause error
+ message string
}
-// Errorf returns a formatted error.
-func Errorf(format string, args ...interface{}) error {
- return fmt.Errorf(format, args...)
+func (e *e) Error() string {
+ return e.message + ": " + e.cause.Error()
+}
+
+func (e *e) Cause() error {
+ return e.cause
+}
+
+// Wrap returns an error annotating the cause with message.
+// If cause is nil, Wrap returns nil.
+func Wrap(cause error, message string) error {
+ if cause == nil {
+ return nil
+ }
+ return &e{cause: cause, message: message}
}
// Cause returns the underlying cause of the error, if possible.
// An error value has a cause if it implements the following
-// method:
+// interface:
//
-// Cause() error
-//
+// type Causer interface {
+// Cause() error
+// }
//
// If the error does not implement Cause, the original error will
// be returned. If the error is nil, nil will be returned without further
diff --git a/errors_test.go b/errors_test.go
index 7c094c4..6b6321e 100644
--- a/errors_test.go
+++ b/errors_test.go
@@ -1,39 +1,11 @@
package errors
import (
- "fmt"
"io"
"reflect"
"testing"
)
-func TestNew(t *testing.T) {
- got := New("test error")
- want := fmt.Errorf("test error")
-
- if !reflect.DeepEqual(got, want) {
- t.Errorf("New: got %#v, want %#v", got, want)
- }
-}
-
-func TestNewError(t *testing.T) {
- tests := []struct {
- err string
- want error
- }{
- {"", fmt.Errorf("")},
- {"foo", fmt.Errorf("foo")},
- {"foo", New("foo")},
- }
-
- for _, tt := range tests {
- got := New(tt.err)
- if got.Error() != tt.want.Error() {
- t.Errorf("New.Error(): got: %q, want %q", got, tt.want)
- }
- }
-}
-
type nilError struct{}
func (nilError) Error() string { return "nil error" }