Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Apr 13, 2022
1 parent cf0a130 commit cd1e5e9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 45 deletions.
32 changes: 16 additions & 16 deletions asisunwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func AsSlice(errs []error, target interface{}) bool {
if x, ok := err.(interface{ As(interface{}) bool }); ok && x.As(target) {
return true
}
err = Unwrap(err)
err = Unwrap(err) //nolint:ineffassign,staticcheck
}
return false
}
Expand Down Expand Up @@ -99,9 +99,9 @@ func Is(err, target error) bool {
if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
return true
}
if ok := As(err, &target); ok {
return true
}
// if ok := As(err, &target); ok {
// return true
// }
// TODO: consider supporting target.Is(err). This would allow
// user-definable predicates, but also may allow for coping with sloppy
// APIs, thereby making it easier to get away with them.
Expand All @@ -114,11 +114,11 @@ func Is(err, target error) bool {
// IsSlice tests err.Is for errs slice
func IsSlice(errs []error, target error) bool {
if target == nil {
//for _, e := range errs {
// for _, e := range errs {
// if e == target {
// return true
// }
//}
// }
return false
}

Expand All @@ -137,11 +137,11 @@ func IsSlice(errs []error, target error) bool {
if x, ok := e.(interface{ Is(error) bool }); ok && x.Is(target) {
return true
}
//if err := Unwrap(e); err == nil {
// if err := Unwrap(e); err == nil {
// return false
//}
// }
}
return false
return false //nolint:staticcheck
}
}

Expand Down Expand Up @@ -179,11 +179,11 @@ func TypeIs(err, target error) bool {
// TypeIsSlice tests err.Is for errs slice
func TypeIsSlice(errs []error, target error) bool {
if target == nil {
//for _, e := range errs {
// for _, e := range errs {
// if e == target {
// return true
// }
//}
// }
return false
}

Expand All @@ -192,9 +192,9 @@ func TypeIsSlice(errs []error, target error) bool {
if isComparable {
tt := reflect.TypeOf(target)
for _, e := range errs {
//if e == target {
// if e == target {
// return true
//}
// }
if reflect.TypeOf(e) == tt {
return true
}
Expand All @@ -206,11 +206,11 @@ func TypeIsSlice(errs []error, target error) bool {
if x, ok := e.(interface{ Is(error) bool }); ok && x.Is(target) {
return true
}
//if err := Unwrap(e); err == nil {
// if err := Unwrap(e); err == nil {
// return false
//}
// }
}
return false
return false //nolint:staticcheck
}
}

Expand Down
35 changes: 26 additions & 9 deletions asisunwrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ import (
"testing"
)

func TestErrorCodeIs(t *testing.T) {
var err error = BadRequest
if !Is(err, BadRequest) {
t.Fatalf("want is")
}

err = io.ErrClosedPipe
if Is(err, BadRequest) {
t.Fatalf("want not is")
}

err = NotFound
if Is(err, BadRequest) {
t.Fatalf("want not is (code)")
}
}

// TestErrorsIs _
func TestErrorsIs(t *testing.T) {
_, err := strconv.ParseFloat("hello", 64)
Expand Down Expand Up @@ -97,7 +114,7 @@ func TestWrap(t *testing.T) {
err = Wrap(Internal, "hello, %v", "world")
t.Logf("failed: %+v", err)

Unwrap(io.EOF)
_ = Unwrap(io.EOF)
}

func TestTypeIsSlice(t *testing.T) {
Expand Down Expand Up @@ -157,24 +174,24 @@ func TestTypeIsSlice(t *testing.T) {
func TestAsRaisePanic(t *testing.T) {

t.Run("1", func(t *testing.T) {
defer func() { recover() }()
defer func() { recover() }() //nolint:errcheck
As(nil, nil)
})

t.Run("2", func(t *testing.T) {
defer func() { recover() }()
defer func() { recover() }() //nolint:errcheck
var v int
As(nil, &v)
})

t.Run("3", func(t *testing.T) {
defer func() { recover() }()
defer func() { recover() }() //nolint:errcheck
var err error
As(nil, &err)
})

t.Run("4", func(t *testing.T) {
defer func() { recover() }()
defer func() { recover() }() //nolint:errcheck
var err int
As(nil, err)
})
Expand All @@ -184,24 +201,24 @@ func TestAsRaisePanic(t *testing.T) {
func TestAsSliceRaisePanic(t *testing.T) {

t.Run("1", func(t *testing.T) {
defer func() { recover() }()
defer func() { recover() }() //nolint:errcheck
AsSlice(nil, nil)
})

t.Run("2", func(t *testing.T) {
defer func() { recover() }()
defer func() { recover() }() //nolint:errcheck
var v int
AsSlice(nil, &v)
})

t.Run("3", func(t *testing.T) {
defer func() { recover() }()
defer func() { recover() }() //nolint:errcheck
var err error
AsSlice(nil, &err)
})

t.Run("4", func(t *testing.T) {
defer func() { recover() }()
defer func() { recover() }() //nolint:errcheck
var err int
AsSlice(nil, err)
})
Expand Down
2 changes: 1 addition & 1 deletion causes.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (w *causes2) WithErrors(errs ...error) *causes2 {
for _, e := range errs {
if e != nil {
if check, ok := e.(interface{ IsEmpty() bool }); ok {
if check.IsEmpty() == false {
if !check.IsEmpty() {
w.Causers = append(w.Causers, e)
}
} else {
Expand Down
18 changes: 9 additions & 9 deletions coded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"testing"
)

type bizErr struct {
type bizErr struct { //nolint:unused
num int
}

func (e *bizErr) Error() string {
func (e *bizErr) Error() string { //nolint:unused
return fmt.Sprintf("%v", e.num)
}

Expand All @@ -29,11 +29,11 @@ func TestCode_Register(t *testing.T) {
c := Code(111)
t.Logf("failed: %+v", c)

c.Register("Code111")
_ = c.Register("Code111")
t.Logf("failed: %+v", c)
}

//func TestCodeEqual(t *testing.T) {
// func TestCodeEqual(t *testing.T) {
// be := &bizErr{1}
// err := InvalidArgument.New("wrong").Attach(be)
//
Expand All @@ -46,9 +46,9 @@ func TestCode_Register(t *testing.T) {
// if !Equal(e1, InvalidArgument) {
// t.Fatal("expecting e1 is equal to InvalidArgument")
// }
//}
// }
//
//func TestCodeAsIsAndSoOn(t *testing.T) {
// func TestCodeAsIsAndSoOn(t *testing.T) {
// be := &bizErr{1}
// err := InvalidArgument.New("wrong").Attach(be)
//
Expand All @@ -61,9 +61,9 @@ func TestCode_Register(t *testing.T) {
// if !err.Is(be) {
// t.Fatal("WithCodeInfo.Is() failed.")
// }
//}
// }

//func TestCodes(t *testing.T) {
// func TestCodes(t *testing.T) {
// be := &bizErr{1}
// err := InvalidArgument.New("wrong").Attach(be)
// t.Log(err)
Expand All @@ -84,7 +84,7 @@ func TestCode_Register(t *testing.T) {
// if old.Is(err, io.EOF) {
// t.Fatal("wrong Is(): shouldn't be like to io.EOF")
// }
//}
// }

func TestCodesEqual(t *testing.T) {
err := InvalidArgument.New("wrong").WithErrors(io.ErrShortWrite)
Expand Down
12 changes: 6 additions & 6 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ func New(args ...interface{}) Error {

if len(args) > 0 {
if message, ok := args[0].(string); ok {
return s.WithSkip(2).WithMessage(message, args[1:]...).Build().(Error)
return s.WithSkip(2).WithMessage(message, args[1:]...).Build()
}
for _, opt := range args {
if o, ok := opt.(Opt); ok {
o(s)
}
}
return s.Build().(Error)
return s.Build()
}

return &WithStackInfo{Stack: callers(1)}
Expand Down Expand Up @@ -107,11 +107,11 @@ func (s *builder) WithCode(code Code) Builder {
return s
}

//// Attach attaches the given errs as inner errors.
//// For backward compatibility to v2
//func (s *builder) Attach(errs ...error) Buildable {
// // Attach attaches the given errs as inner errors.
// // For backward compatibility to v2
// func (s *builder) Attach(errs ...error) Buildable {
// return s.WithErrors(errs...).Build()
//}
// }

// WithMessage formats the error message
func (s *builder) WithMessage(message string, args ...interface{}) Builder {
Expand Down
8 changes: 4 additions & 4 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func TestNew(t *testing.T) {
t.Logf("failed: %+v", err)

// since v3.0.5, Attach() has no return value
//err = New("hello").Attach(io.EOF)
//t.Logf("failed: %+v", err)
// err = New("hello").Attach(io.EOF)
// t.Logf("failed: %+v", err)

}

Expand Down Expand Up @@ -108,10 +108,10 @@ func TestWithStackInfo_New(t *testing.T) {
func TestTemplateFormat(t *testing.T) {
err := New("cannot set: %v (%v) -> %v (%v)")

err.FormatWith("a", "b", "c", "d")
_ = err.FormatWith("a", "b", "c", "d")
t.Logf("Error: %v", err)
t.Logf("Error: %+v", err)

err.FormatWith("1", "2", "3", "4")
_ = err.FormatWith("1", "2", "3", "4")
t.Logf("Error: %v", err)
}

0 comments on commit cd1e5e9

Please sign in to comment.