Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetUnderlying function to ErrorWithPos #260

Merged
merged 1 commit into from Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion desc/protoparse/error_reporting_test.go
Expand Up @@ -190,7 +190,10 @@ func TestErrorReporting(t *testing.T) {
testutil.Eq(t, len(tc.expectedErrs), count, "case #%d: parse should have called reporter %d times", i+1, len(tc.expectedErrs))
testutil.Eq(t, len(tc.expectedErrs), len(reported), "case #%d: wrong number of errors reported", i+1)
for j := range tc.expectedErrs {
testutil.Require(t, strings.Contains(reported[j].Error(), tc.expectedErrs[j]), "case #%d: parse error[%d] have %q; instead got %q", i+1, j, tc.expectedErrs[j], reported[j].Error())
testutil.Eq(t, tc.expectedErrs[j], reported[j].Error(), "case #%d: parse error[%d] have %q; instead got %q", i+1, j, tc.expectedErrs[j], reported[j].Error())
split := strings.SplitN(tc.expectedErrs[j], ":", 4)
testutil.Eq(t, 4, len(split), "case #%d: expected %q [%d] to contain at least 4 elements split by :", i+1, tc.expectedErrs[j], j)
testutil.Eq(t, split[3], " "+reported[j].GetUnderlying().Error(), "case #%d: parse error underlying[%d] have %q; instead got %q", i+1, j, split[3], reported[j].GetUnderlying().Error())
}

count = 0
Expand Down
9 changes: 9 additions & 0 deletions desc/protoparse/errors.go
Expand Up @@ -58,9 +58,12 @@ func (h *errorHandler) getError() error {

// ErrorWithPos is an error about a proto source file that includes information
// about the location in the file that caused the error.
//
// The value of Error() will contain both the SourcePos and Underlying error.
type ErrorWithPos interface {
error
GetPosition() SourcePos
GetUnderlying() error
}

// ErrorWithSourcePos is an error about a proto source file that includes
Expand Down Expand Up @@ -90,4 +93,10 @@ func (e ErrorWithSourcePos) GetPosition() SourcePos {
return *e.Pos
}

// GetUnderlying implements the ErrorWithPos interface, supplying the
// underlying error. This error will not include location information.
func (e ErrorWithSourcePos) GetUnderlying() error {
return e.Underlying
}

var _ ErrorWithPos = ErrorWithSourcePos{}