Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 8026bc4

Browse files
Improve slicker error handling for blank files
Summary: Now we don't crash if there's an error in a blank file, but instead say so. We also don't crash if there's something else we can't handle. Test Plan: ran a slicker command that got me in this situation, got a nice error Reviewers: csilvers Reviewed By: csilvers Differential Revision: https://phabricator.khanacademy.org/D48163
1 parent 92f3803 commit 8026bc4

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

slicker/khodemod.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,16 @@ def handle_warnings(self, root, filename, warnings):
438438
% (warning.message, filename, lineno, line))
439439

440440
def handle_error(self, root, error):
441-
body = read_file(root, error.filename) or ''
442-
lineno, _ = pos_to_line_col(body, error.pos)
443-
line = body.splitlines()[lineno - 1]
444-
emit("ERROR:%s\n on %s:%s --> %s"
445-
% (error.message, error.filename, lineno, line))
441+
body = read_file(root, error.filename)
442+
if body:
443+
try:
444+
lineno, _ = pos_to_line_col(body, error.pos)
445+
line = body.splitlines()[lineno - 1]
446+
line_info = ":%s --> %s" % (lineno, line)
447+
except Exception:
448+
# Error error! Make sure not to crash so we still log it.
449+
line_info = " at invalid position %s" % error.pos
450+
else:
451+
line_info = " (empty)"
452+
emit("ERROR:%s\n on %s%s"
453+
% (error.message, error.filename, line_info))

0 commit comments

Comments
 (0)