Documentation
¶
Index ¶
- Variables
- func BadRequest(err string) (int, interface{})
- func Collect(errs ...error) error
- func Forbidden(err string) (int, interface{})
- func GetErrorResponse(statusCode int, err string) (int, interface{})
- func InternalServerError(err string) (int, interface{})
- func NewInsufficientScopesErr(scopes []string) error
- func Unauthorized(err string) (int, interface{})
- type Collector
- type ListResponse
- type Node
- type Response
- type ScopesList
Constants ¶
This section is empty.
Variables ¶
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 ¶
BadRequest returns ErrorResponse with code 400 Usage (with gin):
context.JSON(BadRequest("invalid data"))
func Collect ¶
Collect errors, return one error for all of them (shorthand for errorutil.NewCollector().Do(...).AsError()). Returns nil if there are no errors.
func Forbidden ¶
Forbidden returns ErrorResponse with code 403 Usage (with gin):
context.JSON(Forbidden("forbidden"))
func GetErrorResponse ¶
GetErrorResponse returns ErrorResponse with specified status code Usage (with gin):
context.JSON(GetErrorResponse(http.StatusPaymentRequired, "Not enough money"))
func InternalServerError ¶
InternalServerError returns ErrorResponse with code 500 Usage (with gin):
context.JSON(BadRequest("invalid data"))
func NewInsufficientScopesErr ¶
NewInsufficientScopesErr is a insufficientScopesErr constructor.
func Unauthorized ¶
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 ¶
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 ¶
Do some operation that returns the error. Supports multiple operations at once.
func (*Collector) Iterate ¶
Iterate over the errors in the list. Every error is represented as an errorutil.Node value.
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.
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.