diff --git a/annotator.go b/annotator.go index 2a2651d..a711d6c 100644 --- a/annotator.go +++ b/annotator.go @@ -35,3 +35,9 @@ func WithAttrs(attrs ...Attribute) AnnotatorFunc { } } } + +func WithNoStackTrace() AnnotatorFunc { + return func(err *Error) { + err.stack = nil + } +} diff --git a/error.go b/error.go index 980968e..f6d5367 100644 --- a/error.go +++ b/error.go @@ -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) } diff --git a/errors_test.go b/errors_test.go index ce3c2a3..4b103c7 100644 --- a/errors_test.go +++ b/errors_test.go @@ -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())) + } +}