-
Notifications
You must be signed in to change notification settings - Fork 168
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
ErrorHandler has no access to UI when command is executed through UI.access() from other thread and throws exception #9971
Comments
Hello. We had a discussion whether or not this works as expected, and basically this is one of those things where it "works like coded" which means that while there has not been given any promises that it errors during push/UI.access would go to the UI's error handler, it is quite natural though that the user would expect so. The current behavior has been around probably at least since version 7. At the moment I'm inclining to calling this a bug, but I don't think I would backport this to a maintenance release since it can change existing application behavior if there are errors happening frequently and it is now intended behavior that those are not shown to the user (it is quite theoretical, but can exist). |
The workaround for now would be to catch the exception and showing any kind of error indication, like Notification with error style, to the user. (Now that I write this, I actually think I've done this in a project a very long time ago.) |
Hi, thank you for responding. I am aware I can catch and handle the exception everywhere as workaround, but that is far from ideal., since the goal is to have uniform error notification for all errors and not leave it to individual developers to handle the errors correctly at every place. |
Good workaround, but far from perfect. Application code shouldn't be forced to call
Vaadin already knows the UI instance, since the error handler is being called by a try{}catch block executed from within the A workaround would be to extend the UI class and override all |
Hello @ggecy, I have the same problem. We are trying to show an error notification for an exception. We created an ErrorHandler but the UI.getCurrent() ist null (on Vaadin 23). Do I understand your workaround correctly? You are getting the current UI before doing your specific calls, set it with UI.setCurrent() and set it back in a finally block? |
Hi @hellectronic, here is my GUIUtils implementation - I am using GUIUtils.access() method everywhere instead of UI.access():
|
Hello @ggecy , thanks a lot! |
Thanks to @rodolforfq for referring me to this issue. I ran into a similar case when I threw This is my workaround:
This is definitely not an ideal solution as it uses Vaadin internal classes and I do not consider it production-ready. Also, it could be called after the view is rendered. Use with care :) |
I think this is a duplicite of #10533 which has been implemented and closed already. |
Description of the bug / feature
When business logic, which runs in background in separate thread from request-response thread, tries to modify the UI calling ui.access(command) method and the command throws an Exception we have a custom ErrorHandler implementation that tries to display a notification in UI, however there is no access to current UI in the error handler in this case since UI.getCurrent() is populated only during normal code execution and is cleared in com.vaadin.flow.component.UI#accessSynchronously(com.vaadin.flow.server.Command, com.vaadin.flow.function.SerializableRunnable) before calling the error handler in com.vaadin.flow.server.ErrorHandlingCommand#handleError() which is then called without any access to current UI.
Minimal reproducible example
Open UI in browser and then try to run a code from separate thread while there is no request-response running that will throw and Exception within UI.access() command.
Expected behavior
Exception is handled in ErrorHandler implementation registered in session which can modify the UI to show user a notification with useful error message.
Actual behavior
User sees no error message, since error handler has no access to current UI and thus opening notification fails.
Versions:
The text was updated successfully, but these errors were encountered: