samber / samber/oops

[Proposal] Set domain, tags etc. via the context

Open
#37 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
990
Forks
45
Avg merge
5d 9h
Merged PRs (30d)
5

Description

Having all fields for the error builder settable via context.Context would make it easy to make sure that any error further down in the chain has e.g., the domain set.

Motivation

For us the use case is that we have different areas of the code where it would be nice to always be able to ensure all errors have a domain or tags set.

Rather than enforcing everyone to remember to add In -- which sometimes might even be possible to figure out as you don't know what the call-chain is -- you set the In via the context and then as long as you do WithContext it will get set automatically.

Interface

In terms of interface I'm not sure what would be the best to avoid confusion. I can think of a few different ways

  1. Prefixing with With.
  2. Prefixing with WithContext.
  3. Prefixing with Context.
  4. Setting a default OopsErrorBuilder in the context.

(1) would have functions like WithIn. It has the advantage of being similar to other APIs when using context but doesn't play very nicely with the already existing function With (WithWith?).
(2) would have functions like WithContextIn. It is similar to the above but still sounds weird (WithContextWith?) and is maybe unnecessarily verbose without much clarity.
(3) would have functions like ContextIn or ContextTags which while not fully in line with what you would expect in therms of API doesn't collide with other names used and is also fairly clear what it would do.
(4) would need only two functions WithBuilder which sets a default builder in the context that can then be retrieved with FromContext(ctx).

Example

As an example of how it could look.

First of all, you add what you want to the context, using:

ctx := oops.WithBuilder(ctx, oops.In("domain"))

Then somewhere further down the call chain you then do

return oops.FromContext(ctx).Wrap(err)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No implementation files or tests are named. Start by reading the existing In, With, WithContext, and error-builder APIs, then compare the proposed context interfaces and their interactions. Done means a decided interface is implemented so context-carried fields reach errors further down the call chain, with coverage for the documented example.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.