Skip to content

Commit 5068c8b

Browse files
committed
#183 limit error message size
1 parent 16f7860 commit 5068c8b

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

feature_iter.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,22 @@ func (iter *Iterator) ReportError(operation string, msg string) {
200200
if peekStart < 0 {
201201
peekStart = 0
202202
}
203-
iter.Error = fmt.Errorf("%s: %s, parsing %v ...%s... at %s", operation, msg, iter.head,
204-
string(iter.buf[peekStart:iter.head]), string(iter.buf[0:iter.tail]))
203+
peekEnd := iter.head + 10
204+
if peekEnd > iter.tail {
205+
peekEnd = iter.tail
206+
}
207+
parsing := string(iter.buf[peekStart:peekEnd])
208+
contextStart := iter.head - 50
209+
if contextStart < 0 {
210+
contextStart = 0
211+
}
212+
contextEnd := iter.head + 50
213+
if contextEnd > iter.tail {
214+
contextEnd = iter.tail
215+
}
216+
context := string(iter.buf[contextStart:contextEnd])
217+
iter.Error = fmt.Errorf("%s: %s, error found in #%v byte of ...|%s|..., bigger context ...|%s|...",
218+
operation, msg, iter.head - peekStart, parsing, context)
205219
}
206220

207221
// CurrentBuffer gets current buffer as string for debugging purpose
@@ -210,7 +224,7 @@ func (iter *Iterator) CurrentBuffer() string {
210224
if peekStart < 0 {
211225
peekStart = 0
212226
}
213-
return fmt.Sprintf("parsing %v ...|%s|... at %s", iter.head,
227+
return fmt.Sprintf("parsing #%v byte, around ...|%s|..., whole buffer ...|%s|...", iter.head,
214228
string(iter.buf[peekStart:iter.head]), string(iter.buf[0:iter.tail]))
215229
}
216230

0 commit comments

Comments
 (0)