github.com/waynz0r/controller-tools@v0.4.1-0.20200916220028-16254aeef2d7/pkg/loader/errors.go (about) 1 /* 2 Copyright 2019 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 loader 18 19 import ( 20 "fmt" 21 "go/ast" 22 "go/token" 23 ) 24 25 // PositionedError represents some error with an associated position. 26 // The position is tied to some external token.FileSet. 27 type PositionedError struct { 28 Pos token.Pos 29 error 30 } 31 32 // ErrFromNode returns the given error, with additional information 33 // attaching it to the given AST node. It will automatically map 34 // over error lists. 35 func ErrFromNode(err error, node ast.Node) error { 36 if asList, isList := err.(ErrList); isList { 37 resList := make(ErrList, len(asList)) 38 for i, baseErr := range asList { 39 resList[i] = ErrFromNode(baseErr, node) 40 } 41 return resList 42 } 43 return PositionedError{ 44 Pos: node.Pos(), 45 error: err, 46 } 47 } 48 49 // MaybeErrList constructs an ErrList if the given list of 50 // errors has any errors, otherwise returning nil. 51 func MaybeErrList(errs []error) error { 52 if len(errs) == 0 { 53 return nil 54 } 55 return ErrList(errs) 56 } 57 58 // ErrList is a list of errors aggregated together into a single error. 59 type ErrList []error 60 61 func (l ErrList) Error() string { 62 return fmt.Sprintf("%v", []error(l)) 63 }