Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit 1e412a1

Browse files
committed
Added New and Cause methods.
1 parent 45e9319 commit 1e412a1

File tree

4 files changed

+128
-1
lines changed

4 files changed

+128
-1
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2015,
1+
Copyright (c) 2015, Dave Cheney <[email protected]>
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without

errors.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Package errors implements functions to manipulate errors.
2+
package errors
3+
4+
type errorString string
5+
6+
func (e errorString) Error() string {
7+
return string(e)
8+
}
9+
10+
// New returns an error that formats as the given text.
11+
func New(text string) error {
12+
return errorString(text)
13+
}
14+
15+
// Cause returns the underlying cause of the error, if possible.
16+
// An error value has a cause if it implements the following
17+
// method:
18+
//
19+
// Cause() error
20+
//
21+
//
22+
// If the error does not implement Cause, the original error will
23+
// be returned. If the error is nil, nil will be returned without further
24+
// investigation.
25+
func Cause(err error) error {
26+
if err == nil {
27+
return nil
28+
}
29+
if err, ok := err.(interface {
30+
Cause() error
31+
}); ok {
32+
return err.Cause()
33+
}
34+
return err
35+
}

errors_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package errors
2+
3+
import (
4+
"io"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func TestNew(t *testing.T) {
10+
got := New("test error")
11+
want := errorString("test error")
12+
13+
if !reflect.DeepEqual(got, want) {
14+
t.Errorf("New: got %#v, want %#v", got, want)
15+
}
16+
}
17+
18+
func TestNewError(t *testing.T) {
19+
tests := []struct {
20+
err string
21+
want error
22+
}{
23+
{"", errorString("")},
24+
{"foo", errorString("foo")},
25+
{"foo", New("foo")},
26+
}
27+
28+
for _, tt := range tests {
29+
got := New(tt.err)
30+
if got.Error() != tt.want.Error() {
31+
t.Errorf("New.Error(): got: %q, want %q", got, tt.want)
32+
}
33+
}
34+
}
35+
36+
type nilError struct{}
37+
38+
func (nilError) Error() string { return "nil error" }
39+
40+
type causeError struct {
41+
err error
42+
}
43+
44+
func (e *causeError) Error() string { return "cause error" }
45+
func (e *causeError) Cause() error { return e.err }
46+
47+
func TestCause(t *testing.T) {
48+
tests := []struct {
49+
err error
50+
want error
51+
}{{
52+
// nil error is nil
53+
err: nil,
54+
want: nil,
55+
}, {
56+
// explicit nil error is nil
57+
err: (error)(nil),
58+
want: nil,
59+
}, {
60+
// typed nil is nil
61+
err: (*nilError)(nil),
62+
want: (*nilError)(nil),
63+
}, {
64+
// uncaused error is unaffected
65+
err: io.EOF,
66+
want: io.EOF,
67+
}, {
68+
// caused error returns cause
69+
err: &causeError{err: io.EOF},
70+
want: io.EOF,
71+
}}
72+
73+
for i, tt := range tests {
74+
got := Cause(tt.err)
75+
if !reflect.DeepEqual(got, tt.want) {
76+
t.Errorf("test %d: got %#v, want %#v", i+1, got, tt.want)
77+
}
78+
}
79+
}

example_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package errors_test
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
func ExampleNew() {
9+
err := errors.New("whoops")
10+
fmt.Println(err.Error())
11+
12+
// Output: whoops
13+
}

0 commit comments

Comments
 (0)