From e9b8c63b123be544d05761f1a85e737a1f218c8a Mon Sep 17 00:00:00 2001 From: Denis Chenu Date: Tue, 13 Jun 2017 16:49:44 +0200 Subject: [PATCH] Dev: Better Exception handling for ajax Dev: think we can use same function for all ajax error Dev: when ajax call return a clean error. Dev: We don't have already a LS.lang object ? Dev: Can be call by other tools : Directly --- .../admin/NotificationController.php | 37 ++++++--------- .../extensions/LimeScript/LimeScript.php | 1 + .../views/admin/super/admin_notifications.php | 9 ++++ scripts/admin/notifications.js | 46 +++++++++---------- 4 files changed, 46 insertions(+), 47 deletions(-) diff --git a/application/controllers/admin/NotificationController.php b/application/controllers/admin/NotificationController.php index b074b915d27..0173dc035aa 100644 --- a/application/controllers/admin/NotificationController.php +++ b/application/controllers/admin/NotificationController.php @@ -28,20 +28,15 @@ public function index() public function getNotificationAsJSON($notId) { $this->checkPermission(); - - header('Content-type: application/json'); - if ((string) (int) $notId !== (string) $notId) { - echo json_encode(array('error' => 'Invalid notification id')); - Yii::app()->end(); + } $not = Notification::model()->findByPk($notId); - - if ($not) { - echo json_encode(array('result' => $not->getAttributes())); - } else { - echo json_encode(array('error' => 'Found no notification with id ' . (int) $notId)); + if(!$not) { + throw new CHttpException(404,sprintf(gT("Notification %s not found"),$notId)); } + header('Content-type: application/json'); + echo json_encode(array('result' => $not->getAttributes())); } /** @@ -54,20 +49,16 @@ public function notificationRead($notId) { $this->checkPermission(); - header('Content-type: application/json'); - if ((string) (int) $notId !== (string) $notId) { - echo json_encode(array('error' => 'Invalid notification id')); - Yii::app()->end(); + throw new CHttpException(403,gT("Invalid notification id")); } - - try { - $not = Notification::model()->findByPk($notId); - $result = $not->markAsRead(); - echo json_encode(array('result' => $result)); - } catch (Exception $ex) { - echo json_encode(array('error' => $ex->getMessage())); + $not = Notification::model()->findByPk($notId); + if(!$not) { + throw new CHttpException(404,sprintf(gT("Notification %s not found"),$notId)); } + $result = $not->markAsRead(); + header('Content-type: application/json'); + echo json_encode(array('result' => $result)); } /** @@ -79,7 +70,6 @@ public function notificationRead($notId) public function actionGetMenuWidget($surveyId = null, $showLoader = false) { $this->checkPermission(); - echo self::getMenuWidget($surveyId, $showLoader); } @@ -111,8 +101,7 @@ protected function checkPermission() { // Abort if user is not logged in if (Yii::app()->user->isGuest) { - echo 'No permission'; - Yii::app()->end(); + throw new CHttpException(401); } } diff --git a/application/extensions/LimeScript/LimeScript.php b/application/extensions/LimeScript/LimeScript.php index aeba5a0c61f..fc08c116480 100644 --- a/application/extensions/LimeScript/LimeScript.php +++ b/application/extensions/LimeScript/LimeScript.php @@ -19,6 +19,7 @@ public function run() $data['replacementFields']['path'] = App()->createUrl("admin/limereplacementfields/sa/index/"); $json = json_encode($data, JSON_FORCE_OBJECT); $script = "LS.data = $json;\n" + . "LS.lang = {};\n" . "$.ajaxSetup({data: {YII_CSRF_TOKEN: LS.data.csrfToken}});"; App()->getClientScript()->registerScript('LimeScript', $script, CClientScript::POS_HEAD); } diff --git a/application/views/admin/super/admin_notifications.php b/application/views/admin/super/admin_notifications.php index efd722bb3f2..b5f247dc545 100644 --- a/application/views/admin/super/admin_notifications.php +++ b/application/views/admin/super/admin_notifications.php @@ -81,3 +81,12 @@ class='admin-notification-link' + gT("Error : %s"), + 'errorUnknow' => gT("unknown"), + 'unknowText' => gT("An unknown error occurred"), + ); + $script = "LS.lang = $.extend(LS.lang,".json_encode($notificationLanguageString).")\n;"; + Yii::app()->getClientScript()->registerScript('notificationLanguageString',$script,CClientScript::POS_HEAD); +?> diff --git a/scripts/admin/notifications.js b/scripts/admin/notifications.js index 824230c632e..0e6ad2dd053 100644 --- a/scripts/admin/notifications.js +++ b/scripts/admin/notifications.js @@ -36,13 +36,12 @@ $(document).ready(function() { method: 'GET', success: function (response) { $('#notification-li').replaceWith(response); - // Re-bind onclick initNotification(); - // Adapt style to window size styleNotificationMenu(); - } + }, + error: showError }); } // Called from outside (update notifications when click @@ -66,9 +65,11 @@ $(document).ready(function() { $.ajax({ url: $(that).data('read-url'), method: 'GET', - }).done(function(response) { - // Fetch new HTML for menu widget - updateNotificationWidget($(that).data('update-url')); + success : function(response) { + // Fetch new HTML for menu widget + updateNotificationWidget($(that).data('update-url')); + }, + error: showError }); } @@ -86,19 +87,6 @@ $(document).ready(function() { method: 'GET', dataType: 'json', success : function(response) { - if(response.error) { - $('#admin-notification-modal .modal-title').html("Error"); - $('#admin-notification-modal .modal-body-text').html(response.error); - $('#admin-notification-modal .modal-content').addClass('panel-danger'); - //$('#admin-notification-modal .notification-date').html(not.created.substr(0, 16)); - $('#admin-notification-modal').modal(); - $('#admin-notification-modal').unbind('hidden.bs.modal'); - $('#admin-notification-modal').on('hidden.bs.modal', function(e) { - notificationIsRead(that); - $('#admin-notification-modal .modal-content').removeClass('panel-danger'); - }); - return; - } var not = response.result; $('#admin-notification-modal .modal-title').html(not.title); $('#admin-notification-modal .modal-body-text').html(not.message); @@ -113,9 +101,7 @@ $(document).ready(function() { $('#admin-notification-modal .modal-content').removeClass('panel-' + not.display_class); }); }, - error: function(response) { - log(response); - } + error: showError }); } @@ -174,7 +160,8 @@ $(document).ready(function() { method: 'GET', success: function (response) { log('response', response); - } + }, + error: showError }).then(function() { updateNotificationWidget(updateUrl); }); @@ -183,4 +170,17 @@ $(document).ready(function() { initNotification(); + function showError(response) { + var status= response.status || LS.lang.errorUnknow; + var responseText= response.responseText || LS.lang.unknowText; + $('#admin-notification-modal .modal-title').html(LS.lang.errorTitle.replace("%s",status)); + $('#admin-notification-modal .modal-body-text').html(responseText); + $('#admin-notification-modal .modal-content').addClass('panel-danger'); + //$('#admin-notification-modal .notification-date').html(not.created.substr(0, 16)); + $('#admin-notification-modal').modal(); + $('#admin-notification-modal').unbind('hidden.bs.modal'); + $('#admin-notification-modal').on('hidden.bs.modal', function(e) { + $('#admin-notification-modal .modal-content').removeClass('panel-danger'); + }); + } });