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

Handle server error on client side

3 Answers 523 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carlos
Top achievements
Rank 1
Carlos asked on 19 Apr 2018, 07:04 PM

I have a problem trying to handle server message error on client side after a read event on grid component

my grid is setting up:

 @(Html.Kendo().Grid<myModel>().GetPageableGrid(gridName)
        .Name(gridName)
        .AutoBind(true)
        .DataSource(dataSource => dataSource
            .Custom()
            .Type("aspnetmvc-ajax")
            .PageSize(10)
            .ServerPaging(true)
            .ServerSorting(true)
            .ServerFiltering(true)
            .Transport(transport => transport
                    .Read(read => read.Action("search", "test"))
                    .Destroy(e => e.Action("delete", "test"))
            )
            .Schema(schema => schema
                .Errors("Errors")
                .Model(model =>
                {
                    model.Id(p => p.modelID);
                })
            )
            .Events(events =>
            {
                events.Error("OnRequestError");
                events.RequestEnd("OnRequestEnd");
            })
        ) ....

the javascript function:

function OnRequestError(e) {
        alert(e.errors); // show 'undefined'
}

in the controller:

DataSourceResult result = new DataSourceResult
{
    Errors = new { message }
};
return new JsonResult(result);

json response look like:

{"Data":null,"Total":0,"AggregateResults":null,"Errors":{"message":"The method or operation is not implemented."}}

I cant reach errors property on OnRequestError, I tryed differents aproacches but not results yet it is alway 'undefined'

I apreciate any help.

3 Answers, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 24 Apr 2018, 07:42 AM
Hello Carlos,

When a server validation fails it is necessary to notify the dataSource that an error occurred. In order to notify the DataSource, that an error occurred during the request execution, the response should return an error HTTP status code (e.g. 4xx or 5xx). This will trigger the error handler and raise the DataSource error event. Within the error event handler the server error messages are available therefore you are able to notify the user that an error occurred.

Then, to revert the newly applied changes in the dataSource call the cancelChanges method of the grid within the error event handler of the dataSource.

I would recommend you to take a look at the following article which contains a detailed step by step explanation on how to use server side validation with the Kendo Grid:



Regards,
Georgi
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
CLOCA
Top achievements
Rank 1
answered on 03 Apr 2019, 01:02 PM

Hello,  I also have a question regarding error handling. 

How do we get the error message from a thrown exception? 
I'm able to get error message for custom errors but when an exception is thrown with a custom error message I cannot seem to find that message. 

0
Georgi
Telerik team
answered on 08 Apr 2019, 08:20 AM
Hi Cloca,

It depends on the format of the response and the configuration of the dataSource. If the dataSource is configured for Ajax binding, the server errors are stored within the Errors property of the response object. In other words you can access the full content of the errors in the e.errors field of the error event object.

e.g.

      .DataSource(dataSource => dataSource   
         .Ajax()
         ...
         .Events(x=> x.Error("onError"))
     )
 
function onError(e) {
     e.errors
 }


Regards,
Georgi
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Carlos
Top achievements
Rank 1
Answers by
Georgi
Telerik team
CLOCA
Top achievements
Rank 1
Share this question
or