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

Commit 2af433a

Browse files
committed
Remove Message interface (#39)
Replace Message with a Format method on cause. This simplifies Fprint as well as removing one interface method from Wrap/Wrapf error impls.
1 parent d146efd commit 2af433a

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

errors.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,25 @@ func Errorf(format string, args ...interface{}) error {
8383
}
8484

8585
type cause struct {
86-
cause error
87-
message string
86+
cause error
87+
msg string
8888
}
8989

90-
func (c cause) Error() string { return c.Message() + ": " + c.Cause().Error() }
91-
func (c cause) Cause() error { return c.cause }
92-
func (c cause) Message() string { return c.message }
90+
func (c cause) Error() string { return fmt.Sprintf("%v", c) }
91+
func (c cause) Cause() error { return c.cause }
92+
93+
func (c cause) Format(s fmt.State, verb rune) {
94+
switch verb {
95+
case 'v':
96+
if s.Flag('+') {
97+
io.WriteString(s, c.msg)
98+
return
99+
}
100+
fallthrough
101+
case 's':
102+
fmt.Fprintf(s, "%s: %v", c.msg, c.Cause())
103+
}
104+
}
93105

94106
// Wrap returns an error annotating err with message.
95107
// If err is nil, Wrap returns nil.
@@ -102,8 +114,8 @@ func Wrap(err error, message string) error {
102114
*stack
103115
}{
104116
cause{
105-
cause: err,
106-
message: message,
117+
cause: err,
118+
msg: message,
107119
},
108120
callers(),
109121
}
@@ -120,8 +132,8 @@ func Wrapf(err error, format string, args ...interface{}) error {
120132
*stack
121133
}{
122134
cause{
123-
cause: err,
124-
message: fmt.Sprintf(format, args...),
135+
cause: err,
136+
msg: fmt.Sprintf(format, args...),
125137
},
126138
callers(),
127139
}
@@ -170,9 +182,6 @@ func Fprint(w io.Writer, err error) {
170182
type stacktrace interface {
171183
Stacktrace() []Frame
172184
}
173-
type message interface {
174-
Message() string
175-
}
176185

177186
for err != nil {
178187
switch err := err.(type) {
@@ -182,12 +191,7 @@ func Fprint(w io.Writer, err error) {
182191
default:
183192
// de nada
184193
}
185-
switch err := err.(type) {
186-
case message:
187-
fmt.Fprintln(w, err.Message())
188-
default:
189-
fmt.Fprintln(w, err.Error())
190-
}
194+
fmt.Fprintf(w, "%+v\n", err)
191195

192196
cause, ok := err.(causer)
193197
if !ok {

0 commit comments

Comments
 (0)