From 46c93951d0d5823aa11eae4c9f3db7a531345857 Mon Sep 17 00:00:00 2001 From: Yanhu007 Date: Wed, 15 Apr 2026 03:36:36 +0800 Subject: [PATCH] fix: return nil from Decorate and EnhanceStackTrace when err is nil Decorate(nil, ...) previously created a synthetic.foreign error with no useful type information. This is surprising and makes it impossible to use Decorate in error-forwarding patterns like: return errorx.Decorate(maybeNilErr, "context") Now both Decorate and EnhanceStackTrace return nil when given a nil error, consistent with the Go convention that wrapping nil returns nil. Fixes #40 --- wrap.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/wrap.go b/wrap.go index eb929db..5d1097e 100644 --- a/wrap.go +++ b/wrap.go @@ -19,6 +19,9 @@ var ( // Without args, leaves the provided message intact, so a message may be generated or provided externally. // With args, a formatting is performed, and it is therefore expected a format string to be constant. func Decorate(err error, message string, args ...interface{}) *Error { + if err == nil { + return nil + } return NewErrorBuilder(transparentWrapper). WithConditionallyFormattedMessage(message, args...). WithCause(err). @@ -30,6 +33,9 @@ func Decorate(err error, message string, args ...interface{}) *Error { // Designed to be used when a original error is passed from another goroutine rather than from a direct method call. // If, however, it is called in the same goroutine, formatter makes some moderated effort to remove duplication. func EnhanceStackTrace(err error, message string, args ...interface{}) *Error { + if err == nil { + return nil + } return NewErrorBuilder(transparentWrapper). WithConditionallyFormattedMessage(message, args...). WithCause(err).