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

No Error Event with Async Read Action

3 Answers 612 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Robert Madrian
Top achievements
Rank 1
Veteran
Iron
Robert Madrian asked on 30 May 2017, 12:53 PM

Hello,

I have the following Grid:

@(Html.Kendo().Grid<dynamic>()
      .Name("gridEQ")
      .Columns(columns =>
      {
          columns.AutoGenerate(true);
      })
      .Pageable()
      .Sortable()
      .Scrollable()
      .Selectable(s => s.Mode(GridSelectionMode.Multiple))
      .NoRecords()
      .Filterable(f => f.Enabled(false))
      .AutoBind(false)
      .DataSource(dataSource => dataSource
          .Ajax()
          .PageSize(100)
          .Read(read => read.Action("ReadAsync", "Home"))
      )
    )

 

If I use the Read Action (synchron) like this and an error on the Server exists the error Event of the grid datasource is raised:

public IActionResult Read(bool execute, string queryJson, string optionsJson, [DataSourceRequest] DataSourceRequest request)
        {
            try
            {
                var query = eqService.GetQueryByJsonDict(queryJson.ToJsonDict());
                var sql = eqService.BuildQuery(query, optionsJson.ToJsonDict());
 
                if (execute)
                {
                    SqlConnection c = new SqlConnection(this.cache.Get("ExecuteConnectString").ToString());
                    result = c.Query<dynamic>(sql, commandType: System.Data.CommandType.Text);
 
                return Json(result.ToDataSourceResult(request));
            }
            catch (Exception ex)
            {
                return Json(ex);
            }
 
        }

 

If I use the AsyncRead Action like this and an error on the Server exists the error Event of the grid datasource is not fired:

public async System.Threading.Tasks.Task<ActionResult> ReadAsync(bool execute, string queryJson, string optionsJson, [DataSourceRequest] DataSourceRequest request, CancellationToken cancellationToken)
        {
            try
            {
                var query = eqService.GetQueryByJsonDict(queryJson.ToJsonDict());
                var sql = eqService.BuildQuery(query, optionsJson.ToJsonDict());
 
                if (execute)
                {
                    SqlConnection c = new SqlConnection(this.cache.Get("ExecuteConnectString").ToString());
                    result = await c.QueryAsync<dynamic>(sql, commandType: System.Data.CommandType.Text, cancellationToken: cancellationToken);
                     
                return Json(result.ToDataSourceResultAsync(request));
            }
            catch (Exception ex)
            {
                return Json(ex);
            }
 
        }

Why this?

What I'm missing?

robert

3 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 02 Jun 2017, 09:14 AM
Hi Robert,

I have tested the following code locally and the error event of the DataSource fires as expected:
public async Task<ActionResult> EditingCustom_Read([DataSourceRequest]DataSourceRequest request)
{
    try
    {
        throw new Exception();
        DataSourceResult result = await productService.Read().ToDataSourceResultAsync(request);
        return Json();
    }
    catch (Exception ex)
    {
        return Json(ex);
    }
}

@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
    .Name("grid")
    .DataSource(dataSource => dataSource
        .Ajax()
        .ServerOperation(true)
        .Events(events => events.Error("error_handler"))       
        .Read(read => read.Action("EditingCustom_Read", "Grid"))
    )
)
 
<script type="text/javascript">
    function error_handler(e) {
        alert("error")
    }
</script>

Could you please inspect the browser's Network tab and see what is the response of the request?


Regards,
Konstantin Dikov
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Robert Madrian
Top achievements
Rank 1
Veteran
Iron
answered on 14 Jun 2017, 12:46 PM

Hi,

it seems that the result Exception is differnt between sync (see Picture Error_sync.jpg) and async Action (see Picture Error_async.jpg)..

should I catch the error and sen a specific Json to the grid (what's the best practise here)?

 

robert

0
Accepted
Konstantin Dikov
Telerik team
answered on 15 Jun 2017, 01:21 PM
Hello Robert,

Sending custom Json would be the best approach, because you could then display a meaningful error message to the end user. As for the exception within the async method, you could try to return the most inner exception instead, but once again, returning customized Json would be a better solution in my opinion.
 

Regards,
Konstantin Dikov
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Robert Madrian
Top achievements
Rank 1
Veteran
Iron
Answers by
Konstantin Dikov
Telerik team
Robert Madrian
Top achievements
Rank 1
Veteran
Iron
Share this question
or