Skip to content

Commit 96aeefc

Browse files
committed
handle requests exceptions gracefully
fixes #3890
1 parent befdea5 commit 96aeefc

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

tools/src/main/python/opengrok_tools/utils/commandsequence.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import logging
2525

26-
from requests.exceptions import HTTPError
26+
from requests.exceptions import RequestException
2727

2828
from .command import Command
2929
from .utils import is_web_uri
@@ -169,7 +169,7 @@ def run(self):
169169
call_rest_api(command, {PROJECT_SUBST: self.name,
170170
URL_SUBST: self.url},
171171
self.http_headers, self.api_timeout)
172-
except HTTPError as e:
172+
except RequestException as e:
173173
self.logger.error("RESTful command {} failed: {}".
174174
format(command, e))
175175
self.failed = True
@@ -227,7 +227,7 @@ def run_cleanup(self):
227227
call_rest_api(cleanup_cmd, {PROJECT_SUBST: self.name,
228228
URL_SUBST: self.url},
229229
self.http_headers, self.api_timeout)
230-
except HTTPError as e:
230+
except RequestException as e:
231231
self.logger.error("RESTful command {} failed: {}".
232232
format(cleanup_cmd, e))
233233
else:

tools/src/main/python/opengrok_tools/utils/mirror.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import urllib
3030
from tempfile import gettempdir
3131

32-
from requests.exceptions import HTTPError
32+
from requests.exceptions import RequestException
3333

3434
from .exitvals import (
3535
FAILURE_EXITVAL,
@@ -297,7 +297,7 @@ def process_changes(repos, project_name, uri, headers=None):
297297
logger.error('Unable to parse project \'{}\' indexed flag: {}'
298298
.format(project_name, e))
299299
return FAILURE_EXITVAL
300-
except HTTPError as e:
300+
except RequestException as e:
301301
logger.error('Unable to determine project \'{}\' indexed flag: {}'
302302
.format(project_name, e))
303303
return FAILURE_EXITVAL
@@ -366,7 +366,7 @@ def handle_disabled_project(config, project_name, disabled_msg, headers=None,
366366
try:
367367
call_rest_api(disabled_command, {PROJECT_SUBST: project_name},
368368
http_headers=headers, timeout=timeout)
369-
except HTTPError as e:
369+
except RequestException as e:
370370
logger.error("API call failed for disabled command of "
371371
"project '{}': {}".
372372
format(project_name, e))

tools/src/main/python/opengrok_tools/utils/opengrok.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#
2323

2424
import urllib.parse
25+
from requests.exceptions import RequestException
2526
from .webutil import get_uri
2627
from .restful import do_api_call
2728

@@ -42,7 +43,7 @@ def get_repos(logger, project, uri, headers=None, timeout=None):
4243
urllib.parse.quote_plus(project),
4344
'repositories'),
4445
headers=headers, timeout=timeout)
45-
except Exception as exception:
46+
except RequestException as exception:
4647
logger.error("could not get repositories for project '{}': {}".
4748
format(project, exception))
4849
return None
@@ -64,7 +65,7 @@ def get_config_value(logger, name, uri, headers=None, timeout=None):
6465
res = do_api_call('GET', get_uri(uri, 'api', 'v1', 'configuration',
6566
urllib.parse.quote_plus(name)),
6667
headers=headers, timeout=timeout)
67-
except Exception as exception:
68+
except RequestException as exception:
6869
logger.error("Cannot get the '{}' config value from the web "
6970
"application: {}".format(name, exception))
7071
return None
@@ -90,7 +91,7 @@ def set_config_value(logger, name, value, uri, headers=None, timeout=None):
9091
local_headers['Content-type'] = 'application/text'
9192
do_api_call('PUT', get_uri(uri, 'api', 'v1', 'configuration', name),
9293
data=value, headers=local_headers, timeout=timeout)
93-
except Exception as exception:
94+
except RequestException as exception:
9495
logger.error("Cannot set the '{}' config field to '{}' in the web "
9596
"application: {}".format(name, value, exception))
9697
return False
@@ -110,7 +111,7 @@ def get_repo_type(logger, repository, uri, headers=None, timeout=None):
110111
res = do_api_call('GET', get_uri(uri, 'api', 'v1', 'repositories',
111112
'property', 'type'), params=payload,
112113
headers=headers, timeout=timeout)
113-
except Exception as exception:
114+
except RequestException as exception:
114115
logger.error("could not get repository type for '{}' from web"
115116
"application: {}".format(repository, exception))
116117
return None
@@ -125,7 +126,7 @@ def get_configuration(logger, uri, headers=None, timeout=None):
125126
try:
126127
res = do_api_call('GET', get_uri(uri, 'api', 'v1', 'configuration'),
127128
headers=headers, timeout=timeout)
128-
except Exception as exception:
129+
except RequestException as exception:
129130
logger.error('could not get configuration from web application: {}'.
130131
format(exception))
131132
return None
@@ -140,7 +141,7 @@ def set_configuration(logger, configuration, uri, headers=None, timeout=None, ap
140141
if r is None or r.status_code != 201:
141142
logger.error('could not set configuration to web application')
142143
return False
143-
except Exception as exception:
144+
except RequestException as exception:
144145
logger.error('could not set configuration to web application: {}'.
145146
format(exception))
146147
return False
@@ -153,7 +154,7 @@ def list_projects(logger, uri, headers=None, timeout=None):
153154
res = do_api_call('GET',
154155
get_uri(uri, 'api', 'v1', 'projects'),
155156
headers=headers, timeout=timeout)
156-
except Exception as exception:
157+
except RequestException as exception:
157158
logger.error("could not list projects from web application: {}".
158159
format(exception))
159160
return None
@@ -166,7 +167,7 @@ def list_indexed_projects(logger, uri, headers=None, timeout=None):
166167
res = do_api_call('GET',
167168
get_uri(uri, 'api', 'v1', 'projects', 'indexed'),
168169
headers=headers, timeout=timeout)
169-
except Exception as exception:
170+
except RequestException as exception:
170171
logger.error("could not list indexed projects from web application: {}".
171172
format(exception))
172173
return None
@@ -181,7 +182,7 @@ def add_project(logger, project, uri, headers=None, timeout=None, api_timeout=No
181182
if r is None or r.status_code != 201:
182183
logger.error(f"could not add project '{project}' in web application")
183184
return False
184-
except Exception as exception:
185+
except RequestException as exception:
185186
logger.error("could not add project '{}' to web application: {}".
186187
format(project, exception))
187188
return False
@@ -197,7 +198,7 @@ def delete_project(logger, project, uri, headers=None, timeout=None, api_timeout
197198
if r is None or r.status_code != 204:
198199
logger.error(f"could not delete project '{project}' in web application")
199200
return False
200-
except Exception as exception:
201+
except RequestException as exception:
201202
logger.error("could not delete project '{}' in web application: {}".
202203
format(project, exception))
203204
return False

0 commit comments

Comments
 (0)