Skip to content

Commit c6879cc

Browse files
committed
Refactor handling of exceptions to format_text
1 parent e402ce2 commit c6879cc

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

pylsp_black/plugin.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,8 @@ def format_document(document, range=None):
3737

3838
try:
3939
formatted_text = format_text(text=text, config=config)
40-
except (
41-
ValueError,
40+
except black.NothingChanged:
4241
# raised when the file is already formatted correctly
43-
black.NothingChanged,
44-
# raised when the file being formatted has an indentation error
45-
IndentationError,
46-
# raised when black produces invalid Python code or formats the file
47-
# differently on the second pass
48-
AssertionError,
49-
):
5042
return []
5143

5244
return [{"range": range, "newText": formatted_text}]
@@ -60,11 +52,19 @@ def format_text(*, text, config):
6052
string_normalization=not config["skip_string_normalization"],
6153
)
6254
try:
55+
# will raise black.NothingChanged, we want to bubble that exception up
6356
return black.format_file_contents(text, fast=config["fast"], mode=mode)
64-
except black.NothingChanged:
65-
raise
66-
except Exception as e:
67-
logger.error("Error formatting with black: %s", e.message)
57+
except (
58+
# raised when the file has syntax errors
59+
ValueError,
60+
# raised when the file being formatted has an indentation error
61+
IndentationError,
62+
# raised when black produces invalid Python code or formats the file
63+
# differently on the second pass
64+
AssertionError,
65+
) as e:
66+
# errors will show on lsp stderr stream
67+
logger.error("Error formatting with black: %s", e)
6868
raise black.NothingChanged
6969

7070

0 commit comments

Comments
 (0)