Skip to content

Commit 292dc24

Browse files
committed
Handle timeout gracefully
1 parent 971bd31 commit 292dc24

2 files changed

Lines changed: 36 additions & 31 deletions

File tree

‎language_check/__init__.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,9 @@ def _get_root(cls, url, data=None, num_tries=2):
299299
with urlopen(url, data, cls._TIMEOUT) as f:
300300
return ElementTree.parse(f).getroot()
301301
except (IOError, http.client.HTTPException) as e:
302-
if n + 1 < num_tries:
303-
cls._start_server()
304-
else:
302+
cls._terminate_server()
303+
cls._start_server()
304+
if n + 1 >= num_tries:
305305
raise Error('{}: {}'.format(cls._url, e))
306306

307307
@classmethod

‎language_check/main.py‎

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from . import __version__
99
from . import get_build_date
1010
from . import get_version
11+
from . import Error
1112
from . import LanguageTool
1213

1314

@@ -116,33 +117,37 @@ def main():
116117
lang_tool.disabled.update(args.disable)
117118
lang_tool.enabled.update(args.enable)
118119

119-
if args.api:
120-
print(lang_tool._check_api(text).decode())
121-
elif args.apply:
122-
print(lang_tool.correct(text))
123-
else:
124-
for match in lang_tool.check(text):
125-
rule_id = match.ruleId
126-
if match.subId is not None:
127-
rule_id += '[{}]'.format(match.subId)
128-
129-
replacement_text = ', '.join(
130-
"'{}'".format(word)
131-
for word in match.replacements).strip()
132-
133-
message = match.msg
134-
135-
# Messages that end with punctuation already include the
136-
# suggestion.
137-
if replacement_text and not message.endswith(('.', '?')):
138-
message += '; suggestions: ' + replacement_text
139-
140-
print('{}:{}:{}: {}: {}'.format(
141-
filename,
142-
match.fromy + 1,
143-
match.fromx + 1,
144-
rule_id,
145-
message))
146-
status = 2
120+
try:
121+
if args.api:
122+
print(lang_tool._check_api(text).decode())
123+
elif args.apply:
124+
print(lang_tool.correct(text))
125+
else:
126+
for match in lang_tool.check(text):
127+
rule_id = match.ruleId
128+
if match.subId is not None:
129+
rule_id += '[{}]'.format(match.subId)
130+
131+
replacement_text = ', '.join(
132+
"'{}'".format(word)
133+
for word in match.replacements).strip()
134+
135+
message = match.msg
136+
137+
# Messages that end with punctuation already include the
138+
# suggestion.
139+
if replacement_text and not message.endswith(('.', '?')):
140+
message += '; suggestions: ' + replacement_text
141+
142+
print('{}:{}:{}: {}: {}'.format(
143+
filename,
144+
match.fromy + 1,
145+
match.fromx + 1,
146+
rule_id,
147+
message))
148+
status = 2
149+
except Error as exception:
150+
print('{}: {}'.format(filename, exception), file=sys.stderr)
151+
continue
147152

148153
return status

0 commit comments

Comments
 (0)