From 5c2aa09db4b7b99718f78362a5eb97b4507fa137 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Mon, 23 Mar 2020 19:27:00 -0700 Subject: [PATCH] Let tenacity.retry look for our EAGAIN instead of plain error. --- src/mega/mega.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/mega/mega.py b/src/mega/mega.py index 479348b..cd9b447 100644 --- a/src/mega/mega.py +++ b/src/mega/mega.py @@ -17,6 +17,7 @@ import shutil import requests from tenacity import retry, wait_exponential, retry_if_exception_type +from . import errors from .errors import ValidationError, RequestError, error_for_code from .crypto import ( a32_to_base64, encrypt_key, base64_url_encode, encrypt_attr, base64_to_a32, @@ -43,7 +44,7 @@ class Mega: self.options = options @retry( - retry=retry_if_exception_type(RuntimeError), + retry=retry_if_exception_type(errors.EAGAIN), wait=wait_exponential(multiplier=2, min=2, max=60) ) def _api_request(self, data, params={}): @@ -67,11 +68,10 @@ class Mega: timeout=self.timeout, ) json_resp = json.loads(req.text) + if isinstance(json_resp, list) and isinstance(json_resp[0], int): + json_resp = json_resp[0] if isinstance(json_resp, int): - if json_resp == -3: - msg = 'Request failed, retrying' - logger.info(msg) - raise RuntimeError(msg) + # If this raises EAGAIN it'll be caught by tenacity retry. raise error_for_code(json_resp) return json_resp[0]