Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context feature #11

Closed
wants to merge 1 commit into from
Closed

Add context feature #11

wants to merge 1 commit into from

Conversation

m-mizutani
Copy link
Owner

This PR adds WithContext method to add values from context to the error. Values can be added to context.Context by goerr.InjectValue.

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))
	}
}

Output:

2024/10/19 09:51:04 ERROR aborted error.message=failed error.values.request_id=req-123 (snip)

@m-mizutani
Copy link
Owner Author

It's not appropriate to separate responsibility of context and error handling

@m-mizutani m-mizutani closed this Oct 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant