Created
January 19, 2023 12:39
-
-
Save willmerae/6e9837929bbb26e6fed83b1523e9c665 to your computer and use it in GitHub Desktop.
Extremely bare bones JSONDecodeError snippet renderer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"foo": [ | |
1, | |
2, | |
3, | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import sys | |
with open(sys.argv[1]) as f: | |
try: | |
doc = json.load(f) | |
except json.JSONDecodeError as exc: | |
print(f'JSON error: {exc.args[0]}', file=sys.stderr) | |
snippet_start = exc.doc.rfind('\n', 0, exc.pos-exc.colno) | |
snippet_end = exc.doc.find('\n', exc.pos) | |
print(exc.doc[snippet_start:snippet_end], file=sys.stderr) | |
print((exc.colno-1)*'~' + '^', file=sys.stderr) | |
sys.exit(1) |
Author
willmerae
commented
Jan 19, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment