View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.admincentral;
35  
36  import info.magnolia.i18nsystem.SimpleTranslator;
37  import info.magnolia.ui.api.message.Message;
38  import info.magnolia.ui.api.message.MessageType;
39  import info.magnolia.ui.framework.message.MessagesManager;
40  
41  import java.lang.reflect.InvocationTargetException;
42  
43  import javax.inject.Inject;
44  
45  import org.apache.commons.lang3.StringUtils;
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  import com.vaadin.event.ListenerMethod;
50  import com.vaadin.server.ErrorEvent;
51  import com.vaadin.server.ErrorHandler;
52  import com.vaadin.server.ServerRpcManager;
53  
54  /**
55   * The {@link AdmincentralErrorHandler} logs unhandled exceptions and sends error messages to the pulse.
56   * <p>
57   * It replaces Vaadin's default behavior for component errors, which would otherwise display error icons and stack traces in tooltips.
58   */
59  public class AdmincentralErrorHandler implements ErrorHandler {
60  
61      private static final Logger log = LoggerFactory.getLogger(AdmincentralErrorHandler.class);
62  
63      private static final String DEFAULT_MESSAGE = "ui.error.default";
64  
65      private final MessagesManager messagesManager;
66  
67      private final SimpleTranslator i18n;
68  
69      @Inject
70      public AdmincentralErrorHandler(MessagesManager messagesManager, SimpleTranslator i18n) {
71          this.messagesManager = messagesManager;
72          this.i18n = i18n;
73      }
74  
75      @Override
76      public void error(ErrorEvent event) {
77          log.error(i18n.translate(DEFAULT_MESSAGE), event.getThrowable());
78  
79          Message message = getErrorMessage(event.getThrowable());
80          messagesManager.sendLocalMessage(message);
81      }
82  
83      private Message getErrorMessage(Throwable e) {
84  
85          Message message = new Message();
86          message.setType(MessageType.ERROR);
87  
88          addMessageDetails(message, e);
89  
90          // append details for RPC exceptions
91          if (e instanceof ServerRpcManager.RpcInvocationException) {
92              e = e.getCause();
93              addMessageDetails(message, e);
94          }
95          if (e instanceof InvocationTargetException) {
96              e = ((InvocationTargetException) e).getTargetException();
97              addMessageDetails(message, e);
98          }
99          if (e instanceof ListenerMethod.MethodException) {
100             e = e.getCause();
101             addMessageDetails(message, e);
102         }
103 
104         // append other potential causes
105         while (e != null && e != e.getCause()) {
106             e = e.getCause();
107             addMessageDetails(message, e);
108         }
109 
110         if (StringUtils.isBlank(message.getSubject())) {
111             message.setSubject(i18n.translate(DEFAULT_MESSAGE));
112         }
113 
114         return message;
115     }
116 
117     private void addMessageDetails(Message message, Throwable e) {
118         if (e != null) {
119 
120             // message details
121             String content = message.getMessage();
122             if (content == null) {
123                 content = "";
124             } else {
125                 content += "\n" + i18n.translate("ui.error.causedby") + " ";
126             }
127             content += e.getClass().getSimpleName();
128             if (StringUtils.isNotBlank(e.getMessage())) {
129                 content += ": " + e.getMessage();
130 
131                 // message subject
132                 message.setSubject(e.getMessage());
133             }
134             message.setMessage(content);
135         }
136     }
137 }