Skip to content

Commit

Permalink
Add GetUnderlying function to ErrorWithPos (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
bufdev authored and jhump committed Aug 2, 2019
1 parent e9bc1d6 commit 0a6fd46
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
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{}

0 comments on commit 0a6fd46

Please sign in to comment.