errorutil

package
v2.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInsufficientScopes = errors.New("insufficient scopes")

ErrInsufficientScopes is wrapped by the insufficientScopesErr. If errors.Is(err, ErrInsufficientScopes) returns true then error implements ScopesList.

Functions

func BadRequest

func BadRequest(err string) (int, interface{})

BadRequest returns ErrorResponse with code 400 Usage (with gin):

context.JSON(BadRequest("invalid data"))

func Collect

func Collect(errs ...error) error

Collect errors, return one error for all of them (shorthand for errorutil.NewCollector().Do(...).AsError()). Returns nil if there are no errors.

func Forbidden

func Forbidden(err string) (int, interface{})

Forbidden returns ErrorResponse with code 403 Usage (with gin):

context.JSON(Forbidden("forbidden"))

func GetErrorResponse

func GetErrorResponse(statusCode int, err string) (int, interface{})

GetErrorResponse returns ErrorResponse with specified status code Usage (with gin):

context.JSON(GetErrorResponse(http.StatusPaymentRequired, "Not enough money"))

func InternalServerError

func InternalServerError(err string) (int, interface{})

InternalServerError returns ErrorResponse with code 500 Usage (with gin):

context.JSON(BadRequest("invalid data"))

func NewInsufficientScopesErr

func NewInsufficientScopesErr(scopes []string) error

NewInsufficientScopesErr is a insufficientScopesErr constructor.

func Unauthorized

func Unauthorized(err string) (int, interface{})

Unauthorized returns ErrorResponse with code 401 Usage (with gin):

context.JSON(Unauthorized("invalid credentials"))

Types

type Collector

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

Collector is a replacement for the core.ErrorCollector function. It is easier to use and contains more functionality. For example, you can iterate over the errors or use Collector.Panic() to immediately panic if there are errors in the chain.

Error messages will differ from the ones produced by ErrorCollector. However, it's for the best because new error messages contain a lot more useful information and can be even used as a stacktrace.

Collector implements Error() and String() methods. As a result, you can use the collector as an error itself or just print out it as a value. However, it is better to use AsError() method if you want to use Collector as an error value because AsError() returns nil if there are no errors in the list.

Example:

err := errorutil.NewCollector().
            Do(errors.New("error 1")).
            Do(errors.New("error 2"), errors.New("error 3"))
// Will print error message.
fmt.Println(err)

This code will produce something like this:

#1 err at /home/user/main.go:62: error 1
#2 err at /home/user/main.go:63: error 2
#3 err at /home/user/main.go:64: error 3

You can also iterate over the error to use their data instead of using predefined message:

err := errorutil.NewCollector().
            Do(errors.New("error 1")).
            Do(errors.New("error 2"), errors.New("error 3"))

for err := range c.Iterate() {
	fmt.Printf("Error at %s:%d: %v\n", err.File, err.Line, err)
}

This code will produce output that looks like this:

Error at /home/user/main.go:164: error 0
Error at /home/user/main.go:164: error 1
Error at /home/user/main.go:164: error 2

Example with GORM migration (Collector is returned as an error here).

return errorutil.NewCollector().Do(
    db.CreateTable(models.Account{}, models.Connection{}).Error,
    db.Table("account").AddUniqueIndex("account_key", "channel").Error,
).AsError()

func NewCollector

func NewCollector() *Collector

NewCollector returns new errorutil.Collector instance.

func (*Collector) AsError

func (e *Collector) AsError() error

AsError returns the Collector itself as an error, but only if there are errors in the list. It returns nil otherwise. This method should be used if you want to return error to the caller, but only if\ Collector actually caught something.

func (*Collector) Do

func (e *Collector) Do(errs ...error) *Collector

Do some operation that returns the error. Supports multiple operations at once.

func (*Collector) Error

func (e *Collector) Error() string

Error message.

func (*Collector) Iterate

func (e *Collector) Iterate() <-chan Node

Iterate over the errors in the list. Every error is represented as an errorutil.Node value.

func (*Collector) Len

func (e *Collector) Len() int

Len returns the number of the errors in the list.

func (*Collector) OK

func (e *Collector) OK() bool

OK returns true if there is no errors in the list.

func (*Collector) Panic

func (e *Collector) Panic()

Panic with the error data if there are errors in the list.

func (*Collector) String

func (e *Collector) String() string

String with an error message.

type ListResponse

type ListResponse struct {
	Error []string `json:"error"`
}

ListResponse contains multiple errors in the list.

type Node

type Node struct {
	Err error

	File string
	PC   uintptr
	Line int
	// contains filtered or unexported fields
}

Node contains information about error in the list.

func (Node) Error

func (e Node) Error() string

Error returns error message from the Node's Err.

type Response

type Response struct {
	Error string `json:"error"`
}

Response with the error message.

type ScopesList

type ScopesList interface {
	Scopes() []string
}

ScopesList is a contract for the scopes list.

func AsInsufficientScopesErr

func AsInsufficientScopesErr(err error) ScopesList

AsInsufficientScopesErr returns ScopesList instance.

Jump to

Keyboard shortcuts

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