From 349a3aaf8e279c044e87f91abed8fea81af84a9b Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 19 Apr 2021 23:40:06 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Handle=20errors=20when=20syncing?= =?UTF-8?q?=20DataFlows?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/sophon/core/admin.py | 21 ++++++++++++++++++--- backend/sophon/core/views.py | 13 ++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/backend/sophon/core/admin.py b/backend/sophon/core/admin.py index f9f8eec..587920d 100644 --- a/backend/sophon/core/admin.py +++ b/backend/sophon/core/admin.py @@ -1,4 +1,4 @@ -from django.contrib import admin +from django.contrib import admin, messages from . import models @@ -25,11 +25,26 @@ class ProjectAdmin(CoreAdmin): ) -@admin.action(description="Syncronize DataFlows (slow, be patient!)") +@admin.action(description="Sync DataFlows") def sync_flows_admin(modeladmin, request, queryset): for datasource in queryset: datasource: models.DataSource - datasource.sync_flows() + try: + datasource.sync_flows() + except NotImplementedError: + modeladmin.message_user( + request, + f"Skipped {datasource}: Syncing DataFlows is not supported on this DataSource.", + level=messages.ERROR + ) + except Exception as exc: + modeladmin.message_user( + request, + f"Skipped {datasource}: {exc}", + level=messages.ERROR + ) + else: + modeladmin.log_change(request, datasource, "Sync DataFlows") @admin.register(models.DataSource) diff --git a/backend/sophon/core/views.py b/backend/sophon/core/views.py index fa0b868..1a27f50 100644 --- a/backend/sophon/core/views.py +++ b/backend/sophon/core/views.py @@ -68,7 +68,18 @@ class DataSourceViewSet(viewsets.ModelViewSet): log.debug(f"Getting DataSource from the database...") db_datasource: models.DataSource = self.get_object() - db_datasource.sync_flows() + try: + db_datasource.sync_flows() + except NotImplementedError: + return response.Response({ + "success": False, + "error": "Syncing DataFlows is not supported on this DataSource." + }) + except Exception as exc: + return response.Response({ + "success": False, + "error": f"{exc}" + }) return response.Response({ "success": True,