How to handle unahandled exceptions in blazor server razor component, In page model, I'm handling with try catch block, but some errors come from razor file and the circuit breaks, application stops. Kindly suggest me the solution to handle this.
0
votes
1 Answers
0
votes
I read alot about this issue and what I found that there is no way to make a global error handling right now for blazor, you can handle the error that related with http request like this:
if (!response.IsSuccessStatusCode)
{
var statusCode = response.StatusCode;
switch (statusCode)
{
case HttpStatusCode.NotFound: _navigation.NavigateTo("/Notfound", true);
break;
case HttpStatusCode.Unauthorized: _navigation.NavigateTo("/Error");
break;
default: _navigation.NavigateTo("/Error");
break;
}
throw new ApplicationException($"Reason: {response.ReasonPhrase}");
}
You need also to be carefull about using try catch inside blazor component with some proberty [inject] becuase maybe your blazor circuit will fail!. You can read more about that in microsoft documents and correct if I am wrong.