Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Added option to change delimiter #208

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -280,3 +284,7 @@ func Cause(err error) error {
}
return err
}

func SetDelimiter(d string) {
delimiter = d + " "
}
27 changes: 27 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}