Custom message for grid validation

Good afternoon, 

I'm having trouble with showing a custom message from the controller (for validation purposes), I followed examples in the forums but I'm getting errors and could make it work. Can you please help me?

In the controller I tried in different attemps:

1)               throw new Exception("Error el registro se encuentra vinculado a otra tabla ");

2)               return new BadRequestObjectResult(message);


and the view is like:

<ejs-grid id="EventosGrid" actionFailure="failureGridEventos" allowFiltering="true" allowGrouping="true" allowPaging="true" allowExcelExport="true"
toolbarClick="toolbarClick" toolbar="@(new List<string>() { "Add", "Edit", "Update", "ExcelExport", "Search", "Cancel", "Delete" })".......>

<script>
    function failureGridEventos(args) {
        var errorMessage = args.error[0].error.responseText.split("Exception:")[1].split('<br>')[0]; //split the message from args
        alert(errorMessage);
    }

Everytime I get an error like:
"Uncaught (in promise) TypeError: args.error"
 

3 Replies 1 reply marked as answer

BS Balaji Sekar Syncfusion Team August 26, 2020 01:16 PM UTC

Hi Javier, 
 
Greetings from Syncfusion. 
 
By default in EJ2 Grid component we have actionFailure event to catch the all the exceptions thrown from the grid component. So, you can also throw any exceptions from the server side which will be captured by the action failure event and using that you can show the exceptions in the client side. 
 
For your convenience we have attached the code example so please refer the code example for your reference. 

Code example: 
HomeController.cs 
 
public IActionResult Insert([FromBody]CRUDModel<Orders> Value) 
        { 
            try 
            { 
             .        .        .      .                 
            } 
            catch (Exception e) { 
 
                     throw new Exception("Error el registro se encuentra vinculado a otra tabla");//message returns the exception content    
            } 
        } 
 
app.component.ts 
 
function actionFailure(args) { 
       var errorMessage = args.error[0].error.responseText.split("Exception:")[1].split("</div>")[0]; 
        console.log(errorMessage) //Get error message here 
    } 
 
 
We have video demonstrated of reported query, please download a below link to know about Exception handling in actionFailure event 
 
 
Please get back to us, if you need further assistance. 
 
Regards, 
Balaji Sekar 


Marked as answer

LC Lucian Calin November 10, 2023 03:33 PM UTC

Hi,


I have the same issue here, instead of the error I got on actionFailure:

"Error at https://localhost:44312/js/ej2/ej2.min.js:1:763649"




JC Joseph Christ Nithin Issack Syncfusion Team November 14, 2023 09:24 AM UTC

Hi Lucian,


  Greetings from Syncfusion support.


We are glad to announce that we have included we have included the fix for “In actionFailure, failure message is not sent properly in latest version” in the latest version 23.1.41. We suspect you are using a older version of the EJ2, hence we suggest you to update to the latest version. To obtain a custom error message from the server-side within the actionFailure event argument during a fetch operation, use the text() method within the await statement to retrieve the custom error message from the server side. Please refer the below code example and sample for more information.


 

 

<script>

    async function actionFailure(args) {

        if (args && args.error && args.error[0] && args.error[0].error) {

            try {

                const response = await args.error[0].error.text();

                const errorMessage = response.split("Exception:")[1].split('\n')[0].slice(1);

               alert(errorMessage);

            } catch (error) {

                console.error("Error reading the error message: " + error);

            }

        }

    }

</script>

 


Regards,

Joseph I.


Loader.
Up arrow icon