@@ -37,16 +37,8 @@ def format_document(document, range=None):
37
37
38
38
try :
39
39
formatted_text = format_text (text = text , config = config )
40
- except (
41
- ValueError ,
40
+ except black .NothingChanged :
42
41
# 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
- ):
50
42
return []
51
43
52
44
return [{"range" : range , "newText" : formatted_text }]
@@ -60,11 +52,19 @@ def format_text(*, text, config):
60
52
string_normalization = not config ["skip_string_normalization" ],
61
53
)
62
54
try :
55
+ # will raise black.NothingChanged, we want to bubble that exception up
63
56
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 )
68
68
raise black .NothingChanged
69
69
70
70
0 commit comments