1/*
2Copyright 2015 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package generator
18
19import (
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.
25type ErrorTracker struct {
26	io.Writer
27	err error
28}
29
30// NewErrorTracker makes a new error tracker; note that it implements io.Writer.
31func NewErrorTracker(w io.Writer) *ErrorTracker {
32	return &ErrorTracker{Writer: w}
33}
34
35// Write intercepts calls to Write.
36func (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.
48func (et *ErrorTracker) Error() error {
49	return et.err
50}
51