Project

General

Profile

Download (1.8 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2018 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.vaadin.event.error;
11

    
12
import java.util.HashMap;
13
import java.util.Map;
14

    
15
import org.apache.log4j.Logger;
16

    
17
import com.vaadin.server.ErrorEvent;
18
import com.vaadin.server.ErrorHandler;
19
import com.vaadin.ui.Notification;
20

    
21
/**
22
 * Vaadin allows setting an com.vaadin.server.ErrorHandler for UIs and components.
23
 * The caveat with this built in approach is that there is only one {@link com.vaadin.server.ErrorHandler}
24
 * for any type of errors. This <code>DelegatingErrorHandler</code> allows registering handlers for specific types of errors.
25
 *
26
 * see https://dev.e-taxonomy.eu/redmine/issues/7241
27
 *
28
 * @author freimeier
29
 * @date 26.02.2018
30
 *
31
 */
32
public class DelegatingErrorHandler implements ErrorHandler{
33

    
34
    private static final long serialVersionUID = 3378605204517477112L;
35

    
36
    Map<Class<? extends Exception>, ErrorTypeHandler<? extends Exception>> handlerMap = new HashMap<>();
37

    
38
    public <E extends Exception> void  registerHandler(Class<E> type, ErrorTypeHandler<E> handler) {
39
        handlerMap.put(type, handler);
40
    }
41

    
42
    /* (non-Javadoc)
43
     * @see com.vaadin.server.ErrorHandler#error(com.vaadin.server.ErrorEvent)
44
     */
45
    @Override
46
    public void error(ErrorEvent event) {
47

    
48
        Class<? extends Throwable> errorClass = event.getThrowable().getCause().getClass();
49
        Logger.getLogger(this.getClass()).debug(errorClass);
50
        if(handlerMap.get(errorClass) != null){
51
            handlerMap.get(errorClass).error(event);
52
          } else {
53
            Notification.show(event.getThrowable().getMessage());
54
          }
55
    }
56
}
(1-1/3)