Trace:
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
applications:custom_errorhandling [2019/08/05 21:35] admin created |
applications:custom_errorhandling [2020/07/08 13:14] (current) cduncan articles |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ~~NOTRANS~~ | ~~NOTRANS~~ | ||
| - | ~~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 which shows a popup dialog: | ||
| + | |||
| + | {{:applications:error_dialog.png?nolink|}} | ||
| + | |||
| + | with the error message and some details (stack trace). \\ | ||
| + | \\ | ||
| + | As an example of a custom error handling, we'll show the exceptions directly in the workscreens. So, every workscreen 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 workscreens should implement the interface, e.g.: | ||
| + | |||
| + | <file java> | ||
| + | public class FirstWorkScreen extends DataSourceWorkScreen implements IErrorHandler | ||
| + | { | ||
| + | /** labelMain. */ | ||
| + | private UILabel.labelMain = new UILabel(); | ||
| + | |||
| + | private void initializeUI() throws Throwable | ||
| + | { | ||
| + | labelMain.setText("ErrorArea"); | ||
| + | labelMain.setForeground(UIColor.red); | ||
| + | |||
| + | ... | ||
| + | |||
| + | 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 workscreen 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 the 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 = ExceptionUtil.getRootCause(liErrors.get(0)).getMessage(); | ||
| + | |||
| + | for (IWorkScreen screen : getWorkScreens()) | ||
| + | { | ||
| + | if (screen instanceof IErrorHandler) | ||
| + | { | ||
| + | ((IErrorHandler)screen).showErrorMessage(message); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | liErrors = null; | ||
| + | } | ||
| + | } | ||
| + | </file> | ||
| + | We collect all occurred exceptions in a list - **liErrors** - and show one message at the end, see **showErrorMessages**. \\ | ||
| + | \\ | ||
| + | In **showErrorMessages**, we iterate through all opened workscreens and check if our interface is implemented. In our example, every workscreen has to implement the interface **IErrorHandler**. A better solution would be a custom workscreen base class, e.g. | ||
| + | |||
| + | <file java> | ||
| + | public class MyCustomWorkScreen extends DataSourceWorkScreen implements IErrorHandler | ||
| + | { | ||
| + | } | ||
| + | </file> | ||
| + | and all our workscreens should extend this base screen: | ||
| + | <file java> | ||
| + | public class FirstWorkScreen extends MyCustomWorkScreen | ||
| + | </file> | ||
| + | |||
| + | And the result will look like the following: | ||
| + | |||
| + | {{:applications:error_screen.jpg?nolink|}} | ||
