| 1 |
/* |
| 2 |
Copyright 2015 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package generator |
| 18 |
|
| 19 |
import ( |
| 20 |
"io" |
| 21 |
) |
| 22 |
|
| 23 |
// ErrorTracker tracks errors to the underlying writer, so that you can ignore |
| 24 |
// them until you're ready to return. |
| 25 |
type ErrorTracker struct { |
| 26 |
io.Writer |
| 27 |
err error |
| 28 |
} |
| 29 |
|
| 30 |
// NewErrorTracker makes a new error tracker; note that it implements io.Writer. |
| 31 |
func NewErrorTracker(w io.Writer) *ErrorTracker { |
| 32 |
return &ErrorTracker{Writer: w} |
| 33 |
} |
| 34 |
|
| 35 |
// Write intercepts calls to Write. |
| 36 |
func (et *ErrorTracker) Write(p []byte) (n int, err error) { |
| 37 |
if et.err != nil { |
| 38 |
return 0, et.err |
| 39 |
} |
| 40 |
n, err = et.Writer.Write(p) |
| 41 |
if err != nil { |
| 42 |
et.err = err |
| 43 |
} |
| 44 |
return n, err |
| 45 |
} |
| 46 |
|
| 47 |
// Error returns nil if no error has occurred, otherwise it returns the error. |
| 48 |
func (et *ErrorTracker) Error() error { |
| 49 |
return et.err |
| 50 |
} |