This repository was archived by the owner on Dec 1, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +15
-12
lines changed
Expand file tree Collapse file tree 2 files changed +15
-12
lines changed Original file line number Diff line number Diff line change 11// Package errors implements functions to manipulate errors.
22package errors
33
4- type errorString string
5-
6- func (e errorString ) Error () string {
7- return string (e )
8- }
4+ import "fmt"
95
106// New returns an error that formats as the given text.
117func New (text string ) error {
12- return errorString (text )
8+ return Errorf (text )
9+ }
10+
11+ // Errorf returns a formatted error.
12+ func Errorf (format string , args ... interface {}) error {
13+ return fmt .Errorf (format , args ... )
1314}
1415
1516// Cause returns the underlying cause of the error, if possible.
@@ -26,9 +27,10 @@ func Cause(err error) error {
2627 if err == nil {
2728 return nil
2829 }
29- if err , ok := err .( interface {
30+ type causer interface {
3031 Cause () error
31- }); ok {
32+ }
33+ if err , ok := err .(causer ); ok {
3234 return err .Cause ()
3335 }
3436 return err
Original file line number Diff line number Diff line change 11package errors
22
33import (
4+ "fmt"
45 "io"
56 "reflect"
67 "testing"
78)
89
910func TestNew (t * testing.T ) {
1011 got := New ("test error" )
11- want := errorString ("test error" )
12+ want := fmt . Errorf ("test error" )
1213
1314 if ! reflect .DeepEqual (got , want ) {
1415 t .Errorf ("New: got %#v, want %#v" , got , want )
@@ -20,8 +21,8 @@ func TestNewError(t *testing.T) {
2021 err string
2122 want error
2223 }{
23- {"" , errorString ("" )},
24- {"foo" , errorString ("foo" )},
24+ {"" , fmt . Errorf ("" )},
25+ {"foo" , fmt . Errorf ("foo" )},
2526 {"foo" , New ("foo" )},
2627 }
2728
@@ -42,7 +43,7 @@ type causeError struct {
4243}
4344
4445func (e * causeError ) Error () string { return "cause error" }
45- func (e * causeError ) Cause () error { return e .cause }
46+ func (e * causeError ) Cause () error { return e .cause }
4647
4748func TestCause (t * testing.T ) {
4849 tests := []struct {
You can’t perform that action at this time.
0 commit comments