From 1bb7db9925ef9e5542664a219171470dc1dc001f Mon Sep 17 00:00:00 2001 From: John Hensley Date: Sun, 28 Jan 2018 09:08:23 -0500 Subject: [PATCH] Improve search error messages When a more specific error message is available from the search backend, show the user that instead of a blanket "There was an error, try searching again." In github.com/edx/edx-search, we're proposing removing quotes from the list of reserved characters in Elastic search terms. This enables searching for exact phrases, but also introduces the possibility of malformed queries. In those cases, it would be nice to give the user a hint that they need to change their query, instead of simply trying the same one again. --- lms/static/js/discovery/discovery_factory.js | 2 +- lms/static/js/discovery/models/search_state.js | 2 ++ lms/static/js/discovery/views/search_form.js | 8 ++++++-- .../course_search/js/collections/search_collection.js | 7 +++++-- .../static/course_search/js/views/search_results_view.js | 2 +- .../course_search/templates/search_error.underscore | 2 +- 6 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lms/static/js/discovery/discovery_factory.js b/lms/static/js/discovery/discovery_factory.js index 514b88aefd..91ce584766 100644 --- a/lms/static/js/discovery/discovery_factory.js +++ b/lms/static/js/discovery/discovery_factory.js @@ -72,7 +72,7 @@ }); dispatcher.listenTo(search, 'error', function() { - form.showErrorMessage(); + form.showErrorMessage(search); form.hideLoadingIndicator(); }); diff --git a/lms/static/js/discovery/models/search_state.js b/lms/static/js/discovery/models/search_state.js index 2bb270f4a7..cd37f938ea 100644 --- a/lms/static/js/discovery/models/search_state.js +++ b/lms/static/js/discovery/models/search_state.js @@ -72,10 +72,12 @@ reset: function() { this.discovery.reset(); this.page = 0; + this.errorMessage = ''; }, onError: function(collection, response, options) { if (response.statusText !== 'abort') { + this.errorMessage = response.responseJSON.error; this.trigger('error'); } }, diff --git a/lms/static/js/discovery/views/search_form.js b/lms/static/js/discovery/views/search_form.js index a9b17672a9..fbbb598980 100644 --- a/lms/static/js/discovery/views/search_form.js +++ b/lms/static/js/discovery/views/search_form.js @@ -60,8 +60,12 @@ this.clearSearch(); }, - showErrorMessage: function() { - this.$message.html(gettext('There was an error, try searching again.')); + showErrorMessage: function(search) { + if (search && search.errorMessage) { + this.$message.html(gettext(search.errorMessage)); + } else { + this.$message.html(gettext('There was an error, try searching again.')); + } } }); diff --git a/openedx/features/course_search/static/course_search/js/collections/search_collection.js b/openedx/features/course_search/static/course_search/js/collections/search_collection.js index 867dcb07eb..29dafc88e3 100644 --- a/openedx/features/course_search/static/course_search/js/collections/search_collection.js +++ b/openedx/features/course_search/static/course_search/js/collections/search_collection.js @@ -40,9 +40,11 @@ }, type: 'POST', success: function(self) { + self.errorMessage = ''; self.trigger('search'); }, - error: function(self) { + error: function(self, response) { + self.errorMessage = response.responseJSON.error; self.trigger('error'); } }); @@ -63,7 +65,8 @@ self.page += 1; // eslint-disable-line no-param-reassign self.trigger('next'); }, - error: function(self) { + error: function(self, response) { + self.errorMessage = response.responseJSON.error; self.trigger('error'); }, add: true, diff --git a/openedx/features/course_search/static/course_search/js/views/search_results_view.js b/openedx/features/course_search/static/course_search/js/views/search_results_view.js index 99d4f1a7e4..5d7bf188b1 100644 --- a/openedx/features/course_search/static/course_search/js/views/search_results_view.js +++ b/openedx/features/course_search/static/course_search/js/views/search_results_view.js @@ -93,7 +93,7 @@ }, showErrorMessage: function() { - HtmlUtils.setHtml(this.$el, HtmlUtils.template(this.errorTemplate)()); + HtmlUtils.setHtml(this.$el, HtmlUtils.template(this.errorTemplate)({errorMessage: this.collection.errorMessage})); this.showResults(); }, diff --git a/openedx/features/course_search/static/course_search/templates/search_error.underscore b/openedx/features/course_search/static/course_search/templates/search_error.underscore index 35990e2783..4d4dcbe497 100644 --- a/openedx/features/course_search/static/course_search/templates/search_error.underscore +++ b/openedx/features/course_search/static/course_search/templates/search_error.underscore @@ -1 +1 @@ -<%- gettext("There was an error, try searching again.") %> +<% if (errorMessage) { %><%- errorMessage %><%} else {%><%- gettext("There was an error, try searching again.") %><% } %>