Debian Codegraph @

errcheck_fielderror.go

raw
1 package check
2
3 import (
4 "errors"
5 "reflect"
6 )
7
8 //nolint:gochecknoinits // Required for default CheckFieldError registration.
9 func init() {
10 RegisterErrChecker(CheckFieldError)
11 }
12
13 // fieldError is a duck-typed interface for errors that carry per-field
14 // validation information, like validator.FieldError.
15 type fieldError interface {
16 Namespace() string
17 Tag() string
18 }
19
20 type fieldErrKey struct {
21 ns, tag string
22 }
23
24 // CheckFieldError compares two errors that (possibly after [errors.As]-style unwrapping)
25 // are validator.FieldError-like values or slices of them,
26 // by an unordered multiset of (Namespace, Tag) pairs — order does not matter.
27 // Works with any type structurally providing Namespace()/Tag().
28 //
29 // Auto-registered on loading the package.
30 func CheckFieldError(actual, expected error) (equal, ok bool) {
31 actualFEs, ok1 := extractFieldErrors(actual)
32 expectedFEs, ok2 := extractFieldErrors(expected)
33 if !ok1 || !ok2 {
34 return false, false
35 }
36 if len(actualFEs) != len(expectedFEs) {
37 return false, true
38 }
39 counts := make(map[fieldErrKey]int, len(expectedFEs))
40 for _, fe := range expectedFEs {
41 counts[fieldErrKey{fe.Namespace(), fe.Tag()}]++
42 }
43 for _, fe := range actualFEs {
44 counts[fieldErrKey{fe.Namespace(), fe.Tag()}]--
45 }
46 for _, c := range counts {
47 if c != 0 {
48 return false, true
49 }
50 }
51 return true, true
52 }
53
54 // extractFieldErrors walks the error tree looking for a value that
55 // (directly or after unwrapping) is a fieldError or a slice of fieldErrors.
56 // Returns the normalized slice and whether field errors were found.
57 func extractFieldErrors(err error) ([]fieldError, bool) {
58 if err == nil {
59 return nil, false
60 }
61 // Avoid calling methods on typed nil (e.g. (*net.OpError)(nil)) which may panic.
62 v := reflect.ValueOf(err)
63 if v.Kind() == reflect.Pointer && v.IsNil() {
64 return nil, false
65 }
66 // Check if the error itself is a fieldError or a slice of fieldErrors.
67 if fe, ok := err.(fieldError); ok {
68 return []fieldError{fe}, true
69 }
70 if fes, ok := sliceFieldErrors(err); ok {
71 return fes, true
72 }
73 // Walk the chain for a single fieldError.
74 var fe fieldError
75 if errors.As(err, &fe) {
76 return []fieldError{fe}, true
77 }
78 // Walk the chain for a slice of fieldErrors.
79 return walkFieldErrorSlice(err)
80 }
81
82 // sliceFieldErrors checks if err is a slice whose every element implements
83 // fieldError, and returns them as a normalized slice.
84 func sliceFieldErrors(err error) ([]fieldError, bool) {
85 val := reflect.ValueOf(err)
86 if val.Kind() != reflect.Slice {
87 return nil, false
88 }
89 fes := make([]fieldError, 0, val.Len())
90 for i := range val.Len() {
91 elem := val.Index(i).Interface()
92 fe, ok := elem.(fieldError)
93 if !ok {
94 return nil, false
95 }
96 fes = append(fes, fe)
97 }
98 return fes, true
99 }
100
101 // walkFieldErrorSlice walks the error chain looking for a slice of fieldErrors.
102 func walkFieldErrorSlice(err error) ([]fieldError, bool) {
103 //nolint:errorlint // Structural interface checks, not error matching.
104 switch u := err.(type) {
105 case interface{ Unwrap() error }:
106 return extractFieldErrors(u.Unwrap())
107 case interface{ Unwrap() []error }:
108 for _, e := range u.Unwrap() {
109 if fes, ok := extractFieldErrors(e); ok {
110 return fes, ok
111 }
112 }
113 }
114 return nil, false
115 }