From 2d813295e6eb2a38dd9d0792f135c37b1dc2d142 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sat, 8 Apr 2017 21:18:13 +0200 Subject: [PATCH] laze init of error messages --- common/errors/errors.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/common/errors/errors.go b/common/errors/errors.go index 9f749d1b..03455d7e 100644 --- a/common/errors/errors.go +++ b/common/errors/errors.go @@ -28,15 +28,23 @@ type hasSeverity interface { // Error is an error object with underlying error. type Error struct { - message string + format string + message []interface{} inner error severity Severity path []string } +func (v *Error) formMessage() string { + if len(v.format) == 0 { + return serial.Concat(v.message) + } + return fmt.Sprintf(v.format, v.message...) +} + // Error implements error.Error(). func (v *Error) Error() string { - msg := v.message + msg := v.formMessage() if v.inner != nil { msg += " > " + v.inner.Error() } @@ -103,13 +111,17 @@ func (v *Error) Path(path ...string) *Error { // New returns a new error object with message formed from given arguments. func New(msg ...interface{}) *Error { return &Error{ - message: serial.Concat(msg...), + message: msg, severity: SeverityInfo, } } func Format(format string, values ...interface{}) *Error { - return New(fmt.Sprintf(format, values...)) + return &Error{ + format: format, + message: values, + severity: SeverityInfo, + } } // Cause returns the root cause of this error.