From 46844fd6d831e2d5895cfbdb736e53a7d6c989b2 Mon Sep 17 00:00:00 2001 From: Denis Dillmann Date: Sat, 21 Jan 2023 11:43:53 +0100 Subject: [PATCH] Provide error handling for route "/check-class-has-data-quality/{id}" instead of incorrect "400 Bad Request" http status code --- src/Controller/DataQualityController.php | 19 ++++- src/Resources/public/js/DataQualityBundle.js | 77 +++++++++++--------- 2 files changed, 60 insertions(+), 36 deletions(-) diff --git a/src/Controller/DataQualityController.php b/src/Controller/DataQualityController.php index 43593b5..f36c94d 100644 --- a/src/Controller/DataQualityController.php +++ b/src/Controller/DataQualityController.php @@ -90,9 +90,24 @@ public function checkClassHasDataQuality(int $id): JsonResponse throw new DataQualityException('class has no data quality.'); } - return new JsonResponse(['message' => 'ok'], 200); + return new JsonResponse([ + 'result' => ['message' => 'ok'], + 'error' => null, + ], 200); + } catch (Exception $exception) { - return new JsonResponse(['message' => $exception->getMessage()], 400); + + if ($exception instanceof DataQualityException) { + return new JsonResponse([ + 'result' => null, + 'error' => [ + 'message' => $exception->getMessage(), + 'code' => $exception->getCode() + ], + ], 200); + } + + return new JsonResponse(['message' => $exception->getMessage()], 500); } } } diff --git a/src/Resources/public/js/DataQualityBundle.js b/src/Resources/public/js/DataQualityBundle.js index e77a76c..cc1fdc5 100644 --- a/src/Resources/public/js/DataQualityBundle.js +++ b/src/Resources/public/js/DataQualityBundle.js @@ -23,41 +23,50 @@ pimcore.plugin.DataQualityBundle = Class.create(pimcore.plugin.admin, { if (object.data.general.o_classId !== 'DQC' && object.data.general.o_type === 'object') { fetch('/admin/data-quality/check-class-has-data-quality/' + object.id) .then((response) => { - if (response.status === 200) { - var path = '/admin/data-quality/index/' + object.id; - var html = ''; - - this.tab = Ext.create('Ext.panel.Panel', { - border: false, - autoScroll: true, - closable: false, - iconCls: 'pimcore_icon_charty', - bodyCls: 'pimcore_overflow_scrolling', - html: html, - tbar: { - items: [ - '->', // Fill - { - xtype: 'button', - text: t('refresh'), - iconCls: 'pimcore_icon_reload', - handler: function () { - var key = "object_" + object.id; - if (pimcore.globalmanager.exists(key)) { - var objectTab = pimcore.globalmanager.get(key); - objectTab.saveToSession(function () { - this.tab.setHtml(html); - }.bind(this)); - } - }.bind(this) - } - ] - } - }); - // this adds the pimcore tab - object.tabbar.add([this.tab]); - pimcore.layout.refresh(); + if (response.ok) { + return response.json(); + } else { + throw new Error(response.statusText); + } + }) + .then((json) => { + if (json.error) { + throw new Error(json.error.message); } + + var path = '/admin/data-quality/index/' + object.id; + var html = ''; + + this.tab = Ext.create('Ext.panel.Panel', { + border: false, + autoScroll: true, + closable: false, + iconCls: 'pimcore_icon_charty', + bodyCls: 'pimcore_overflow_scrolling', + html: html, + tbar: { + items: [ + '->', // Fill + { + xtype: 'button', + text: t('refresh'), + iconCls: 'pimcore_icon_reload', + handler: function () { + var key = "object_" + object.id; + if (pimcore.globalmanager.exists(key)) { + var objectTab = pimcore.globalmanager.get(key); + objectTab.saveToSession(function () { + this.tab.setHtml(html); + }.bind(this)); + } + }.bind(this) + } + ] + } + }); + // this adds the pimcore tab + object.tabbar.add([this.tab]); + pimcore.layout.refresh(); }) .catch(() => { // do nothing and show nothing