Return Error Message from Data Access Layer to UI

Hi All,

I am trying to pass error messages in my Blazor Webassembly app back to the UI from the Data Access Layer in the Server. These are exceptions where I manually check to see if something can be deleted, or not. For example, if it is in use in another table, then I cancel the delete operation.

I use a try ... catch and throw the relevant exception in the DAL:

    throw new Exception("Cannot Delete the selected Architecture, as it is being referenced in a Version!");

However, when I check the details in my "DataGrid" UI code, the message I receive is:

    'Cannot access a disposed object.\nObject name: 'System.Net.Http.WebAssemblyHttpHandler+WasmHttpContent'.'

Here is my "ActionFailure" code:

    public void ActionFailureHandler(Syncfusion.Blazor.Grids.FailureEventArgs Args)
    {
        var errorMessage = Args.Error.Message;
    }

How should I pass my own messages in these circumstances?

Thanks in advance

Simon

****EDIT****
I've worked out that I should handle this in the UI, rather than the DAL :)

However, I would still appreciate some comments on how to handle Database Errors in the UI?

3 Replies 1 reply marked as answer

JP Jeevakanth Palaniappan Syncfusion Team September 16, 2020 02:07 PM UTC

Hi Simon, 

Greetings from Syncfusion support. 

We have checked your query and found that you need to throw custom error message. The grid is bound with the order service and so if we throw custom exception from order service then only the same error message will get in the ActionFailure event of the grid. Otherwise the corresponding error message from the db will be thrown in ActionFailure event.  

We have prepared a sample which will throw exception from the OrderService and printed in the UI. Please find the below code snippet and the sample for your reference. 

OrderService.cs 
    public class OrderService 
    { 
        string baseUrl = "https://localhost:44311/"; 
        public async Task<List<Order>> GetOrdersAsync() 
        { 
            HttpClient http = new HttpClient(); 
            throw new System.Exception("Custom Exception thrown from OrderService"); 
            var json = await http.GetStringAsync($"{baseUrl}api/Default"); 
            return JsonConvert.DeserializeObject<List<Order>>(json); 
        } 
    } 

<span class="error">@ErrorDetails</span> 
 
<SfGrid ID="Grid" TValue="Order"> 
    <GridEvents OnActionFailure="ActionFailure" TValue="Order"></GridEvents> 
</SfGrid> 
 
<style> 
    .error { 
        color: red; 
    } 
</style> 
 
@code { 
    public string ErrorDetails = ""; 
 
    private void ActionFailure(Syncfusion.Blazor.Grids.FailureEventArgs args) { 
        ErrorDetails= args.Error.Message; 
        StateHasChanged(); 
    } 
} 


Please get back to us if you need further assistance. 

Regards, 
Jeevakanth SP. 


Marked as answer

SI Simon September 18, 2020 11:13 AM UTC

Hi Jeevakanth,

Thanks for the reply.

I knew it would be something fairly straightforward :)

Regards

Simon


JP Jeevakanth Palaniappan Syncfusion Team September 21, 2020 12:34 PM UTC

Hi Simon, 

Thanks for the update. Please get back to us if you need further assistance. 

Regards, 
Jeevakanth SP. 


Loader.
Up arrow icon