Permalink
Browse files

add errorHandler, closes #31

  • Loading branch information...
1 parent bbbdf96 commit 96166a2d9589c76baf0bac2b1f0c6c4e3680371e @jpmckinney jpmckinney committed Apr 11, 2013
Showing with 25 additions and 8 deletions.
  1. +13 −3 core/AbstractManager.js
  2. +1 −1 core/Core.js
  3. +11 −4 managers/Manager.jquery.js
View
@@ -154,16 +154,17 @@ AjaxSolr.AbstractManager = AjaxSolr.Class.extend(
* An abstract hook for child implementations.
*
* <p>Sends the request to Solr, i.e. to <code>this.solrUrl</code> or <code>
- * this.proxyUrl</code>, and receives Solr's response. It should send <code>
- * this.store.string()</code> as the Solr query, and it should pass Solr's
+ * this.proxyUrl</code>, and receives Solr's response. It should pass Solr's
* response to <code>handleResponse()</code> for handling.</p>
*
* <p>See <tt>managers/Manager.jquery.js</tt> for a jQuery implementation.</p>
*
* @param {String} servlet The Solr servlet to send the request to.
+ * @param {String} string The query string of the request. If not set, it
+ * should default to <code>this.store.string()</code>
* @throws If not defined in child implementation.
*/
- executeRequest: function (servlet) {
+ executeRequest: function (servlet, string) {
throw 'Abstract method executeRequest must be overridden in a subclass.';
},
@@ -179,5 +180,14 @@ AjaxSolr.AbstractManager = AjaxSolr.Class.extend(
for (var widgetId in this.widgets) {
this.widgets[widgetId].afterRequest();
}
+ },
+
+ /**
+ * This method is executed if Solr encounters an error.
+ *
+ * @param {String} message An error message.
+ */
+ handleError: function (message) {
+ window.console && console.log && console.log(message);
}
});
View
@@ -185,7 +185,7 @@ AjaxSolr.isString = function (obj) {
*/
AjaxSolr.theme = function (func) {
if (AjaxSolr.theme[func] || AjaxSolr.theme.prototype[func] == undefined) {
- console.log('Theme function "' + func + '" is not defined.');
+ window.console && console.log && console.log('Theme function "' + func + '" is not defined.');
}
else {
for (var i = 1, args = []; i < arguments.length; i++) {
View
@@ -8,17 +8,24 @@
AjaxSolr.Manager = AjaxSolr.AbstractManager.extend(
/** @lends AjaxSolr.Manager.prototype */
{
- executeRequest: function (servlet, string, handler) {
- var self = this;
+ executeRequest: function (servlet, string, handler, errorHandler) {
+ var self = this,
+ options = {dataType: 'json'};
string = string || this.store.string();
handler = handler || function (data) {
self.handleResponse(data);
};
+ errorHandler = errorHandler || function (jqXHR, textStatus, errorThrown) {
+ self.handleError(textStatus + ', ' + errorThrown);
+ };
if (this.proxyUrl) {
- jQuery.post(this.proxyUrl, { query: string }, handler, 'json');
+ options.url = this.proxyUrl;
+ options.data: {query: string};
+ options.type = 'POST';
}
else {
- jQuery.getJSON(this.solrUrl + servlet + '?' + string + '&wt=json&json.wrf=?', {}, handler);
+ options.url = this.solrUrl + servlet + '?' + string + '&wt=json&json.wrf=?';
}
+ jQuery.ajax(options).done(handler).fail(errorHandler);
}
});

6 comments on commit 96166a2

Thanks for the solution JP. One more "open ended" question; Solr is written in Java, how does javascript work against all the java classes that make up Solr? what books or websites would you recommend I look at to learn how to do this? I don't want to have to keep asking Q's here when I'm stumped...which is just about all the time :/

Contributor

jpmckinney replied Apr 25, 2013

Hmm, not sure what books to recommend, you can have a look at this list: http://lucene.apache.org/solr/books.html I've never looked at any of Solr's Java code - I just assume it works and respects its public API :)

Let me flex my Javascript inexperience a bit:
I would think the error handling would want to capture unsuccessful attempts at making calls to Solr but consider any result from Solr (error message or data set) to be a successful call.
jquery uses >> function (servlet, string, handler, errorHandler) {...do stuff.. } << for it's AJAX error handling which gives generic error messages such as JSON.parse: unexpected character if you search for a term like York" but the error in Solr itself will give the specific Invalid character (") at character position 5.
Is it possible to pass Solr's error message back to the UI as a "successful" AJAX call and have the errorHandler check for unsuccessful calls to Solr?

Contributor

jpmckinney replied May 2, 2013

How do you determine whether an error is a Solr error or not?

I would think the initial AJAX call would have error handling to make sure the call to Solr is not broken (say in case of an invalid URL, network connection error, system failure, etc). Once that call to Solr is successfully made, if the search term causes an error with Solr, that ought to be irrelevant to the AJAX error handler. The entire process has two calls: one from the UI to Solr and then Solr to its Index. The AJAX error handler has the responsibility to cover both calls but losses some specificity as a result. I suppose I'm looking for a way to break it down with the error handler to check only for the UI to Solr connection. Whatever Solr gives back is success.

The response handler (or something) would need to check for something like
If not response.err.msg do response.doc output
else do resposne.err.msg output.

Contributor

jpmckinney replied May 3, 2013

You can create your own manager that overrides executeRequest, checks the response, and either processes it as "successful" or as a failure depending on the contents of the response. I think it makes sense to handle all errors as errors, because error responses are not processable by widgets by default. ajax-solr lets you override its functionality if you want it to behave differently.

Please sign in to comment.