Trace:
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision Next revision Both sides next revision | ||
applications:custom_errorhandling [2019/08/05 21:35] admin created |
applications:custom_errorhandling [2019/08/06 13:00] admin |
||
---|---|---|---|
Line 2: | Line 2: | ||
~~Title: Custom error handling~~ | ~~Title: Custom error handling~~ | ||
- | ... | + | The application (ProjX) implements the **IExceptionListener** interface and registers itself as listener: |
+ | |||
+ | <file java> | ||
+ | protected void initApplication(UILauncher pLauncher) throws Throwable | ||
+ | { | ||
+ | ExceptionHandler.addExceptionListener(this); | ||
+ | |||
+ | ... | ||
+ | } | ||
+ | </file> | ||
+ | |||
+ | All Exceptions will be handled in the listener method: | ||
+ | |||
+ | <file java> | ||
+ | public void handleException(Throwable pThrowable) | ||
+ | {} | ||
+ | </file> | ||
+ | |||
+ | So, it's very easy to change the default error handling. The default error handling shows a popup dialog with the error message and some details (stack trace). | ||
+ | |||
+ | As example, we'll show the exceptions directly in the work-screens. So, every work-screen should be able to show error messages. To make this possible, we introduce the following interface: | ||
+ | |||
+ | <file java> | ||
+ | public interface IErrorHandler | ||
+ | { | ||
+ | public void showErrorMessage(String pMessage); | ||
+ | } | ||
+ | </file> | ||
+ | and all our work-screens should implement it like this example: | ||
+ | |||
+ | <file java> | ||
+ | public class FirstWorkScreen extends DataSourceWorkScreen implements IErrorHandler | ||
+ | { | ||
+ | /** labelMain. */ | ||
+ | private UILabel.labelMain = new UILabel(); | ||
+ | |||
+ | private void initializeUI() throws Throwable | ||
+ | { | ||
+ | labelMain.setText("ErrorArea"); | ||
+ | |||
+ | ... | ||
+ | |||
+ | add(labelMain, UIBorderLayout.SOUTH); | ||
+ | } | ||
+ | |||
+ | public void onLoad() throws Throwable | ||
+ | { | ||
+ | super.onLoad(); | ||
+ | |||
+ | labelMain.setVisible(false); | ||
+ | } | ||
+ | |||
+ | public void showErrorMessage(String pMessage) | ||
+ | { | ||
+ | labelMain.setText(pMessage); | ||
+ | labelMain.setVisible(true); | ||
+ | } | ||
+ | } | ||
+ | </file> | ||
+ | |||
+ | So, our work-screen example contains a new label in the SOUTH area (the bottom of the screen). The label will be hidden initially in **onLoad()**. Our screen is now ready to show error messages, but our application must be changed. To do this, we need a [[vaadin:customize_application|custom application]]. | ||
+ | |||
+ | Our application contains following code: | ||
+ | |||
+ | <file java MyCustomApplication.java> | ||
+ | public class MyCustomApplication extends ProjX | ||
+ | { | ||
+ | private List<Throwable> liErrors = null; | ||
+ | |||
+ | public MyCustomApplication(UILauncher pLauncher) throws Throwable | ||
+ | { | ||
+ | super(pLauncher); | ||
+ | } | ||
+ | |||
+ | public void handleException(Throwable pThrowable) | ||
+ | { | ||
+ | if (liErrors == null) | ||
+ | { | ||
+ | liErrors = new ArrayUtil<Throwable>(); | ||
+ | |||
+ | invokeLater(this, "showErrorMessage"); | ||
+ | } | ||
+ | |||
+ | liErrors.add(pThrowable); | ||
+ | } | ||
+ | |||
+ | public void showErrorMessage() | ||
+ | { | ||
+ | String message = liErrors.get(0).getMessage(); | ||
+ | |||
+ | for (IWorkScreen screen : getWorkScreens()) | ||
+ | { | ||
+ | if (screen instanceof IErrorHandler) | ||
+ | { | ||
+ | ((IErrorHandler)screen).showErrorMessage(message); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | liErrors = null; | ||
+ | } | ||
+ | } | ||
+ | </file> | ||
+ | We collect all occurred Exceptions and show one message at the end. We iterate through all opened work-screens and check if the interface is implemented. In our example, every work-screen has to implement the interface IErrorHandler. A better solution would be a custom work-screen base class, e.g. | ||
+ | |||
+ | <file java> | ||
+ | public class MyCustomWorkScreen extends DataSourceWorkScreen implements IErrorHandler | ||
+ | { | ||
+ | } | ||
+ | </file> | ||
+ | and all our work-screens should extend this base screen: | ||
+ | <file java> | ||
+ | public class FirstWorkScreen extends MyCustomWorkScreen | ||
+ | </file> |