fargo: hide user-not-found API errors and only that (#35352)

This commit is contained in:
Benjamin Dauvergne 2020-03-22 12:07:14 +01:00
parent cd2fcfebab
commit 3baed95c28
1 changed files with 30 additions and 7 deletions

View File

@ -14,16 +14,22 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
from django.conf import settings
from django.db import models
from django.forms import models as model_forms
from django.forms import Select
from django.utils.translation import ugettext_lazy as _
from requests import RequestException, HTTPError
from combo.data.models import CellBase
from combo.data.library import register_cell_class
from combo.utils import requests
logger = logging.getLogger(__name__)
@register_cell_class
class RecentDocumentsCell(CellBase):
@ -63,14 +69,31 @@ class RecentDocumentsCell(CellBase):
return hasattr(settings, 'KNOWN_SERVICES') and settings.KNOWN_SERVICES.get('fargo')
def get_json(self, path, context):
remote_service = get_fargo_site(self.fargo_site)
response = requests.get(path,
remote_service=get_fargo_site(self.fargo_site),
user=self.get_concerned_user(context),
raise_if_not_cached=not(context.get('synchronous')),
headers={'accept': 'application/json'})
if response.status_code == 200:
user = self.get_concerned_user(context)
try:
response = requests.get(path,
remote_service=get_fargo_site(self.fargo_site),
user=user,
raise_if_not_cached=not(context.get('synchronous')),
headers={'accept': 'application/json'},
log_errors=True)
response.raise_for_status()
return response.json()
except HTTPError as e:
status_code = e.response.status_code
try:
content = e.response.json()
except Exception:
err = None
else:
err = content.get('err') if isinstance(content, dict) else None
if status_code == 401 and err == 'user-not-found':
log = logger.info
else:
log = logger.error
log('could not retrieve recent documents for user %s: status-code=%s err=%s', user, status_code, err)
except RequestException as e:
logger.error('could not retrieve recent documents for user %s: %s', user, e)
return {}
def render(self, context):