errors

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2023 License: MIT Imports: 9 Imported by: 0

README

pkg说明

errors

此包主要作用是聚合errors,将多个不同的error聚合成一个error。如果是同样的error则只会被记录一次。 使用方式如下所示:

func closeClients(rdclis []*redis.Client) error {
	var errlist []error
	for _, rdcli := range rdclis {
		if rdcli != nil {
			err := rdcli.Close()
			if err != nil {
				errlist = append(errlist, err)
			}
		}
	}
	return utilerrors.NewAggregate(errlist)
}

Documentation

Overview

Package errors provides a way to return detailed information for an RPC request error. The error is normally JSON encoded.

Index

Constants

This section is empty.

Variables

View Source
var ErrPreconditionViolated = errors.New("precondition is violated")

ErrPreconditionViolated is returned when the precondition is violated

Functions

func BadRequest

func BadRequest(id, format string, a ...interface{}) error

BadRequest generates a 400 error.

func Conflict

func Conflict(id, format string, a ...interface{}) error

Conflict generates a 409 error.

func ConvertGrpcCode

func ConvertGrpcCode(err error) codes.Code

ConvertGrpcCode converts a standard Go error into its canonical code. Note that this is only used to translate the error returned by the server applications.

func FilterOut

func FilterOut(err error, fns ...Matcher) error

FilterOut removes all errors that match any of the matchers from the input error. If the input is a singular error, only that error is tested. If the input implements the Aggregate interface, the list of errors will be processed recursively.

This can be used, for example, to remove known-OK errors (such as io.EOF or os.PathNotFound) from a list of errors.

func Forbidden

func Forbidden(id, format string, a ...interface{}) error

Forbidden generates a 403 error.

func GrpcAcceptable

func GrpcAcceptable(err error) bool

func Http2GrpcCode

func Http2GrpcCode(err *Error) codes.Code

func InternalServerError

func InternalServerError(id, format string, a ...interface{}) error

InternalServerError generates a 500 error.

func MethodNotAllowed

func MethodNotAllowed(id, format string, a ...interface{}) error

MethodNotAllowed generates a 405 error.

func New

func New(id, detail string, code int32) error

New generates a custom error.

func NotFound

func NotFound(id, format string, a ...interface{}) error

NotFound generates a 404 error.

func Reduce

func Reduce(err error) error

Reduce will return err or, if err is an Aggregate and only has one item, the first item in the aggregate.

func Timeout

func Timeout(id, format string, a ...interface{}) error

Timeout generates a 408 error.

func Unauthorized

func Unauthorized(id, format string, a ...interface{}) error

Unauthorized generates a 401 error.

func UnwrapIgnorableError

func UnwrapIgnorableError(err string) (bool, string)

UnwrapIgnorableError tries to parse a JSON string into an error. If that fails, it will set the given string as the error detail.

func WrapIgnorableError

func WrapIgnorableError(err error) error

IgnoreError generates a -1 error.

Types

type Aggregate

type Aggregate interface {
	error
	Errors() []error
	Is(error) bool
}

Aggregate represents an object that contains multiple errors, but does not necessarily have singular semantic meaning. The aggregate can be used with `errors.Is()` to check for the occurrence of a specific error type. Errors.As() is not supported, because the caller presumably cares about a specific error of potentially multiple that match the given type.

func AggregateGoroutines

func AggregateGoroutines(funcs ...func() error) Aggregate

AggregateGoroutines runs the provided functions in parallel, stuffing all non-nil errors into the returned Aggregate. Returns nil if all the functions complete successfully.

func CreateAggregateFromMessageCountMap

func CreateAggregateFromMessageCountMap(m MessageCountMap) Aggregate

CreateAggregateFromMessageCountMap converts MessageCountMap Aggregate

func Flatten

func Flatten(agg Aggregate) Aggregate

Flatten takes an Aggregate, which may hold other Aggregates in arbitrary nesting, and flattens them all into a single Aggregate, recursively.

func NewAggregate

func NewAggregate(errlist []error) Aggregate

NewAggregate converts a slice of errors into an Aggregate interface, which is itself an implementation of the error interface. If the slice is empty, this returns nil. It will check if any of the element of input error list is nil, to avoid nil pointer panic when call Error().

type Error

type Error struct {
	Id     string `json:"id"`
	Code   int32  `json:"code"`
	Detail string `json:"detail"`
	Status string `json:"status"`
}

Error implements the error interface.

func Parse

func Parse(err string) *Error

Parse tries to parse a JSON string into an error. If that fails, it will set the given string as the error detail.

func (*Error) Error

func (e *Error) Error() string

type IgnorableError

type IgnorableError struct {
	Err       string `json:"error"`
	Ignorable bool   `json:"ignorable"`
}

func (*IgnorableError) Error

func (e *IgnorableError) Error() string

type Matcher

type Matcher func(error) bool

Matcher is used to match errors. Returns true if the error matches.

type MessageCountMap

type MessageCountMap map[string]int

TODO: move this errors to tech-lab/utils MessageCountMap contains occurrence for each error message.

Jump to

Keyboard shortcuts

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