Skip to content

Commit

Permalink
Remove Message interface (pkg#39)
Browse files Browse the repository at this point in the history
Replace Message with a Format method on cause. This simplifies Fprint as
well as removing one interface method from Wrap/Wrapf error impls.
  • Loading branch information
davecheney committed Jun 8, 2016
1 parent d146efd commit 2af433a
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,25 @@ func Errorf(format string, args ...interface{}) error {
}

type cause struct {
cause error
message string
cause error
msg string
}

func (c cause) Error() string { return c.Message() + ": " + c.Cause().Error() }
func (c cause) Cause() error { return c.cause }
func (c cause) Message() string { return c.message }
func (c cause) Error() string { return fmt.Sprintf("%v", c) }
func (c cause) Cause() error { return c.cause }

func (c cause) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
io.WriteString(s, c.msg)
return
}
fallthrough
case 's':
fmt.Fprintf(s, "%s: %v", c.msg, c.Cause())
}
}

// Wrap returns an error annotating err with message.
// If err is nil, Wrap returns nil.
Expand All @@ -102,8 +114,8 @@ func Wrap(err error, message string) error {
*stack
}{
cause{
cause: err,
message: message,
cause: err,
msg: message,
},
callers(),
}
Expand All @@ -120,8 +132,8 @@ func Wrapf(err error, format string, args ...interface{}) error {
*stack
}{
cause{
cause: err,
message: fmt.Sprintf(format, args...),
cause: err,
msg: fmt.Sprintf(format, args...),
},
callers(),
}
Expand Down Expand Up @@ -170,9 +182,6 @@ func Fprint(w io.Writer, err error) {
type stacktrace interface {
Stacktrace() []Frame
}
type message interface {
Message() string
}

for err != nil {
switch err := err.(type) {
Expand All @@ -182,12 +191,7 @@ func Fprint(w io.Writer, err error) {
default:
// de nada
}
switch err := err.(type) {
case message:
fmt.Fprintln(w, err.Message())
default:
fmt.Fprintln(w, err.Error())
}
fmt.Fprintf(w, "%+v\n", err)

cause, ok := err.(causer)
if !ok {
Expand Down

0 comments on commit 2af433a

Please sign in to comment.