Skip to content
Merged
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
2 changes: 1 addition & 1 deletion builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func (b OopsErrorBuilder) WithContext(ctx context.Context, keys ...any) OopsErro
for _, k := range keys {
switch k := k.(type) {
case fmt.Stringer:
snap[k.String()] = contextValueOrNil(ctx, k.String())
snap[k.String()] = contextValueOrNil(ctx, k)
case string:
snap[k] = contextValueOrNil(ctx, k)
case *string:
Expand Down
26 changes: 26 additions & 0 deletions context_stringer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package oops

import (
"context"
"testing"
)

type stringerContextKey int

func (k stringerContextKey) String() string { return "request_id" }

func TestWithContextPreservesStringerKeyIdentity(t *testing.T) {
t.Parallel()
key := stringerContextKey(1)
ctx := context.WithValue(context.Background(), key, "typed value")
// A string key with the same display name must not shadow the typed key.
ctx = context.WithValue(ctx, "request_id", "string value") //nolint:staticcheck,revive
wrapped := WithContext(ctx, key).Errorf("failed")
err, ok := wrapped.(OopsError)
if !ok {
t.Fatalf("unexpected error type: %T", wrapped)
}
if got := err.context["request_id"]; got != "typed value" {
t.Fatalf("context value = %v", got)
}
}
Loading