You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ErrorHandlers.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.vaadin.tests.errorhandler;
  2. import java.lang.reflect.InvocationTargetException;
  3. import com.vaadin.event.ListenerMethod.MethodException;
  4. import com.vaadin.server.DefaultErrorHandler;
  5. import com.vaadin.server.ErrorHandler;
  6. import com.vaadin.server.ServerRpcManager.RpcInvocationException;
  7. import com.vaadin.server.VaadinRequest;
  8. import com.vaadin.tests.components.AbstractReindeerTestUI;
  9. import com.vaadin.ui.Button;
  10. import com.vaadin.ui.Button.ClickEvent;
  11. import com.vaadin.ui.Button.ClickListener;
  12. import com.vaadin.ui.Component;
  13. import com.vaadin.ui.Label;
  14. import com.vaadin.ui.Notification;
  15. import com.vaadin.ui.Notification.Type;
  16. import com.vaadin.ui.VerticalLayout;
  17. public class ErrorHandlers extends AbstractReindeerTestUI {
  18. public static class NotificationErrorHandler implements ErrorHandler {
  19. @Override
  20. public void error(com.vaadin.server.ErrorEvent event) {
  21. Notification.show(getErrorMessage(event), Type.ERROR_MESSAGE);
  22. }
  23. }
  24. @Override
  25. protected void setup(VaadinRequest request) {
  26. addComponent(runtimeExceptionOnClick(new Button("Standard button")));
  27. addComponent(npeOnClick(new Button("Standard button with NPE")));
  28. Button customErrorButton = notificationErrorHandler(
  29. new Button("Button with notification error handler"));
  30. addComponent(runtimeExceptionOnClick(customErrorButton));
  31. final VerticalLayout layoutWithErrorHandler = new VerticalLayout(
  32. runtimeExceptionOnClick(new Button("Error handler on parent")));
  33. ErrorHandler e = event -> layoutWithErrorHandler.addComponent(
  34. new Label("Layout error: " + getErrorMessage(event)));
  35. layoutWithErrorHandler.setErrorHandler(e);
  36. layoutWithErrorHandler.addComponent(notificationErrorHandler(
  37. npeOnClick(new Button("Error handler on button and parent"))));
  38. addComponent(layoutWithErrorHandler);
  39. }
  40. private Button notificationErrorHandler(Button button) {
  41. button.setErrorHandler(new NotificationErrorHandler());
  42. return button;
  43. }
  44. protected static String getErrorMessage(
  45. com.vaadin.server.ErrorEvent event) {
  46. Component c = DefaultErrorHandler.findAbstractComponent(event);
  47. String errorMsg = "Error: '" + getMessage(event) + "' in ";
  48. errorMsg += c.getClass().getSimpleName() + " with caption '"
  49. + c.getCaption() + "'";
  50. return errorMsg;
  51. }
  52. private static String getMessage(com.vaadin.server.ErrorEvent event) {
  53. Throwable e = getUserCodeException(event);
  54. if (e.getMessage() != null) {
  55. return e.getMessage();
  56. } else {
  57. return e.getClass().getSimpleName();
  58. }
  59. }
  60. private static Throwable getUserCodeException(
  61. com.vaadin.server.ErrorEvent event) {
  62. Throwable t = event.getThrowable();
  63. if (t instanceof RpcInvocationException) {
  64. t = t.getCause();
  65. }
  66. if (t instanceof InvocationTargetException) {
  67. t = t.getCause();
  68. }
  69. if (t instanceof MethodException) {
  70. t = t.getCause();
  71. }
  72. return t;
  73. }
  74. private Button runtimeExceptionOnClick(Button customErrorButton) {
  75. customErrorButton.setCaption("RE: " + customErrorButton.getCaption());
  76. customErrorButton.addClickListener(new ClickListener() {
  77. @Override
  78. public void buttonClick(ClickEvent event) {
  79. throw new RuntimeException("Fail in click event");
  80. }
  81. });
  82. return customErrorButton;
  83. }
  84. private Button npeOnClick(Button customErrorButton) {
  85. customErrorButton.setCaption("NPE: " + customErrorButton.getCaption());
  86. customErrorButton.addClickListener(new ClickListener() {
  87. @Override
  88. public void buttonClick(ClickEvent event) {
  89. Integer i = null;
  90. i += 2;
  91. }
  92. });
  93. return customErrorButton;
  94. }
  95. @Override
  96. protected String getTestDescription() {
  97. // TODO Auto-generated method stub
  98. return null;
  99. }
  100. @Override
  101. protected Integer getTicketNumber() {
  102. // TODO Auto-generated method stub
  103. return null;
  104. }
  105. }