diff --git a/errors.go b/errors.go index 8617bee..6794d80 100644 --- a/errors.go +++ b/errors.go @@ -97,6 +97,10 @@ import ( "io" ) +var ( + delimiter string = ": " +) + // New returns an error with the supplied message. // New also records the stack trace at the point it was called. func New(message string) error { @@ -238,7 +242,7 @@ type withMessage struct { msg string } -func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() } +func (w *withMessage) Error() string { return w.msg + delimiter + w.cause.Error() } func (w *withMessage) Cause() error { return w.cause } func (w *withMessage) Format(s fmt.State, verb rune) { @@ -280,3 +284,7 @@ func Cause(err error) error { } return err } + +func SetDelimiter(d string) { + delimiter = d + " " +} diff --git a/errors_test.go b/errors_test.go index 2089b2f..cbbc2cd 100644 --- a/errors_test.go +++ b/errors_test.go @@ -249,3 +249,30 @@ func TestErrorEquality(t *testing.T) { } } } + +func TestSetDelimiter(t *testing.T) { + tests := []struct { + name string + delimiter string + }{ + {"empty delimiter", + "", + }, + {"single character delimiter", + "|", + }, + {"string delimiter", + "delimiter", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + SetDelimiter(tt.delimiter) + err := Wrap(New("foo"), "foo") + want := "foo" + tt.delimiter + " " + "foo" + if err.Error() != want { + t.Errorf("Error(): got: %s, want %s", err.Error(), want) + } + }) + } +}