-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Labels
kind/enhancementA net-new feature or improvement to an existing featureA net-new feature or improvement to an existing featurekind/supportA question or request for supportA question or request for support
Description
I wish to propose a pattern for logging the non-fatal errors that occur in asynchronous operations.
Context exposes an error logging method:
func (ctx *u.IPFSContext) LogError(err error) {
// may be implemented synchronously
}The interface extends the golang Context:
type IPFSContext interface {
context.Context
LogError(err error)
}Now async actors can get information back out to parent without modifying control flow or confusing the caller/callee contract!
func asyncWork(ctx u.IPFSContext, in <-chan rawData) {
for raw := <- in {
val, err := transform(raw)
if err != nil {
ctx.LogError(err)
continue
}
doThingsWith(val)
}
}the context creator can listen for errors:
func (ctx *context.IPFSContext) Errs() <-chan error {
// return a channel of errors
}The pattern is analogous to the way children listen on Done()
ctx := context.New(context.Background())
go asyncWork(ctx, ch)
for {
select {
case err <-ctx.Errs():
u.DOut(err)
}
}Thoughts? @jbenet @whyrusleeping
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
kind/enhancementA net-new feature or improvement to an existing featureA net-new feature or improvement to an existing featurekind/supportA question or request for supportA question or request for support