Browse Source

Add efficient implementation of fmt and use for errors.

pull/1780/head
Jae Kwon 7 years ago
parent
commit
99437a96fb
2 changed files with 20 additions and 8 deletions
  1. +12
    -6
      common/errors.go
  2. +8
    -2
      common/string.go

+ 12
- 6
common/errors.go View File

@ -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)
}


+ 8
- 2
common/string.go View File

@ -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 {


Loading…
Cancel
Save