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 typed IteratorError #685

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 20 additions & 2 deletions iter.go
Expand Up @@ -93,6 +93,19 @@ func NewIterator(cfg API) *Iterator {
}
}

// An IteratorError is a typed description of an Iterator error with additional context fields about the error
type IteratorError struct {
Offset int
Operation string
Msg string
Peek string
Context string
}

func (e *IteratorError) Error() string {
return fmt.Sprintf("%s: %s, error found in #%v byte of ...|%s|..., bigger context ...|%s|...", e.Operation, e.Msg, e.Offset, e.Peek, e.Context)
}

// Parse creates an Iterator instance from io.Reader
func Parse(cfg API, reader io.Reader, bufSize int) *Iterator {
return &Iterator{
Expand Down Expand Up @@ -221,8 +234,13 @@ func (iter *Iterator) ReportError(operation string, msg string) {
contextEnd = iter.tail
}
context := string(iter.buf[contextStart:contextEnd])
iter.Error = fmt.Errorf("%s: %s, error found in #%v byte of ...|%s|..., bigger context ...|%s|...",
operation, msg, iter.head-peekStart, parsing, context)
iter.Error = &IteratorError{
Offset: iter.head - peekStart,
Operation: operation,
Msg: msg,
Peek: parsing,
Context: context,
}
}

// CurrentBuffer gets current buffer as string for debugging purpose
Expand Down