I have been using this ServiceResponse model to communicate data between an Azure Function backend and a Blazor WebAssembly frontend:
public class ServiceResponse<T>
{
public T? Data { get; set; }
public bool Success { get; set; } = true;
public string Message { get; set; } = string.Empty;
}
Inside my Razor components I have been using something like this:
@if(Error)
{
<span>@ErrorMessage</span>
}
else
{
// page content
}
@code
{
private Data Data { get; set; } = new();
private bool Error = false;
private string ErrorMessage = string.Empty;
protected override async Task OnInitializedAsync()
{
var response = Service.GetData();
if(!response.Success)
{
Error = true;
ErrorMessage = response.Message;
}
Data = response.Data!;
}
}
I’m looking for guidance on best practices for handling errors and displaying them in the frontend. Specifically, I’d like to know how to structure error handling so that when one component fails to load its data, it can display an error message just for that component without breaking other components on the page. I’d also like to avoid repeating if statements and manual error checks for every backend call if there’s a cleaner approach.