github.com/jhump/protoreflect@v1.16.0/desc/protoparse/errors.go (about) 1 package protoparse 2 3 import ( 4 "errors" 5 "fmt" 6 7 "github.com/bufbuild/protocompile/linker" 8 "github.com/bufbuild/protocompile/parser" 9 "github.com/bufbuild/protocompile/reporter" 10 11 "github.com/jhump/protoreflect/desc/protoparse/ast" 12 ) 13 14 // SourcePos is the same as ast.SourcePos. This alias exists for 15 // backwards compatibility (SourcePos used to be defined in this package.) 16 type SourcePos = ast.SourcePos 17 18 // ErrInvalidSource is a sentinel error that is returned by calls to 19 // Parser.ParseFiles and Parser.ParseFilesButDoNotLink in the event that syntax 20 // or link errors are encountered, but the parser's configured ErrorReporter 21 // always returns nil. 22 var ErrInvalidSource = reporter.ErrInvalidSource 23 24 // ErrNoSyntax is a sentinel error that may be passed to a warning reporter. 25 // The error the reporter receives will be wrapped with source position that 26 // indicates the file that had no syntax statement. 27 var ErrNoSyntax = parser.ErrNoSyntax 28 29 // ErrLookupImportAndProtoSet is the error returned if both LookupImport and LookupImportProto are set. 30 // 31 // Deprecated: This error is no longer used. It is now legal to set both LookupImport and LookupImportProto 32 // fields on the Parser. 33 var ErrLookupImportAndProtoSet = errors.New("both LookupImport and LookupImportProto set") 34 35 // ErrorReporter is responsible for reporting the given error. If the reporter 36 // returns a non-nil error, parsing/linking will abort with that error. If the 37 // reporter returns nil, parsing will continue, allowing the parser to try to 38 // report as many syntax and/or link errors as it can find. 39 type ErrorReporter = reporter.ErrorReporter 40 41 // WarningReporter is responsible for reporting the given warning. This is used 42 // for indicating non-error messages to the calling program for things that do 43 // not cause the parse to fail but are considered bad practice. Though they are 44 // just warnings, the details are supplied to the reporter via an error type. 45 type WarningReporter = reporter.WarningReporter 46 47 // ErrorWithPos is an error about a proto source file that includes information 48 // about the location in the file that caused the error. 49 // 50 // The value of Error() will contain both the SourcePos and Underlying error. 51 // The value of Unwrap() will only be the Underlying error. 52 type ErrorWithPos = reporter.ErrorWithPos 53 54 // ErrorWithSourcePos is an error about a proto source file that includes 55 // information about the location in the file that caused the error. 56 // 57 // Errors that include source location information *might* be of this type. 58 // However, calling code that is trying to examine errors with location info 59 // should instead look for instances of the ErrorWithPos interface, which 60 // will find other kinds of errors. This type is only exported for backwards 61 // compatibility. 62 // 63 // SourcePos should always be set and never nil. 64 type ErrorWithSourcePos struct { 65 // These fields are present and exported for backwards-compatibility 66 // with v1.4 and earlier. 67 Underlying error 68 Pos *SourcePos 69 70 reporter.ErrorWithPos 71 } 72 73 // Error implements the error interface 74 func (e ErrorWithSourcePos) Error() string { 75 sourcePos := e.GetPosition() 76 return fmt.Sprintf("%s: %v", sourcePos, e.Underlying) 77 } 78 79 // GetPosition implements the ErrorWithPos interface, supplying a location in 80 // proto source that caused the error. 81 func (e ErrorWithSourcePos) GetPosition() SourcePos { 82 if e.Pos == nil { 83 return SourcePos{Filename: "<input>"} 84 } 85 return *e.Pos 86 } 87 88 // Unwrap implements the ErrorWithPos interface, supplying the underlying 89 // error. This error will not include location information. 90 func (e ErrorWithSourcePos) Unwrap() error { 91 return e.Underlying 92 } 93 94 var _ ErrorWithPos = ErrorWithSourcePos{} 95 96 func toErrorWithSourcePos(err ErrorWithPos) ErrorWithPos { 97 pos := err.GetPosition() 98 return ErrorWithSourcePos{ 99 ErrorWithPos: err, 100 Underlying: err.Unwrap(), 101 Pos: &pos, 102 } 103 } 104 105 // ErrorUnusedImport may be passed to a warning reporter when an unused 106 // import is detected. The error the reporter receives will be wrapped 107 // with source position that indicates the file and line where the import 108 // statement appeared. 109 type ErrorUnusedImport = linker.ErrorUnusedImport 110 111 type errorWithFilename struct { 112 underlying error 113 filename string 114 } 115 116 func (e errorWithFilename) Error() string { 117 return fmt.Sprintf("%s: %v", e.filename, e.underlying) 118 } 119 120 func (e errorWithFilename) Unwrap() error { 121 return e.underlying 122 }