summaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/errorhandler/ErrorHandlers.java
blob: a123af0f7dae5ffb9e8eb5d7b5ad6a008ca57cd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.vaadin.tests.errorhandler;

import java.lang.reflect.InvocationTargetException;

import com.vaadin.event.ListenerMethod.MethodException;
import com.vaadin.server.DefaultErrorHandler;
import com.vaadin.server.ErrorHandler;
import com.vaadin.server.ServerRpcManager.RpcInvocationException;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.VerticalLayout;

public class ErrorHandlers extends AbstractTestUI {

    public static class NotificationErrorHandler implements ErrorHandler {

        @Override
        public void error(com.vaadin.server.ErrorEvent event) {
            Notification.show(getErrorMessage(event), Type.ERROR_MESSAGE);
        }

    }

    @Override
    protected void setup(VaadinRequest request) {
        addComponent(runtimeExceptionOnClick(new Button("Standard button")));
        addComponent(npeOnClick(new Button("Standard button with NPE")));
        Button customErrorButton = notificationErrorHandler(new Button(
                "Button with notification error handler"));
        addComponent(runtimeExceptionOnClick(customErrorButton));

        final VerticalLayout layoutWithErrorHandler = new VerticalLayout(
                runtimeExceptionOnClick(new Button("Error handler on parent")));
        ErrorHandler e = new ErrorHandler() {

            @Override
            public void error(com.vaadin.server.ErrorEvent event) {
                layoutWithErrorHandler.addComponent(new Label("Layout error: "
                        + getErrorMessage(event)));
            }

        };
        layoutWithErrorHandler.setErrorHandler(e);
        layoutWithErrorHandler
                .addComponent(notificationErrorHandler(npeOnClick(new Button(
                        "Error handler on button and parent"))));
        addComponent(layoutWithErrorHandler);
    }

    private Button notificationErrorHandler(Button button) {
        button.setErrorHandler(new NotificationErrorHandler());
        return button;
    }

    protected static String getErrorMessage(com.vaadin.server.ErrorEvent event) {
        Component c = DefaultErrorHandler.findAbstractComponent(event);
        String errorMsg = "Error: '" + getMessage(event) + "' in ";
        errorMsg += c.getClass().getSimpleName() + " with caption '"
                + c.getCaption() + "'";
        return errorMsg;
    }

    private static String getMessage(com.vaadin.server.ErrorEvent event) {
        Throwable e = getUserCodeException(event);
        if (e.getMessage() != null) {
            return e.getMessage();
        } else {
            return e.getClass().getSimpleName();
        }
    }

    private static Throwable getUserCodeException(
            com.vaadin.server.ErrorEvent event) {
        Throwable t = event.getThrowable();
        if (t instanceof RpcInvocationException) {
            t = t.getCause();
        }
        if (t instanceof InvocationTargetException) {
            t = t.getCause();
        }
        if (t instanceof MethodException) {
            t = t.getCause();
        }

        return t;

    }

    private Button runtimeExceptionOnClick(Button customErrorButton) {
        customErrorButton.setCaption("RE: " + customErrorButton.getCaption());

        customErrorButton.addClickListener(new ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                throw new RuntimeException("Fail in click event");
            }
        });
        return customErrorButton;
    }

    private Button npeOnClick(Button customErrorButton) {
        customErrorButton.setCaption("NPE: " + customErrorButton.getCaption());
        customErrorButton.addClickListener(new ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                Integer i = null;
                i += 2;
            }
        });
        return customErrorButton;
    }

    @Override
    protected String getTestDescription() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected Integer getTicketNumber() {
        // TODO Auto-generated method stub
        return null;
    }

}