morecontext

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2022 License: Apache-2.0 Imports: 6 Imported by: 7

README

morecontext

context.Context helpers to make your life slightly easier.

-- import "github.com/gopuff/morecontext"

Usage

func ForSignals
func ForSignals(sigs ...os.Signal) context.Context

ForSignals returns a context.Context that will be cancelled if the given signals (or SIGTERM and SIGINT by default, if none are passed) are received by the process.

func WithMessage
func WithMessage(ctx context.Context, format string, args ...interface{}) context.Context

WithMessage is a helper for creating a MessageContext instance, with more context about exactly which context cancellation occurred.

type MessageContext
type MessageContext struct {
	context.Context
	Message string
}

MessageContext is a context.Context wrapper that is intended to include some extra metadata about which context was cancelled. Useful for distinguishing e.g. http request cancellation vs deadline vs process sigterm handling.

func (MessageContext) Err
func (c MessageContext) Err() error

Err returns an error with extra context messaging

type MessageError
type MessageError struct {
	Message  string
	Original error
}

MessageError implements error but separates the message from the original error in case you want it.

func (*MessageError) Error
func (c *MessageError) Error() string

Error implements error and prints out the message plus the metadata about which context was cancelled.

func (*MessageError) Unwrap
func (c *MessageError) Unwrap() error

Unwrap supports errors.Is/errors.As

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ForSignals added in v0.3.0

func ForSignals(sigs ...os.Signal) context.Context

ForSignals returns a context.Context that will be cancelled if the given signals (or SIGTERM and SIGINT by default, if none are passed) are received by the process.

func WithMessage

func WithMessage(ctx context.Context, format string, args ...interface{}) context.Context

WithMessage is a helper for creating a MessageContext instance, with more context about exactly which context cancellation occurred.

Types

type MessageContext

type MessageContext struct {
	context.Context
	Message string
}

MessageContext is a context.Context wrapper that is intended to include some extra metadata about which context was cancelled. Useful for distinguishing e.g. http request cancellation vs deadline vs process sigterm handling.

func (MessageContext) Err

func (c MessageContext) Err() error

Err returns an error with extra context messaging

type MessageError

type MessageError struct {
	Message  string
	Original error
}

MessageError implements error but separates the message from the original error in case you want it.

func (*MessageError) Error

func (c *MessageError) Error() string

Error implements error and prints out the message plus the metadata about which context was cancelled.

func (*MessageError) Unwrap

func (c *MessageError) Unwrap() error

Unwrap supports errors.Is/errors.As

Jump to

Keyboard shortcuts

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