-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patherrors.go
More file actions
109 lines (96 loc) · 3.15 KB
/
errors.go
File metadata and controls
109 lines (96 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package spec
import (
"errors"
"strings"
"github.com/oaswrap/spec/internal/validate"
)
// Severity represents the severity level of a validation error.
type Severity = validate.Severity
const (
// SeverityError indicates a strict validation failure.
SeverityError = validate.SeverityError
// SeverityWarning indicates a validation warning that doesn't necessarily invalidate the document.
SeverityWarning = validate.SeverityWarning
// SeverityInfo indicates informational validation feedback.
SeverityInfo = validate.SeverityInfo
)
// ValidationError represents a single validation error with an associated severity.
type ValidationError = validate.Error
// ValidationErrors is a collection of ValidationError. It implements the error
// interface and aggregates multiple validation issues into a single error value.
type ValidationErrors struct { //nolint:errname // ValidationErrors is a better name than ErrorsError or ValidationError here
Errors []ValidationError
}
// Error implements the error interface by joining each entry with "; ".
func (e ValidationErrors) Error() string {
parts := make([]string, 0, len(e.Errors))
for _, ve := range e.Errors {
parts = append(parts, ve.Error())
}
return strings.Join(parts, "; ")
}
// Unwrap returns each ValidationError as a standard error for [errors.Is]/[errors.As] walks.
func (e ValidationErrors) Unwrap() []error {
out := make([]error, 0, len(e.Errors))
for i := range e.Errors {
ve := e.Errors[i]
out = append(out, &ve)
}
return out
}
// HasSeverity reports whether the collection contains an entry at the given severity.
func (e ValidationErrors) HasSeverity(s Severity) bool {
for _, ve := range e.Errors {
if ve.Severity == s {
return true
}
}
return false
}
// toValidationError converts an arbitrary error into a ValidationError. Non-
// validate.Error values are wrapped as SeverityError.
func toValidationError(err error) ValidationError {
var valErr validate.Error
var valErrPtr *validate.Error
if errors.As(err, &valErrPtr) {
return *valErrPtr
}
if errors.As(err, &valErr) {
return valErr
}
return validate.Error{Err: err, Severity: SeverityError}
}
// collectValidationErrors converts a raw error slice into ValidationError entries,
// optionally filtered to a single severity.
func collectValidationErrors(errs []error, only Severity, filter bool) []ValidationError {
var out []ValidationError
for _, err := range errs {
if err == nil {
continue
}
ve := toValidationError(err)
if filter && ve.Severity != only {
continue
}
out = append(out, ve)
}
return out
}
// joinErrors returns a ValidationErrors containing only SeverityError entries,
// or nil when no strict failures exist.
func joinErrors(errs []error) error {
filtered := collectValidationErrors(errs, SeverityError, true)
if len(filtered) == 0 {
return nil
}
return ValidationErrors{Errors: filtered}
}
// joinAllErrors returns a ValidationErrors containing every non-nil entry,
// preserving severity. Used by ValidateReport.
func joinAllErrors(errs []error) error {
collected := collectValidationErrors(errs, 0, false)
if len(collected) == 0 {
return nil
}
return ValidationErrors{Errors: collected}
}