From 105e86fc3b42f63dab09c57776e8951b0cedebcd Mon Sep 17 00:00:00 2001 From: Dave Cheney Date: Sun, 27 Dec 2015 16:12:56 +0100 Subject: [PATCH] fixing layout to make Andy happy --- errors.go | 18 ++++++++++-------- errors_test.go | 9 +++++---- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/errors.go b/errors.go index 1c3c713..18a9e68 100644 --- a/errors.go +++ b/errors.go @@ -1,15 +1,16 @@ // Package errors implements functions to manipulate errors. package errors -type errorString string - -func (e errorString) Error() string { - return string(e) -} +import "fmt" // New returns an error that formats as the given text. func New(text string) error { - return errorString(text) + return Errorf(text) +} + +// Errorf returns a formatted error. +func Errorf(format string, args ...interface{}) error { + return fmt.Errorf(format, args...) } // Cause returns the underlying cause of the error, if possible. @@ -26,9 +27,10 @@ func Cause(err error) error { if err == nil { return nil } - if err, ok := err.(interface { + type causer interface { Cause() error - }); ok { + } + if err, ok := err.(causer); ok { return err.Cause() } return err diff --git a/errors_test.go b/errors_test.go index e024b5d..7c094c4 100644 --- a/errors_test.go +++ b/errors_test.go @@ -1,6 +1,7 @@ package errors import ( + "fmt" "io" "reflect" "testing" @@ -8,7 +9,7 @@ import ( func TestNew(t *testing.T) { got := New("test error") - want := errorString("test error") + want := fmt.Errorf("test error") if !reflect.DeepEqual(got, want) { t.Errorf("New: got %#v, want %#v", got, want) @@ -20,8 +21,8 @@ func TestNewError(t *testing.T) { err string want error }{ - {"", errorString("")}, - {"foo", errorString("foo")}, + {"", fmt.Errorf("")}, + {"foo", fmt.Errorf("foo")}, {"foo", New("foo")}, } @@ -42,7 +43,7 @@ type causeError struct { } func (e *causeError) Error() string { return "cause error" } -func (e *causeError) Cause() error { return e.cause } +func (e *causeError) Cause() error { return e.cause } func TestCause(t *testing.T) { tests := []struct {