Skip to content

Commit

Permalink
add errorHandler, closes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
James McKinney committed Apr 11, 2013
1 parent bbbdf96 commit 96166a2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
16 changes: 13 additions & 3 deletions core/AbstractManager.js
Expand Up @@ -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.';
},

Expand All @@ -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);
}
});
2 changes: 1 addition & 1 deletion core/Core.js
Expand Up @@ -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++) {
Expand Down
15 changes: 11 additions & 4 deletions managers/Manager.jquery.js
Expand Up @@ -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

@JSnoob
Copy link

@JSnoob JSnoob commented on 96166a2 Apr 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :/

@jpmckinney
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

@JSnoob
Copy link

@JSnoob JSnoob commented on 96166a2 May 2, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@jpmckinney
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@JSnoob
Copy link

@JSnoob JSnoob commented on 96166a2 May 2, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jpmckinney
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.