diff --git a/common/errors.go b/common/errors.go index 4e2591a9f..4becd0446 100644 --- a/common/errors.go +++ b/common/errors.go @@ -10,16 +10,19 @@ import ( type Error interface { Error() string - Trace(msg string) Error - TraceCause(cause error, msg string) Error + Trace(format string, a ...interface{}) Error + TraceCause(cause error, format string, a ...interface{}) Error Cause() error } -func NewError(msg string) Error { +func NewError(format string, a ...interface{}) Error { + msg := Fmt(format, a...) return newError(msg, nil) + } -func NewErrorWithCause(cause error, msg string) Error { +func NewErrorWithCause(cause error, format string, a ...interface{}) Error { + msg := Fmt(format, a...) return newError(msg, cause) } @@ -39,6 +42,7 @@ type cmnError struct { traces []traceItem } +// NOTE: Do not expose, it's not very friendly. func newError(msg string, cause error) *cmnError { return &cmnError{ msg: msg, @@ -52,13 +56,15 @@ func (err *cmnError) Error() string { } // Add tracing information with msg. -func (err *cmnError) Trace(msg string) Error { +func (err *cmnError) Trace(format string, a ...interface{}) Error { + msg := Fmt(format, a...) return err.doTrace(msg, 2) } // Add tracing information with cause and msg. // If a cause was already set before, it is overwritten. -func (err *cmnError) TraceCause(cause error, msg string) Error { +func (err *cmnError) TraceCause(cause error, format string, a ...interface{}) Error { + msg := Fmt(format, a...) err.cause = cause return err.doTrace(msg, 2) } diff --git a/common/string.go b/common/string.go index a6895eb25..64484921f 100644 --- a/common/string.go +++ b/common/string.go @@ -6,8 +6,14 @@ import ( "strings" ) -// Fmt shorthand, XXX DEPRECATED -var Fmt = fmt.Sprintf +// Like fmt.Sprintf, but skips formatting if args are empty. +var Fmt = func(format string, a ...interface{}) string { + if len(a) == 0 { + return format + } else { + return fmt.Sprintf(format, a...) + } +} // RightPadString adds spaces to the right of a string to make it length totalLength func RightPadString(s string, totalLength int) string {