mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-05 08:27:05 +02:00
fixed / suppressed (irrelevant) golangci-lint errors
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package error
|
||||
package err
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
)
|
||||
|
||||
// baseError is an immutable wrapper around an error.
|
||||
//
|
||||
//nolint:recvcheck
|
||||
type baseError struct {
|
||||
Err error `json:"err"`
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package error
|
||||
package err
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -60,7 +60,7 @@ func (b *Builder) Add(err error) *Builder {
|
||||
b.Lock()
|
||||
defer b.Unlock()
|
||||
|
||||
switch err := err.(type) {
|
||||
switch err := From(err).(type) {
|
||||
case *baseError:
|
||||
b.errs = append(b.errs, err.Err)
|
||||
case *nestedError:
|
||||
@@ -70,7 +70,7 @@ func (b *Builder) Add(err error) *Builder {
|
||||
b.errs = append(b.errs, err)
|
||||
}
|
||||
default:
|
||||
b.errs = append(b.errs, err)
|
||||
panic("bug: should not reach here")
|
||||
}
|
||||
|
||||
return b
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package error_test
|
||||
package err_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package error
|
||||
package err
|
||||
|
||||
type Error interface {
|
||||
error
|
||||
@@ -24,6 +24,8 @@ type Error interface {
|
||||
|
||||
// this makes JSON marshaling work,
|
||||
// as the builtin one doesn't.
|
||||
//
|
||||
//nolint:errname
|
||||
type errStr string
|
||||
|
||||
func (err errStr) Error() string {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package error
|
||||
package err
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -81,10 +81,10 @@ func TestErrorImmutability(t *testing.T) {
|
||||
|
||||
for range 3 {
|
||||
// t.Logf("%d: %v %T %s", i, errors.Unwrap(err), err, err)
|
||||
err.Subject("foo")
|
||||
_ = err.Subject("foo")
|
||||
ExpectFalse(t, strings.Contains(err.Error(), "foo"))
|
||||
|
||||
err.With(err2)
|
||||
_ = err.With(err2)
|
||||
ExpectFalse(t, strings.Contains(err.Error(), "extra"))
|
||||
ExpectFalse(t, err.Is(err2))
|
||||
|
||||
@@ -102,7 +102,7 @@ func TestErrorWith(t *testing.T) {
|
||||
ExpectTrue(t, err3.Is(err1))
|
||||
ExpectTrue(t, err3.Is(err2))
|
||||
|
||||
err2.Subject("foo")
|
||||
_ = err2.Subject("foo")
|
||||
|
||||
ExpectTrue(t, err3.Is(err1))
|
||||
ExpectTrue(t, err3.Is(err2))
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package error
|
||||
package err
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package error
|
||||
package err
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
//nolint:recvcheck
|
||||
type nestedError struct {
|
||||
Err error `json:"err"`
|
||||
Extras []error `json:"extras"`
|
||||
@@ -66,7 +67,18 @@ func (err *nestedError) Is(other error) bool {
|
||||
}
|
||||
|
||||
func (err *nestedError) Error() string {
|
||||
return buildError(err, 0)
|
||||
if err == nil {
|
||||
return makeLine("<nil>", 0)
|
||||
}
|
||||
|
||||
lines := make([]string, 0, 1+len(err.Extras))
|
||||
if err.Err != nil {
|
||||
lines = append(lines, makeLine(err.Err.Error(), 0))
|
||||
}
|
||||
if extras := makeLines(err.Extras, 1); len(extras) > 0 {
|
||||
lines = append(lines, extras...)
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
//go:inline
|
||||
@@ -86,7 +98,7 @@ func makeLines(errs []error, level int) []string {
|
||||
}
|
||||
lines := make([]string, 0, len(errs))
|
||||
for _, err := range errs {
|
||||
switch err := err.(type) {
|
||||
switch err := From(err).(type) {
|
||||
case *nestedError:
|
||||
if err.Err != nil {
|
||||
lines = append(lines, makeLine(err.Err.Error(), level))
|
||||
@@ -100,21 +112,3 @@ func makeLines(errs []error, level int) []string {
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
func buildError(err error, level int) string {
|
||||
switch err := err.(type) {
|
||||
case nil:
|
||||
return makeLine("<nil>", level)
|
||||
case *nestedError:
|
||||
lines := make([]string, 0, 1+len(err.Extras))
|
||||
if err.Err != nil {
|
||||
lines = append(lines, makeLine(err.Err.Error(), level))
|
||||
}
|
||||
if extras := makeLines(err.Extras, level+1); len(extras) > 0 {
|
||||
lines = append(lines, extras...)
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
default:
|
||||
return makeLine(err.Error(), level)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package error
|
||||
package err
|
||||
|
||||
import (
|
||||
"strings"
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/yusing/go-proxy/internal/utils/strutils/ansi"
|
||||
)
|
||||
|
||||
//nolint:errname
|
||||
type withSubject struct {
|
||||
Subject string `json:"subject"`
|
||||
Err error `json:"err"`
|
||||
@@ -18,23 +19,26 @@ func highlight(subject string) string {
|
||||
}
|
||||
|
||||
func PrependSubject(subject string, err error) error {
|
||||
switch err := err.(type) {
|
||||
case nil:
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
//nolint:errorlint
|
||||
switch err := err.(type) {
|
||||
case *withSubject:
|
||||
return err.Prepend(subject)
|
||||
case Error:
|
||||
return err.Subject(subject)
|
||||
default:
|
||||
return &withSubject{subject, err}
|
||||
}
|
||||
return &withSubject{subject, err}
|
||||
}
|
||||
|
||||
func (err withSubject) Prepend(subject string) *withSubject {
|
||||
func (err *withSubject) Prepend(subject string) *withSubject {
|
||||
clone := *err
|
||||
if subject != "" {
|
||||
err.Subject = subject + subjectSep + err.Subject
|
||||
clone.Subject = subject + subjectSep + clone.Subject
|
||||
}
|
||||
return &err
|
||||
return &clone
|
||||
}
|
||||
|
||||
func (err *withSubject) Is(other error) bool {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package error
|
||||
package err
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
@@ -19,12 +20,12 @@ func Errorf(format string, args ...any) Error {
|
||||
return &baseError{fmt.Errorf(format, args...)}
|
||||
}
|
||||
|
||||
func From(err error) Error {
|
||||
func From(err error) (e Error) {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if err, ok := err.(Error); ok {
|
||||
return err
|
||||
if errors.As(err, &e) {
|
||||
return e
|
||||
}
|
||||
return &baseError{err}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user