Skip to content

Commit 6616b06

Browse files
Zerekerclaude
andcommitted
refactor(go/core): omit empty details in ToReflectionError JSON output
Only allocate ReflectionErrorDetails when stack or traceId is present, leveraging the omitempty tag to produce cleaner JSON output. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 110e9c3 commit 6616b06

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

go/core/error.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,19 @@ func (e *GenkitError) Unwrap() error {
107107

108108
// ToReflectionError returns a JSON-serializable representation for reflection API responses.
109109
func (e *GenkitError) ToReflectionError() ReflectionError {
110-
errDetails := &ReflectionErrorDetails{}
110+
var errDetails *ReflectionErrorDetails
111111
if e.Details != nil {
112-
if stackVal, ok := e.Details["stack"].(string); ok {
113-
errDetails.Stack = &stackVal
114-
}
115-
if traceVal, ok := e.Details["traceId"].(string); ok {
116-
errDetails.TraceID = &traceVal
112+
stackVal, stackOk := e.Details["stack"].(string)
113+
traceVal, traceOk := e.Details["traceId"].(string)
114+
115+
if stackOk || traceOk {
116+
errDetails = &ReflectionErrorDetails{}
117+
if stackOk {
118+
errDetails.Stack = &stackVal
119+
}
120+
if traceOk {
121+
errDetails.TraceID = &traceVal
122+
}
117123
}
118124
}
119125
return ReflectionError{

go/core/error_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ func TestGenkitErrorToReflectionError(t *testing.T) {
157157
if re.Message != "success" {
158158
t.Errorf("Message = %q, want %q", re.Message, "success")
159159
}
160-
if re.Details.Stack != nil {
161-
t.Error("expected nil stack")
160+
if re.Details != nil {
161+
t.Error("expected nil details")
162162
}
163163
})
164164
}

0 commit comments

Comments
 (0)