13f708b483
Merge in DNS/adguard-home from 2229-query-log to master Closes #2229. Squashed commit of the following: commit 32508a3f3b1e098869e1649a2774f1f17d14d41f Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Nov 10 16:51:25 2020 +0300 * querylog: add test commit 774159cc313a0284a8bb8327489671e5d7a3e4eb Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Nov 10 15:26:26 2020 +0300 * querylog: better errors commit 27b13a4dcaff9e8f9b08aec81c0c03f62ebd3fa5 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Nov 10 15:18:51 2020 +0300 * querylog: fix end of log handling
68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
// Package agherr contains the extended error type, and the function for
|
|
// wrapping several errors.
|
|
package agherr
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// 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.
|
|
func Many(message string, underlying ...error) error {
|
|
err := &manyError{
|
|
message: message,
|
|
underlying: underlying,
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// Error implements the error interface for *manyError.
|
|
func (e *manyError) Error() 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() error {
|
|
if len(e.underlying) == 0 {
|
|
return nil
|
|
}
|
|
|
|
return e.underlying[0]
|
|
}
|