1
|
/**
|
2
|
* Copyright (C) 2018 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.vaadin.event.error;
|
10
|
|
11
|
import java.lang.reflect.InvocationTargetException;
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.List;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
|
17
|
import com.vaadin.event.ListenerMethod;
|
18
|
import com.vaadin.server.ErrorEvent;
|
19
|
import com.vaadin.server.ErrorHandler;
|
20
|
import com.vaadin.server.ServerRpcManager.RpcInvocationException;
|
21
|
import com.vaadin.ui.Notification;
|
22
|
|
23
|
/**
|
24
|
* Vaadin allows setting an com.vaadin.server.ErrorHandler for UIs and components.
|
25
|
* The caveat with this built in approach is that there is only one {@link com.vaadin.server.ErrorHandler}
|
26
|
* for any type of errors. This <code>DelegatingErrorHandler</code> allows registering handlers for specific types of errors.
|
27
|
*
|
28
|
* see https://dev.e-taxonomy.eu/redmine/issues/7241
|
29
|
*
|
30
|
* @author freimeier
|
31
|
* @author a.kohlbecker
|
32
|
*
|
33
|
*/
|
34
|
public class DelegatingErrorHandler implements ErrorHandler{
|
35
|
|
36
|
private static final long serialVersionUID = 3378605204517477112L;
|
37
|
|
38
|
List<ErrorTypeHandler<? extends Throwable>> handlers = new ArrayList<>();
|
39
|
|
40
|
public <E extends Throwable> void registerHandler(ErrorTypeHandler<E> handler) {
|
41
|
assert findHandler(handler.supports()) == null;
|
42
|
handlers.add(handler);
|
43
|
}
|
44
|
|
45
|
@SuppressWarnings("unchecked")
|
46
|
public <E extends Throwable> ErrorTypeHandler<E> findHandler(Class<E> errorClass){
|
47
|
for(ErrorTypeHandler<?> h : handlers){
|
48
|
if(h.supports().isAssignableFrom(errorClass)){
|
49
|
return (ErrorTypeHandler<E>) h;
|
50
|
}
|
51
|
}
|
52
|
return null;
|
53
|
|
54
|
}
|
55
|
|
56
|
/* (non-Javadoc)
|
57
|
* @see com.vaadin.server.ErrorHandler#error(com.vaadin.server.ErrorEvent)
|
58
|
*/
|
59
|
@Override
|
60
|
public void error(ErrorEvent event) {
|
61
|
|
62
|
Throwable throwable = event.getThrowable();
|
63
|
while(throwable != null && (
|
64
|
RpcInvocationException.class.isAssignableFrom(throwable.getClass()) ||
|
65
|
InvocationTargetException.class.isAssignableFrom(throwable.getClass()) ||
|
66
|
ListenerMethod.MethodException.class.isAssignableFrom(throwable.getClass())
|
67
|
)
|
68
|
){
|
69
|
// we are only interested into the cause in these cases
|
70
|
throwable = throwable.getCause().getCause();
|
71
|
event.setThrowable(throwable);
|
72
|
}
|
73
|
while(throwable != null){
|
74
|
if(delegate(event, throwable)){
|
75
|
break;
|
76
|
}
|
77
|
throwable = throwable.getCause();
|
78
|
}
|
79
|
if(event.getThrowable() != null){
|
80
|
Notification.show(event.getThrowable().getMessage());
|
81
|
}
|
82
|
}
|
83
|
|
84
|
private <E extends Throwable> boolean delegate(ErrorEvent event, E throwable){
|
85
|
|
86
|
Class<E> errorClass = (Class<E>) throwable.getClass();
|
87
|
Logger.getLogger(this.getClass()).debug(errorClass);
|
88
|
ErrorTypeHandler<E> handler = findHandler(errorClass);
|
89
|
if(handler != null){
|
90
|
handler.handleError(event, throwable);
|
91
|
return true;
|
92
|
}
|
93
|
return false;
|
94
|
}
|
95
|
}
|