OnFailure Grid not return custom message

Hi,

this is javascript code:

function gridFailure(args) {

    var str = "";

    var message = "";

    var dialogError = document.getElementById('ErrorList').ej2_instances[0]

    str = args.error[0].error.responseText;

    message = "<p>" + str + "</p>";

    dialogError.content = message;

    dialogError.show();

};


This is controller:

public async Task<ActionResult> InsTipiCarrello(TipoCarrello value, string action)

        {

            log4net.Config.XmlConfigurator.Configure();

            log.Info("Inizio esecuzione InsTipiCarrello(TipoCarello value, string action)");

            try

            {

                TipoCarrello tipoCarello = _tipoCarrelloRepository.CreateTipoCarrello();

                using (_tipoCarrelloRepository)

                {

                    ModelState.Remove("value.IdTipoCarrello");

                    value.IdTipoCarrello = Guid.NewGuid();

                    if (ModelState.IsValid)

                    {

                        tipoCarello.IdTipoCarrello = value.IdTipoCarrello;

                        tipoCarello.Tipo = value.Tipo;

                        tipoCarello.Codice = value.Codice;


                        if (await _tipoCarrelloRepository.ExistTipoCarrello(value.Tipo))

                        {

                            Response.StatusCode = 412;

                            throw new Exception(Server.HtmlEncode(Resources.TipiCarrello.TipoGiaPresente));

                        }

                        else

                        {

                            if (await _tipoCarrelloRepository.InsTipoCarrello(tipoCarello) != 1)

                            {

                                Response.StatusCode = 412;

                                throw new Exception(Resources.Common.ErroreTrasmissione);

                            }

                        }

                    }

                    else

                    {

                        var message = string.Join(" | ", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage));

                        Response.StatusCode = 412;

                        throw new Exception(Server.HtmlEncode(Server.HtmlEncode(message)));


                    }

                    log.Info("Fine esecuzione InsTipiCarrello(TipoCarello value, string action)");

                }

            }

            catch (Exception ex)

            {

                var message = ex.InnerException == null ? ex.Message : ex.InnerException.Message;

                log.Error(string.Format("Errore:{0}", message.ToString()));

                Response.StatusCode = 412;

                throw new Exception(Server.HtmlEncode(Server.HtmlEncode(message)));

            }

            return Json(value);

        }


But args.error not return custom exception message selected, and in insert if i throw exception grid refresh but is empty.


11 Replies

SK Sujith Kumar Rajkumar Syncfusion Team June 28, 2021 12:26 PM UTC

Hi Pio, 
 
Greetings from Syncfusion support. 
 
Based on the provided information we could understand that you are facing problem when trying to access exception thrown from server action method on inserting a new record. We checked this problem from our end but could not reproduce it as we were able to access the exception message in the actionFailure event as shown in the below image, 
 
 
 
And the exception is thrown like in below code snippet, 
 
public ActionResult Insert([FromBody]CRUDModel<OrdersDetails> requestData) 
{ 
    if (requestData.value.CustomerID == null) 
    { 
        throw new Exception("Cannot insert row - 'CustomerID' is empty"); 
    } 
    else 
    { 
        OrdersDetails.GetAllRecords().Insert(0, requestData.value); 
    } 
    return Json(requestData.value); 
} 
 
We have prepared a sample based on this for your reference. You can find it below, 
 
 
So please share us the following information to validate further on this, 
 
  • Let us know what error message is returned in the actionFailure event.
  • Let us know if you are able to reproduce your problem scenario in the above shared sample. If so, share us the replication steps for the same.
  • Syncfusion package version used.
  • Let us know how you have referenced the script file for the EJ2 controls in your application.
  • Grid code file.
 
Regards, 
Sujith R 



PL Pio Luca Valvona June 28, 2021 01:45 PM UTC

Hi Sujith

thank you fo rsupport.

as you could see in the code I shared with you, I use your own code but args doesn't return the desired value, but another custom error that in debug is never injected into the grid, so I don't understand where args takes it from.

commented on all the code that refers to the error returned but arg keeps give me the same error.



SK Sujith Kumar Rajkumar Syncfusion Team June 29, 2021 11:00 AM UTC

Hi Pio, 

We are not able to clearly identify the reported problem with the provided information. So can you please share us the details requested in our previous update which will be helpful to identify your exact problem scenario and validate further based on that. 

Regards, 
Sujith R 



PL Pio Luca Valvona June 29, 2021 11:38 AM UTC

Hi Sujith,

with the code I showed you above, I get the following result:


- This is the message I expect:





-This is the message that returns args:





SK Sujith Kumar Rajkumar Syncfusion Team June 30, 2021 10:29 AM UTC

Hi Pio, 
 
We checked your reported case by throwing the below exception message as shown in your code snippet but still could not reproduce the problem as the error message was properly received, 
 
if (requestData.value.CustomerID == null) 
{ 
    var message = "Cannot insert row - 'CustomerID' is empty"; 
    Response.StatusCode = 412; 
    throw new Exception(Server.HtmlEncode(Server.HtmlEncode(message))); 
} 
 
 
Please check the below modified sample and screenshot image for reference, 
 
 
 
 
So could you please check the above provided sample and let us know if you are able to replicate your problem case in it. It would be helpful to identify your exact problem and provide the solution based on that. 
 
Regards, 
Sujith R 



PL Pio Luca Valvona July 10, 2021 12:26 PM UTC

Hi Sujith,

I found that the problem is due to enabling the custom erro page in the webconfig.

If I disable those settings in the web.config, I get the error as shown, but as soon as I reactivate, the Resposne.StatusCode= 412 which is hardcoded, generates the message I indicated overwriting what I pass via args.

If I delete the 412 it generates me a 500 error.

How can I fix it?



SK Sujith Kumar Rajkumar Syncfusion Team July 12, 2021 12:43 PM UTC

Hi Pio, 
 
Based on the provided information we modified the sample to define a custom error page in the web.config file, but the custom error message was still properly received in the Grid’s actionFailure event. Please check the below modified sample for reference, 
 
 
So it would be helpful to identify your exact problem scenario if you could reproduce the same in the above sample and if we misunderstood the reported case then please share us more details on it to validate further. 
 
Regards, 
Sujith R 



PL Pio Luca Valvona July 12, 2021 04:51 PM UTC

Hi Sujith,

I modified your example, to show you how the problem occurs.

I added a controller for handling error pages, error pages and the web.config


Attachment: CustomErrorGrid1828716021_a7e60b72.zip


SK Sujith Kumar Rajkumar Syncfusion Team July 13, 2021 12:34 PM UTC

Hi Pio, 
 
Thanks for sharing the modified sample. 
 
The reported problem was occurring since the default error page is directly returned when the exception is thrown from the insert method which is causing the custom error message passed to be omitted. So we suggest you to achieve your requirement by defining the custom error message in a global class property in the Insert method, write this message to the response in the error controller and then accessing it in the Grid’s actionFailure event. 
 
This is demonstrated in the below code snippets, 
 
HomeController.cs 
public static class Globals 
{ 
    public static string CustomMessage { get; set; } 
} 
 
public ActionResult Insert([FromBody]CRUDModel<OrdersDetails> requestData) 
{ 
    if (requestData.value.CustomerID == null) 
    { 
        Response.StatusCode = 412; 
        Globals.CustomMessage = "/Cannot insert row CustomerID is empty?"; 
        throw new Exception(); 
    } 
               ... 
    return Json(requestData.value); 
} 
 
ErrorController.cs 
public ViewResult ServerError() 
{ 
    try 
    { 
        Response.StatusCode = 500; 
        string errorMsg = Globals.CustomMessage; 
        Response.Write(errorMsg); 
        Response.TrySkipIisCustomErrors = true; 
        return View("500"); 
    } 
              ... 
} 
 
View page 
// Grid’s actionFailure event handler 
function onActionFailure(args) { 
    var errorDetails = args.error[0].error.responseText; 
    // Custom error message is retrieved 
    var errorText = errorDetails.substring(errorDetails.indexOf('/') + 1, errorDetails.indexOf('?')); 
} 
 
Please find the below modified sample for reference, 
 
 
Let us know if you have any concerns. 
 
Regards, 
Sujith R 



PL Pio Luca Valvona July 17, 2021 11:46 AM UTC

Hi Sujith,

Perfect!!

Thank you for support.



SK Sujith Kumar Rajkumar Syncfusion Team July 19, 2021 09:05 AM UTC

Hi Pio, 

You’re welcome. We are glad to hear that your query has been resolved. 

Regards, 
Sujith R 


Loader.
Up arrow icon