@@ -84,41 +84,22 @@ func (r *Repo) Validate() (
84
84
return warnings , valErrMap , errors .New ("must find more than zero KEPs" )
85
85
}
86
86
87
- kepHandler , prrHandler := r .KEPHandler , r .PRRHandler
88
87
prrDir := r .PRRApprovalPath
89
88
logrus .Infof ("PRR directory: %s" , prrDir )
90
89
91
90
for _ , filename := range files {
92
- kepFile , err := os .Open (filename )
93
- if err != nil {
94
- return warnings , valErrMap , errors .Wrapf (err , "could not open file %s" , filename )
95
- }
96
-
97
- defer kepFile .Close ()
98
-
99
- logrus .Infof ("parsing %s" , filename )
100
- kep , kepParseErr := kepHandler .Parse (kepFile )
101
- if kepParseErr != nil {
102
- return warnings , valErrMap , errors .Wrap (kepParseErr , "parsing KEP file" )
103
- }
104
- kep .Filename = filename
105
-
106
- // TODO: This shouldn't be required once we push the errors into the
107
- // parser struct
108
- if kep .Error != nil {
109
- return warnings , valErrMap , errors .Wrapf (kep .Error , "%v has an error" , filename )
110
- }
111
-
112
- err = kepval .ValidatePRR (kep , prrHandler , prrDir )
113
- if err != nil {
91
+ if err := validateFile (r , prrDir , filename ); err != nil {
92
+ fvErr := & fatalValidationError {}
93
+ if errors .As (err , fvErr ) {
94
+ return warnings , valErrMap , err
95
+ }
114
96
valErrMap [filename ] = append (valErrMap [filename ], err )
115
97
}
116
98
}
117
99
118
100
if len (valErrMap ) > 0 {
119
101
for filename , errs := range valErrMap {
120
102
logrus .Infof ("the following PRR validation errors occurred in %s:" , filename )
121
-
122
103
for _ , e := range errs {
123
104
logrus .Infof ("%v" , e )
124
105
}
@@ -127,3 +108,35 @@ func (r *Repo) Validate() (
127
108
128
109
return warnings , valErrMap , nil
129
110
}
111
+
112
+ // fatalValidationError will short-circuit KEP parsing and return early.
113
+ type fatalValidationError struct { Err error }
114
+
115
+ func (e fatalValidationError ) Error () string { return e .Err .Error () }
116
+ func (e fatalValidationError ) Unwrap () error { return e .Err }
117
+
118
+ // validateFile runs a validation and returns an error if validation fails.
119
+ // fatalValidationError will be returned if further parsing should be stopped.
120
+ func validateFile (r * Repo , prrDir , filename string ) error {
121
+ kepFile , err := os .Open (filename )
122
+ if err != nil {
123
+ return & fatalValidationError {Err : errors .Wrapf (err , "could not open file %s" , filename )}
124
+ }
125
+ defer kepFile .Close ()
126
+
127
+ logrus .Infof ("parsing %s" , filename )
128
+ kepHandler , prrHandler := r .KEPHandler , r .PRRHandler
129
+ kep , kepParseErr := kepHandler .Parse (kepFile )
130
+ if kepParseErr != nil {
131
+ return errors .Wrap (kepParseErr , "parsing KEP file" )
132
+ }
133
+ kep .Filename = filename
134
+
135
+ // TODO: This shouldn't be required once we push the errors into the
136
+ // parser struct
137
+ if kep .Error != nil {
138
+ return & fatalValidationError {Err : errors .Wrapf (kep .Error , "%v has an error" , filename )}
139
+ }
140
+
141
+ return kepval .ValidatePRR (kep , prrHandler , prrDir )
142
+ }
0 commit comments