views:

56403

answers:

6

Is there some way I can show custom exception messages as an alert in my jQuery Ajax error message?

For example, if I want to throw an exception on the server side via Struts by "throw new ApplicationException("User name already exists");", I want to catch this message (user name already exists) in the jQuery Ajax error message.

jQuery("#save").click(function(){
    if(jQuery('#form').jVal()){
            jQuery.ajax({
                type: "POST",
                url: "saveuser.do",
                dataType:"html",
                data:"userId="+encodeURIComponent(trim(document.forms[0].userId.value)),
                success:function(response){
                    jQuery("#usergrid").trigger("reloadGrid");
                    clear();
                    alert("Details saved successfully!!!");
                },
                error:function (xhr, ajaxOptions, thrownError){
                    alert(xhr.status);
                    alert(thrownError);
                }    
            });
        }
    }
);

On the second alert where I alert thrown error, I am getting undefined and the status code is 500.

I am not sure where I am going wrong. What can I do to fix this problem?

+24  A: 

Try this:

alert(xhr.statusText);

This is actually a XMLHttpRequest object.

kgiannakakis
Would this not return 'Internal Server Error' and not the custom exception message?
jamiebarrow
A: 

I believe the Ajax response handler uses the HTTP status code to check if there was an error.

So if you just throw a Java exception on your server side code but then the HTTP response doesn't have a 500 status code jQuery (or in this case probably the XMLHttpRequest object) will just assume that everything was fine.

I'm saying this because I had a similar problem in ASP.NET where I was throwing something like a ArgumentException("Don't know what to do...") but the error handler wasn't firing.

I then set the Response.StatusCode to either 500 or 200 whether I had an error or not.

vitorsilva
+12  A: 

Make sure you're setting Response.StatusCode to something other than 200. Write your exception's message using Response.Write, then use...

xhr.responseText

..in your javascript.

Sprintstar
A: 

Throw a new exception on server using:

Response.StatusCode = 500

Response.StatusDescription = ex.Message()

I believe that the StatusDescription is returned to the Ajax call...

Example: Try

            Dim file As String = Request.QueryString("file")

            If String.IsNullOrEmpty(file) Then Throw New Exception("File does not exist")

            Dim sTmpFolder As String = "Temp\" & Session.SessionID.ToString()

            sTmpFolder = IO.Path.Combine(Request.PhysicalApplicationPath(), sTmpFolder)

            file = IO.Path.Combine(sTmpFolder, file)

            If IO.File.Exists(file) Then

                IO.File.Delete(file)

            End If

        Catch ex As Exception

            Response.StatusCode = 500

            Response.StatusDescription = ex.Message()

        End Try
Steffan
+7  A: 

Controller:

public class ClientErrorHandler : FilterAttribute, IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        var responce = filterContext.RequestContext.HttpContext.Response;
        responce.Write(filterContext.Exception.Message);
        responce.ContentType = MediaTypeNames.Text.Plain;
        filterContext.ExceptionHandled = true;
    }
}

[ClientErrorHandler]
public class SomeController : Controller
{
    [HttpPost]
    public ActionResult SomeAction()
    {
        throw new Exception("Error message");
    }
}

View script:

$.ajax({
    type: "post", url: "/SomeController/SomeAction",
    success: function (data, text) {
        //...
    },
    error: function (request, status, error) {
        alert(request.responseText);
    }
});
AlexMAS
This isn't a "correct" answer to the question but it most certainly shows a higher level solution to the problem... Nice!
Jeff Spicoli
I'm doing something similar. It works fine if everything's done on the development box. If I try connecting from a different box on the network, the xhr.responseText contains the generic error page html and not my custom message, see http://stackoverflow.com/questions/3882752/returning-a-response-on-error-in-ajax-call-using-asp-net-mvc-and-jquery
jamiebarrow
+1  A: 

This is probably caused because the JSON field names do not have quotation marks.

Change the json structure from:

{welcome:"Welcome"}

to:

{"welcome":"Welcome"}
Guy