This is a migrated thread and some comments may be shown as answers.

Displaying errors from Web API calls

2 Answers 319 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lars
Top achievements
Rank 1
Lars asked on 08 Sep 2014, 01:08 AM
Hello,

Let's say the grid makes a GET to api/tickets/2/lines to get the lines for ticket # 2, and the Web API controller cannot find that ticket.  It creates an HttpResponseMessage like this:

return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Ticket not found: " + ticketId);

The raw HTTP response is:
HTTP/1.1 404 Not Found
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcRG90TmV0XFdlYlBvc1xXZWJQb3NcYXBpXHRpY2tldHNcMlxsaW5lcw==?=
X-Powered-By: ASP.NET
Date: Mon, 08 Sep 2014 00:25:57 GMT
Content-Length: 60
 
{"message":"Ticket not found: 2"}

In the grid's dataSource, I have defined an error event handler:
error: function(e) {
    if (e.errors) {
        alert(e.errors);
        this.cancelChanges();
    }
},
The handler gets called, but e.errors is undefined.  Looking at e, there I cannot find the error message, "Ticket not found: 2" anywhere.

How can I get to my message? 

Also, do you have a recommended approach for handling and displaying errors during grid operations (I was planning to use Toastr or similar to display the friendly message, in addition to obviously log details server-side).

Thanks,
Lars

2 Answers, 1 is accepted

Sort by
0
Accepted
Rosen
Telerik team
answered on 10 Sep 2014, 06:25 AM
Hi Lars,

The error field will be populated only if a custom errors are return. This is in the case where the status code is 200 but there are, for example, model errors. Those custom errors are send as part of the response object. The name of the field in the response can be configured via the schema.errors option.

However, in the scenario you have described you will need to get the error text from the jqXHR object passed as part of the event argument. For example:

function error_handler(e) {
    alert(e.xhr.responseText);
}


Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Lars
Top achievements
Rank 1
answered on 10 Sep 2014, 07:44 PM
That should work - thanks!

Lars
Tags
Grid
Asked by
Lars
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Lars
Top achievements
Rank 1
Share this question
or