From 3d51049932d1969bbd09aad2e85997633e2d8f2a Mon Sep 17 00:00:00 2001 From: btnmasher <67168637+btnmasher@users.noreply.github.com> Date: Wed, 30 Aug 2023 16:27:19 -0700 Subject: [PATCH] Add typed IteratorError --- iter.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/iter.go b/iter.go index 29b31cf7..66fc66f0 100644 --- a/iter.go +++ b/iter.go @@ -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{ @@ -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