AdGuardHome/internal/agherr/agherr.go
Ainar Garipov 73c30590e0 Pull request: all: fix lint and naming issues vol. 4
Merge in DNS/adguard-home from 2276-fix-lint-4 to master

Updates #2276.

Squashed commit of the following:

commit 15d49184cd8ce1f8701bf3221e69418ca1778b36
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Dec 8 15:51:34 2020 +0300

    util: fix naming

commit 3b9a86a0feb8c6e0b167e6e23105e8137b0dda76
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Dec 8 15:41:10 2020 +0300

    all: fix lint and naming issues vol. 4
2020-12-08 16:01:13 +03:00

73 lines
1.6 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]
}
// wrapper is a copy of the hidden errors.wrapper interface for tests, linting,
// etc.
type wrapper interface {
Unwrap() error
}