Skip to content

Logging non-fatal async errors #66

@btc

Description

@btc

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/enhancementA net-new feature or improvement to an existing featurekind/supportA question or request for support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions