errorcontext

package
v3.11.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2021 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package errorcontext implements a Context that can be canceled with a specific error instead of the usual context.Canceled.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CancelFunc

type CancelFunc func(error)

CancelFunc extends the regular context.CancelFunc with an error argument

func New

func New(parent context.Context) (context.Context, CancelFunc)

New returns a new ErrorContext that extends the parent context

type ErrorContext

type ErrorContext struct {
	context.Context
	// contains filtered or unexported fields
}

ErrorContext can be used to attach errors to a context cancelation.

Example
package main

import (
	"go.thethings.network/lorawan-stack/v3/pkg/errorcontext"
	"go.thethings.network/lorawan-stack/v3/pkg/util/test"
)

var err error

func main() {
	ctx, cancel := errorcontext.New(test.Context())
	defer cancel(nil)

	go func() {
		for {
			// do work
			if err != nil {
				cancel(err)
			}
		}
	}()

	for {
		select {
		// case data := <-dataChan:
		case <-ctx.Done():
			return
		}
	}
}
Output:

func (*ErrorContext) Cancel

func (c *ErrorContext) Cancel(err error)

Cancel the ErrorContext with an error. The context can not be re-used or re-canceled after this function is called.

func (*ErrorContext) Err

func (c *ErrorContext) Err() error

Err returns the err of the ErrorContext if non-nil, or the error of the underlying Context.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL