Skip to content

Commit ca326d3

Browse files
Fixed conditionals on retry handling
Fixes #290.
1 parent 2bd5a05 commit ca326d3

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

meraki/rest_session.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def user_agent_extended(be_geo_id, caller):
2525

2626
caller_string = f'Caller/({user_agent["caller"]})'
2727

28-
return json.dumps(caller_string)
28+
return caller_string
2929

3030

3131
# Main module interface
@@ -246,7 +246,10 @@ def handle_4xx_errors(self, metadata, operation, reason, response, retries, stat
246246
network_delete_concurrency_error_text = 'concurrent'
247247
action_batch_concurrency_error_text = 'executing batches'
248248

249+
# First we check for network deletion concurrency errors
249250
if operation == 'deleteNetwork' and response.status_code == 400:
251+
# message['errors'][0] is the first error, and it contains helpful text
252+
# here we use it to confirm that the 400 error is related to concurrent requests
250253
if network_delete_concurrency_error_text in message['errors'][0]:
251254
wait = random.randint(30, self._network_delete_retry_wait_time)
252255
if self._logger:
@@ -256,19 +259,20 @@ def handle_4xx_errors(self, metadata, operation, reason, response, retries, stat
256259
if retries == 0:
257260
raise APIError(metadata, response)
258261

259-
elif message_is_dict and 'errors' in message.keys():
260-
261-
action_batch_errors = [error for error in message['errors'] if action_batch_concurrency_error_text in error]
262-
263-
if action_batch_errors:
264-
wait = self._action_batch_retry_wait_time
265-
if self._logger:
266-
self._logger.warning(f'{tag}, {operation} - {status} {reason}, retrying in {wait} seconds')
267-
time.sleep(wait)
268-
retries -= 1
269-
if retries == 0:
270-
raise APIError(metadata, response)
262+
# Next we check for action batch concurrency errors
263+
# message['errors'][0] is the first error, and it contains helpful text
264+
# here we use it to confirm that the 400 error is related to concurrent requests
265+
elif (message_is_dict and 'errors' in message.keys() and action_batch_concurrency_error_text
266+
in message['errors'][0]):
267+
wait = self._action_batch_retry_wait_time
268+
if self._logger:
269+
self._logger.warning(f'{tag}, {operation} - {status} {reason}, retrying in {wait} seconds')
270+
time.sleep(wait)
271+
retries -= 1
272+
if retries == 0:
273+
raise APIError(metadata, response)
271274

275+
# Then we check if the user asked to retry other 4xx errors, based on their session config
272276
elif self._retry_4xx_error:
273277
wait = random.randint(1, self._retry_4xx_error_wait_time)
274278
if self._logger:
@@ -278,7 +282,7 @@ def handle_4xx_errors(self, metadata, operation, reason, response, retries, stat
278282
if retries == 0:
279283
raise APIError(metadata, response)
280284

281-
# All other client-side errors
285+
# All other client-side errors will raise an error
282286
else:
283287
if self._logger:
284288
self._logger.error(f'{tag}, {operation} - {status} {reason}, {message}')

0 commit comments

Comments
 (0)