2021-03-16 16:11:32 +00:00
|
|
|
// Package agherr contains AdGuard Home's error handling helpers.
|
2020-11-05 12:20:57 +00:00
|
|
|
package agherr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2021-03-22 13:46:36 +00:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2020-11-05 12:20:57 +00:00
|
|
|
)
|
|
|
|
|
2020-11-10 16:00:55 +00:00
|
|
|
// Error is the constant error type.
|
|
|
|
type Error string
|
|
|
|
|
|
|
|
// Error implements the error interface for Error.
|
|
|
|
func (err Error) Error() (msg string) {
|
|
|
|
return string(err)
|
|
|
|
}
|
|
|
|
|
2020-11-05 12:20:57 +00:00
|
|
|
// manyError is an error containing several wrapped errors. It is created to be
|
|
|
|
// a simpler version of the API provided by github.com/joomcode/errorx.
|
|
|
|
type manyError struct {
|
|
|
|
message string
|
|
|
|
underlying []error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Many wraps several errors and returns a single error.
|
2021-03-16 16:11:32 +00:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Add formatting to message.
|
|
|
|
func Many(message string, underlying ...error) (err error) {
|
|
|
|
err = &manyError{
|
2020-11-05 12:20:57 +00:00
|
|
|
message: message,
|
|
|
|
underlying: underlying,
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error implements the error interface for *manyError.
|
2021-03-16 16:11:32 +00:00
|
|
|
func (e *manyError) Error() (msg string) {
|
2020-11-05 12:20:57 +00:00
|
|
|
switch len(e.underlying) {
|
|
|
|
case 0:
|
|
|
|
return e.message
|
|
|
|
case 1:
|
|
|
|
return fmt.Sprintf("%s: %s", e.message, e.underlying[0])
|
|
|
|
default:
|
|
|
|
b := &strings.Builder{}
|
|
|
|
|
|
|
|
// Ignore errors, since strings.(*Buffer).Write never returns
|
2021-04-07 18:16:06 +01:00
|
|
|
// errors. We don't use aghstrings.WriteToBuilder here since
|
|
|
|
// this package should be importable for any other.
|
2020-11-05 12:20:57 +00:00
|
|
|
_, _ = fmt.Fprintf(b, "%s: %s (hidden: %s", e.message, e.underlying[0], e.underlying[1])
|
|
|
|
for _, u := range e.underlying[2:] {
|
|
|
|
// See comment above.
|
|
|
|
_, _ = fmt.Fprintf(b, ", %s", u)
|
|
|
|
}
|
|
|
|
|
|
|
|
// See comment above.
|
|
|
|
_, _ = b.WriteString(")")
|
|
|
|
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unwrap implements the hidden errors.wrapper interface for *manyError.
|
2021-03-16 16:11:32 +00:00
|
|
|
func (e *manyError) Unwrap() (err error) {
|
2020-11-05 12:20:57 +00:00
|
|
|
if len(e.underlying) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.underlying[0]
|
|
|
|
}
|
2020-12-08 13:01:13 +00:00
|
|
|
|
|
|
|
// wrapper is a copy of the hidden errors.wrapper interface for tests, linting,
|
|
|
|
// etc.
|
|
|
|
type wrapper interface {
|
|
|
|
Unwrap() error
|
|
|
|
}
|
2021-03-16 16:11:32 +00:00
|
|
|
|
|
|
|
// Annotate annotates the error with the message, unless the error is nil. This
|
|
|
|
// is a helper function to simplify code like this:
|
|
|
|
//
|
|
|
|
// func (f *foo) doStuff(s string) (err error) {
|
|
|
|
// defer func() {
|
|
|
|
// if err != nil {
|
|
|
|
// err = fmt.Errorf("bad foo string %q: %w", s, err)
|
|
|
|
// }
|
|
|
|
// }()
|
|
|
|
//
|
|
|
|
// // …
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Instead, write:
|
|
|
|
//
|
|
|
|
// func (f *foo) doStuff(s string) (err error) {
|
|
|
|
// defer agherr.Annotate("bad foo string %q: %w", &err, s)
|
|
|
|
//
|
|
|
|
// // …
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// msg must contain the final ": %w" verb.
|
2021-03-22 13:46:36 +00:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Clearify the function usage.
|
2021-03-16 16:11:32 +00:00
|
|
|
func Annotate(msg string, errPtr *error, args ...interface{}) {
|
|
|
|
if errPtr == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := *errPtr
|
|
|
|
if err != nil {
|
|
|
|
args = append(args, err)
|
|
|
|
|
|
|
|
*errPtr = fmt.Errorf(msg, args...)
|
|
|
|
}
|
|
|
|
}
|
2021-03-22 13:46:36 +00:00
|
|
|
|
|
|
|
// LogPanic is a convinient helper function to log a panic in a goroutine. It
|
|
|
|
// should not be used where proper error handling is required.
|
|
|
|
func LogPanic(prefix string) {
|
|
|
|
if v := recover(); v != nil {
|
|
|
|
if prefix != "" {
|
|
|
|
log.Error("%s: recovered from panic: %v", prefix, v)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Error("recovered from panic: %v", v)
|
|
|
|
}
|
|
|
|
}
|