decodeErrorContext returns input context for an error encountered during decoding. There is quite a bit of code here, but debugging faulty JSON is hard, so take extra care to produce nice error message, with good context information.
(err error, s string, pos int)
| 288 | // There is quite a bit of code here, but debugging faulty JSON is hard, so |
| 289 | // take extra care to produce nice error message, with good context information. |
| 290 | func decodeErrorContext(err error, s string, pos int) error { |
| 291 | if len(s) == 0 { |
| 292 | return errors.Wrap(err, "while decoding empty string") |
| 293 | } |
| 294 | |
| 295 | const contextSize = 16 |
| 296 | ctxStart := pos - contextSize |
| 297 | if ctxStart < 0 { |
| 298 | ctxStart = 0 |
| 299 | } |
| 300 | ctxEnd := pos + contextSize |
| 301 | if ctxEnd > len(s) { |
| 302 | ctxEnd = len(s) |
| 303 | } |
| 304 | |
| 305 | var leftPad, rightPad string |
| 306 | if pos > ctxStart { |
| 307 | leftPad = strings.Repeat(".", pos-ctxStart) |
| 308 | } |
| 309 | if ctxEnd > pos { |
| 310 | rightPad = strings.Repeat(".", ctxEnd-pos-1) |
| 311 | } |
| 312 | |
| 313 | return errors.Wrapf(err, |
| 314 | "while decoding %d bytes at offset %d:\n"+ |
| 315 | "...|%s|...\n"+ |
| 316 | "...|%s^%s|...", |
| 317 | len(s), pos, |
| 318 | s[ctxStart:ctxEnd], |
| 319 | leftPad, rightPad, |
| 320 | ) |
| 321 | } |
| 322 | |
| 323 | // unsafeGetBytes returns []byte in the underlying string, |
| 324 | // without incurring copy. |
no outgoing calls
no test coverage detected
searching dependent graphs…