From 91c53ba1845bc093282fcb4fa196f0e5c58381da Mon Sep 17 00:00:00 2001
From: Eva Bardou <ebardou@teklia.com>
Date: Sun, 26 Jul 2020 20:20:09 +0000
Subject: [PATCH] Improve API exceptions display when fetching element

---
 arkindex_worker/worker.py | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/arkindex_worker/worker.py b/arkindex_worker/worker.py
index 99971d3b..4319bb51 100644
--- a/arkindex_worker/worker.py
+++ b/arkindex_worker/worker.py
@@ -6,6 +6,8 @@ import os
 import sys
 import uuid
 
+from apistar.exceptions import ErrorResponse
+
 from arkindex import ArkindexClient, options_from_env
 from arkindex_worker import logger
 from arkindex_worker.models import Element
@@ -124,12 +126,18 @@ class ElementsWorker(BaseWorker):
                     **self.api_client.request("RetrieveElement", id=element_id)
                 )
                 logger.info(f"Processing {element} ({i}/{count})")
-
                 self.process_element(element)
+            except ErrorResponse as e:
+                failed += 1
+                logger.warning(
+                    f"An API error occurred while processing element {element_id}: {e.title} - {e.content}",
+                    exc_info=e if self.args.verbose else None,
+                )
+                self.report.error(element_id, e)
             except Exception as e:
                 failed += 1
                 logger.warning(
-                    "Failed running worker on {}: {!r}".format(element_id, e),
+                    f"Failed running worker on element {element_id}: {e}",
                     exc_info=e if self.args.verbose else None,
                 )
                 self.report.error(element_id, e)
-- 
GitLab