errctx

package
v1.1.0-beta.0...-9958228 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var StrictNoWarningContext = NewContext(contextutil.IgnoreWarn)

StrictNoWarningContext returns all errors directly, and ignore all errors

Functions

This section is empty.

Types

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context defines how to handle an error

func NewContext

func NewContext(handler contextutil.WarnAppender) Context

NewContext creates an error context to handle the errors and warnings

func NewContextWithLevels

func NewContextWithLevels(levels LevelMap, handler contextutil.WarnAppender) Context

NewContextWithLevels creates an error context to handle the errors and warnings

func (*Context) AppendNote

func (ctx *Context) AppendNote(err error)

AppendNote appends the error to warning with level 'Note'. If the inner `warnHandler` is nil, do nothing.

func (*Context) AppendWarning

func (ctx *Context) AppendWarning(err error)

AppendWarning appends the error to warning. If the inner `warnHandler` is nil, do nothing.

func (*Context) HandleError

func (ctx *Context) HandleError(err error) error

HandleError handles the error according to the contextutil. See the comment of `HandleErrorWithAlias` for detailed logic.

It also allows using `errors.ErrorGroup`, in this case, it'll handle each error in order, and return the first error it founds.

func (*Context) HandleErrorWithAlias

func (ctx *Context) HandleErrorWithAlias(internalErr error, err error, warnErr error) error

HandleErrorWithAlias handles the error according to the contextutil.

  1. If the `internalErr` is not `"pingcap/errors".Error`, or the error code is not defined in the `errGroupMap`, or the error level is set to `LevelError`(0), the `err` will be returned directly.
  2. If the error level is set to `LevelWarn`, the `warnErr` will be appended as a warning.
  3. If the error level is set to `LevelIgnore`, this function will return a `nil`.

In most cases, these three should be the same. If there are many different kinds of error internally, but they are expected to give the same error to users, the `err` can be different form `internalErr`. Also, if the warning is expected to be different from the initial error, you can also use the `warnErr` argument.

TODO: is it good to give an error code for internal only errors? Or should we use another way to distinguish different group of errors? TODO: both `types.Context` and `errctx.Context` can handle truncate error now. Refractor them.

func (*Context) LevelForGroup

func (ctx *Context) LevelForGroup(errGroup ErrGroup) Level

LevelForGroup returns the level for a specified group.

func (*Context) LevelMap

func (ctx *Context) LevelMap() LevelMap

LevelMap returns the `levelMap` of the context.

func (*Context) WithErrGroupLevel

func (ctx *Context) WithErrGroupLevel(eg ErrGroup, l Level) Context

WithErrGroupLevel sets a `Level` for an `ErrGroup`

func (*Context) WithErrGroupLevels

func (ctx *Context) WithErrGroupLevels(levels LevelMap) Context

WithErrGroupLevels sets `levelMap` for an `ErrGroup`

func (*Context) WithStrictErrGroupLevel

func (ctx *Context) WithStrictErrGroupLevel() Context

WithStrictErrGroupLevel makes the context to return the error directly for any kinds of errors.

type ErrGroup

type ErrGroup int

ErrGroup groups the error according to the behavior of handling errors

const (
	// ErrGroupTruncate is the group of truncated errors
	ErrGroupTruncate ErrGroup = iota
	// ErrGroupDupKey is the group of duplicate key errors
	ErrGroupDupKey
	// ErrGroupBadNull is the group of bad null errors
	ErrGroupBadNull
	// ErrGroupNoDefault is the group of no default value errors
	ErrGroupNoDefault
	// ErrGroupDividedByZero is the group of divided by zero errors
	ErrGroupDividedByZero
	// ErrGroupAutoIncReadFailed is the group of auto increment read failed errors
	ErrGroupAutoIncReadFailed
	// ErrGroupNoMatchedPartition is the group of no partition is matched errors.
	ErrGroupNoMatchedPartition
)

type Level

type Level uint8

Level defines the behavior for each error

const (
	// LevelError means the error will be returned
	LevelError Level = iota
	// LevelWarn means it will be regarded as a warning
	LevelWarn
	// LevelIgnore means the error will be ignored
	LevelIgnore
)

func ResolveErrLevel

func ResolveErrLevel(ignore bool, warn bool) Level

ResolveErrLevel resolves the error level according to the `ignore` and `warn` flags if ignore is true, it will return `LevelIgnore` to ignore the error, otherwise, it will return `LevelWarn` or `LevelError` according to the `warn` flag Only one of `ignore` and `warn` can be true.

type LevelMap

type LevelMap [errGroupCount]Level

LevelMap indicates the map from `ErrGroup` to `Level`

Jump to

Keyboard shortcuts

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