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