3b2f5d7842
Merge in DNS/adguard-home from 2704-local-addresses-vol.1 to master Updates #2704. Updates #2829. Updates #2846. Squashed commit of the following: commit 9a49b3d27edcb30da7f16a065226907833b1dc81 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Mar 22 15:39:17 2021 +0300 aghnet: imp docs and logging commit 74f95a29c55b9e732276601b0ecc63fb7c3a9f9e Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Mar 19 20:56:51 2021 +0300 all: fix friday evening mistakes commit 0e2066bc5c16ed807fa601780b99e154502361a9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Mar 19 20:51:15 2021 +0300 all: upd testify, imp code quality commit 8237c50b670c58361ccf7adec3ff2452b1196677 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Mar 19 20:19:29 2021 +0300 aghnet: imp test naming commit 14eb1e189339554c0a6d38e2ba7a93917774ebab Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Mar 19 19:41:43 2021 +0300 aghnet: isolate windows-specific functionality commit d461ac8b18c187999da3e3aba116571b7ebe6785 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Mar 19 14:50:05 2021 +0300 aghnet: imp code quality commit d0ee01cb1f8613de2085c0f2f2f396e46beb52a5 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Mar 19 11:59:10 2021 +0300 all: mv funcs to agherr, mk system resolvers getter
128 lines
2.9 KiB
Go
128 lines
2.9 KiB
Go
// Package agherr contains AdGuard Home's error handling helpers.
|
|
package agherr
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
)
|
|
|
|
// 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)
|
|
}
|
|
|
|
// 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.
|
|
//
|
|
// TODO(a.garipov): Add formatting to message.
|
|
func Many(message string, underlying ...error) (err error) {
|
|
err = &manyError{
|
|
message: message,
|
|
underlying: underlying,
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// Error implements the error interface for *manyError.
|
|
func (e *manyError) Error() (msg string) {
|
|
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
|
|
// errors.
|
|
_, _ = 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.
|
|
func (e *manyError) Unwrap() (err error) {
|
|
if len(e.underlying) == 0 {
|
|
return nil
|
|
}
|
|
|
|
return e.underlying[0]
|
|
}
|
|
|
|
// wrapper is a copy of the hidden errors.wrapper interface for tests, linting,
|
|
// etc.
|
|
type wrapper interface {
|
|
Unwrap() error
|
|
}
|
|
|
|
// 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.
|
|
//
|
|
// TODO(a.garipov): Clearify the function usage.
|
|
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...)
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|