Skip to content

Commit

Permalink
add const function (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
taxio authored Dec 17, 2024
1 parent 029ce7e commit 19e8486
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions annotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ func WithAttrs(attrs ...Attribute) AnnotatorFunc {
}
}
}

func WithNoStackTrace() AnnotatorFunc {
return func(err *Error) {
err.stack = nil
}
}
4 changes: 4 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func New(message string, annotators ...any) error {
return wrap(err, annotators...)
}

func Const(message string) error {
return New(message, WithNoStackTrace())
}

func Is(err, target error) bool {
return errors.Is(err, target)
}
Expand Down
14 changes: 14 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,17 @@ func TestJoin(t *testing.T) {
})
}
}

func TestConst(t *testing.T) {
err := errors.Const("test")
if err == nil {
t.Fatal("got nil error")
}
if err.Error() != "test" {
t.Errorf("expected test, got %s", err.Error())
}
cErr := mustCast(t, err)
if len(cErr.StackTrace()) != 0 {
t.Errorf("expected no stack trace, got %d", len(cErr.StackTrace()))
}
}

0 comments on commit 19e8486

Please sign in to comment.