-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Global exception handling #13452
Comments
All unhandled exceptions should end up in the Logger. If you're seeing otherwise, please let us know and we'd be happy to investigate. Looking at the logs should be the way to go here. |
OK, what i understand is i have to provide my own implementation of ILogger ? and its on my implementation i have to deal with the exception ? (for example show notification, write exception in a file for server side and send it to controller for client-side) But even with this extension, if there is an unhandled exception in the code, the app crash. so if i understand, i don't have the choice to surround all of my code with try catch ? thanks for your explanation |
Any of the logger providers listed here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.2#built-in-logging-providers as also community developed providers (https://github.com/aspnet/Extensions/tree/master/src/Logging#providers) should work.
For server-side Blazor, an unhandled exception should disconnect the circuit, but not crash the application. If a block is meant to throw an exception you can tolerate, a |
Client side blazor does not log any exceptions. They are just printed to console. Here is the code from WebAssemblyRenderer
Only way I found that can intercept exceptions is described here But I would not call this exception handling... I would like to have a global exception handler so I could write my code without catch blocks...
If SomeAsync method throws an exeption my IsLoading varialbe is set to false but my view is not rerendered. You have to call SetStateHasChanged in finally block to force view rerender |
for client side blazor, i finally write my own implementation on ILogger (inspired by the code of default implementation which log in the console) . I abandon the idea of global exception handling, which seem not be the logic of blazor, and put try/catch everywhere and inject an ILogger in all component. Except if you think there is better to do, you can close this issue. |
I think this is a missing feature.. When exception is thrown I would also like to have a centralized place to capture all unhandled exceptions and do something.. Either display it in a popup, log it somewhere, send it to the server or whatever.. without any harm to app state |
Isn't the |
@SteveSandersonMS look at my comment above |
@rborosak Thanks for the clarification! |
Any update on this issue ? Currently my sever side blazor app just stopped responding to user input after an exception is thrown. Its not a good practice to cover all lines of code with try-catch (nor to expect the developer to do so) , in case of "un-predicted" exception we should have a generic handler that allows us : 1.Choose whether to break the circuit. Currently after un-handleded exception the app can not notify the user on a problem and the user can only re-run the app (close the browser and goto to the app URL again) or refresh the page and the loose all the state (which is sometimes good , but the app developer should choose this). |
@hagaygo Very good summary! @SteveSandersonMS Please consider this for the 3.1.x milestone ! Please do not move this to 5.0.0 |
Hi, I get this kind of error when the circuit breaks in the Console of Developer Window . I dont think there anyway presently that can notify the user that an error has occurred after this in the screen and ask to refresh it without manually opening the developer window to check and refreshing the page |
@SteveSandersonMS Could you clarify wheter or not the I have seen several cases where my custom
An example are when my repository are throwing an exception as such: My MVC solution are catching all exceptions and it is using similar ErrorHandling implementations. |
Moving this to |
the biggest issue for me is error boundaries. i want to be able to use a third party component and, somehow, wrap my use of that component in error handling so that a bug in the library doesn’t kill my whole web application. right now there’s no “place to put the try catch” - the whole component tree is supervised by the blazor renderer, which will die if anything deep in the tree messes up. |
+1 for this feature. I personnaly used to throw exception to manage business errors. At a high level, I catch the exception and according to its type, I decide if the exception must be logged, displayed to the user and if displayed if I display the complete message (business exceptions) or just a "Something goes wrong"). Actually, the only way I found to do something like this is to create a ComponentBaseEx class which inherit to ComponentBase and override OnInitialized(Async), OnParametersSet(Async), ... Do you think
|
Here's my scenario: Blazor desktop/hybrid app using Blazor server. One user logged in at a time and state persists across reloads. So, if there's an unhandled exception, there's no way to get the app back to a trustworthy state other than restarting it. We need to catch unhandled exceptions, show appropriate UX, and then shut down. Standard desktop-app pattern. Temporary hack: we have logging set up with Serilog. Serilog has an API that lets you filter all log messages and decide whether they are emitted. We abuse this API by checking whether a log event has a |
Hello guys, like the others, i'm realy worried if you move this to the .NET 5 milestone. But i definitively can't do that without a strong way to catch or at least log all unexpected errors. Please consider make something, even not perfect, before the 5.0 release (juste a method with the exception is OK for me, at least we can log...) Thans you very much ! |
Hello, I'm in the same situation that @julienGrd |
Same goes with me.
We have used Blazor Server side for all our development and migrated from
the legacy ASP.Net Web Pages.
…On Tue, Mar 3, 2020 at 12:02 PM eliudgerardo ***@***.***> wrote:
Hello, I'm in the same situation that @julienGrd
<https://github.com/julienGrd>
Please do something to help/guide us.
Regards!
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#13452?email_source=notifications&email_token=ANKYSDFSUQ3OTBGWQMRV5EDRFUZ27A5CNFSM4IPQWA62YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOENUJXLA#issuecomment-594058156>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ANKYSDGFCDRMZV2N4NLMOMLRFUZ27ANCNFSM4IPQWA6Q>
.
--
*Ankit Bansal*
*Scientific Games *
|
The intention is that you should be able to log all uncaught exceptions using the However there's still a piece of this we haven't yet implemented. This is being tracked by #19533. We'll be sure to get this done soon. |
Thanks for contacting us. |
For anyone looking for a client-side workaround: function reportError(error) {
if (DotNet) {
DotNet.invokeMethodAsync('MyApp.Client', 'NotifyError', error);
}
}
var exLog = console.error;
console.error = function (msg) {
exLog.apply(console, arguments);
reportError(msg);
}
window.addEventListener("unhandledrejection", function (promiseRejectionEvent) {
reportError(promiseRejectionEvent.reason.message);
}); public partial class AppLayout
{
[JSInvokable("NotifyError")]
public static void NotifyError(string error)
{
//Handle errors
}
} This is a critically important feature for a production application and I wouldn't feel comfortable releasing an application where I couldn't control the application's behavior in the event of an unhandled error, or at least be able to reliably log it. Fortunately, I'm sure there's enough support for this in JavaScript we can leverage, however it's a big enough gap to make me question whether this technology's really mature enough to be adopted in serious applications. I hope this a global error handler is prioritized in future releases. |
What a pity that a major issue like this one, opened in Aug. 2019, is still delayed |
@Lixan For sure. If an essential issue like this isn’t take into account, imagine others... It’s already a big deal to choose Blazor as web framework because of its youth but if Microsoft doesn’t listen to developpers !!! |
Perhaps it is not technically feasible to fix this issue as we need without some kind of AOP or attribute + compiler feature. It would be an improvement if components could implement an optional interface which exposes a handle error method. Then components would invoke this method on unhandled error. @mkArtakMSFT, is at least some solution planned for real for this issue? |
I am looking for some global error handling solution to both my Blazor server and WASM projects. Is there any forecast date for when .Net 6 will be out? |
Yes, the .NET ship dates are listed at https://github.com/dotnet/core/blob/master/roadmap.md Please note that you already can catch errors and log them globally. That's what the Since #26953 reflects the work we plan to do, I'm closing this issue now. If anyone is concerned that #26953 (and the existing |
@SteveSandersonMS you are pointing to ILogger implementation. This works for WebAssembly. We've already done it for the ABP Framework (see). We are showing a messagebox to the user. However, for Blazor Serverside, it doesn't help much because the SignalR connection is broken in anway. I investigated the source code (of aspnetcore), but there is no way to prevent this. |
I'm afraid that's not a valid solution. Continuing after an arbitrary unhandled exception compromises the server's stability and may open security vulnerabilities. If there's an unhandled exception, the only safe action is to terminate the circuit, since there's no way to know what state things within that circuit have been left in. We already send a notification about the error to JS code running in the browser, so it's possible to display some custom UI at that point (which the project template already does by default). I know this makes things seem less convenient, but helping customers deliver secure applications is a very high priority for us. |
@SteveSandersonMS Would the following suggestion work? ComponentBase is modified to include a virtual method which would be invoked when BuildRenderTree method encounters a problem. This method would provide alternative render. If it too fails, break the circuit. This may work if RenderTreeBuilder is not immediately committing contents between OpenComponent and CloseComponent statements? |
@akovac35 That's interesting. We could do that, but I think the plan we already have in #26953 gives you the same benefits but even better still, since you don't have to repeat your error handling in every single component, but rather can plug in an error renderer at any set of points in your component hierarchy. Errors caught within an error boundary will not be fatal to the circuit, since we know exactly what state is affected and the framework can clear it up so the app can safely continue. |
Hello guys, I wanted to know what is the better way to catch exception at application level, to avoid put try catch in all my method and component, and centralize the exception management in one point (typically show an error notification and log the exception).
so there is a better way to do this in preview8, or something planned for the RTM ?
thanks !
The text was updated successfully, but these errors were encountered: