P.Brian.Mackey
P.Brian.Mackey

Reputation: 44275

jQuery give user friendly error message on fail

I handle server exceptions with $.ajax:

.fail(function (qXHR, textStatus, errorThrown) {
     $('#loadingDiv').hide();
     console.log(qXHR.responseText);
     console.log(textStatus);
     console.log(errorThrown);
     $('#errorMessage').text(qXHR + "::" + textStatus + "::" + errorThrown);
});

Sample Error: [object Object]::error::Internal Server Error

When I throw an ApplicationException from the server I want to tell the user to try searching for something more specific.

"Internal Server Error" is too vague. The object.responseText has the specific error information, but it's hidden in piles of HTML.

How can I give the user better information based on the exception that occurred without overloading them with qxHR.responseText?

Upvotes: 0

Views: 205

Answers (1)

Barmar
Barmar

Reputation: 780994

Don't generate an HTTP error when the server code detects invalid input. Generate a successful response, but put an error message into the response structure (use JSON, for instance). Then have the .done() handler check whether response.error is filled in, and display that.

Upvotes: 3

Related Questions