-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f192e6
commit d61c452
Showing
5 changed files
with
145 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package goerr | ||
|
||
import "context" | ||
|
||
type errContext struct { | ||
values map[string]any | ||
} | ||
|
||
type errContextKey struct{} | ||
|
||
func InjectValue(ctx context.Context, key string, value any) context.Context { | ||
newCtx := errContext{ | ||
values: make(map[string]any), | ||
} | ||
oldCtx, ok := ctx.Value(errContextKey{}).(*errContext) | ||
if ok { | ||
for k, v := range oldCtx.values { | ||
newCtx.values[k] = v | ||
} | ||
} | ||
|
||
newCtx.values[key] = value | ||
return context.WithValue(ctx, errContextKey{}, &newCtx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package goerr_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/m-mizutani/goerr" | ||
) | ||
|
||
func funcWithContext(ctx context.Context, color string) error { | ||
if color != "red" { | ||
return goerr.New("color is not red").With("color", color).WithContext(ctx) | ||
} | ||
return nil | ||
} | ||
|
||
func TestContext(t *testing.T) { | ||
reqID := "req-123" | ||
|
||
ctx := context.Background() | ||
ctx = goerr.InjectValue(ctx, "request_id", reqID) | ||
|
||
err := funcWithContext(ctx, "blue") | ||
|
||
if err == nil { | ||
t.Error("Expected error, got nil") | ||
} | ||
|
||
goErr := goerr.Unwrap(err) | ||
if goErr == nil { | ||
t.Error("Expected goerr.Error, got other type") | ||
} | ||
|
||
if goErr.Values()["color"] != "blue" { | ||
t.Errorf("Expected color value to be 'blue', got '%v'", goErr.Values()["color"]) | ||
} | ||
|
||
if goErr.Values()["request_id"] != reqID { | ||
t.Errorf("Expected request_id value to be '%s', got '%v'", reqID, goErr.Values()["request_id"]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
"github.com/m-mizutani/goerr" | ||
) | ||
|
||
type requestID string | ||
|
||
func someAction(id requestID) error { | ||
ctx := context.Background() | ||
ctx = goerr.InjectValue(ctx, "request_id", id) | ||
|
||
// ... some action | ||
|
||
return goerr.New("failed").WithContext(ctx) | ||
} | ||
|
||
func main() { | ||
id := requestID("req-123") | ||
if err := someAction(id); err != nil { | ||
slog.Default().Error("aborted", slog.Any("error", err)) | ||
} | ||
} |