From 6675d5a20d16efb56b55a6037c84220a370633f0 Mon Sep 17 00:00:00 2001 From: Jonathan Labreuille Date: Wed, 3 Aug 2016 14:08:22 +0200 Subject: [PATCH] added default error messages to errorResponse and badRequest --- assets/js/src/ajax.js | 8 +------- lib/API/Endpoint.php | 11 ++++++++++- tests/unit/API/SettingsTest.php | 1 + views/settings.html | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/assets/js/src/ajax.js b/assets/js/src/ajax.js index c44a1bf10b..3878a3582a 100644 --- a/assets/js/src/ajax.js +++ b/assets/js/src/ajax.js @@ -60,13 +60,7 @@ define('ajax', ['mailpoet', 'jquery', 'underscore'], function(MailPoet, jQuery, ).then(function(data) { return data; }, function(xhr) { - if (!xhr.responseJSON) { - return { - errors: [{ error: 'undefined', message: "An unknown error occurred." }] - }; - } else { - return xhr.responseJSON; - } + return xhr.responseJSON; }); // clear options diff --git a/lib/API/Endpoint.php b/lib/API/Endpoint.php index f41ca3d9b2..98b406ff51 100644 --- a/lib/API/Endpoint.php +++ b/lib/API/Endpoint.php @@ -15,11 +15,20 @@ abstract class Endpoint { function errorResponse( $errors = array(), $meta = array(), $status = Response::STATUS_NOT_FOUND ) { - + if(empty($errors)) { + $errors = array( + 'unknown' => __('An unknown error occurred.') + ); + } return new ErrorResponse($errors, $meta, $status); } function badRequest($errors = array(), $meta = array()) { + if(empty($errors)) { + $errors = array( + 'bad_request' => __('Invalid request parameters.') + ); + } return new ErrorResponse($errors, $meta, Response::STATUS_BAD_REQUEST); } } \ No newline at end of file diff --git a/tests/unit/API/SettingsTest.php b/tests/unit/API/SettingsTest.php index 354cc8bf4f..c5b5c068f7 100644 --- a/tests/unit/API/SettingsTest.php +++ b/tests/unit/API/SettingsTest.php @@ -36,6 +36,7 @@ class SettingsTest extends MailPoetTest { $router = new Settings(); $response = $router->set(/* missing data */); + expect($response->errors[0]['error'])->equals('bad_request'); expect($response->status)->equals(Response::STATUS_BAD_REQUEST); $response = $router->set($new_settings); diff --git a/views/settings.html b/views/settings.html index 0f248f1003..33b6494b5c 100644 --- a/views/settings.html +++ b/views/settings.html @@ -110,7 +110,7 @@ $('.mailpoet_page_preview').on('click', function() { var selection = $(this).siblings('.mailpoet_page_selection'); - if(selection.length > 0) { + if (selection.length > 0) { $(this).attr('href', $(selection).find('option[value="'+$(selection).val()+'"]').data('preview-url')); $(this).attr('target', '_blank'); } else {