1 package fr.ifremer.quadrige2.ui.swing.common;
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 import fr.ifremer.quadrige2.ui.swing.common.action.AbstractAction;
28 import jaxx.runtime.swing.JAXXRuntimeException;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.nuiton.jaxx.application.ApplicationTechnicalException;
32 import org.nuiton.jaxx.application.swing.action.ApplicationActionException;
33 import org.springframework.security.access.AccessDeniedException;
34 import org.springframework.security.core.AuthenticationException;
35
36 import java.lang.reflect.InvocationTargetException;
37
38
39
40
41
42
43 public class ExceptionHandler implements Thread.UncaughtExceptionHandler {
44
45 private static final Log LOG = LogFactory.getLog(ExceptionHandler.class);
46
47
48 @Override
49 public void uncaughtException(Thread t, Throwable ex) {
50 handleException(t.getName(), ex);
51 }
52
53
54
55
56
57
58 public void handle(Throwable thrown) {
59
60 handleException(Thread.currentThread().getName(), thrown);
61 }
62
63
64
65
66
67
68
69 protected void handleException(String tname, Throwable ex) {
70 Throwable cause = ex;
71
72
73 while (cause.getCause() instanceof InvocationTargetException) {
74
75 cause = cause.getCause();
76 }
77 if (cause instanceof InvocationTargetException) {
78 cause = ((InvocationTargetException) cause).getTargetException();
79 }
80
81
82 if (cause instanceof ApplicationActionException) {
83 ApplicationActionException actionException = (ApplicationActionException) cause;
84 Throwable innerException = actionException.getCause();
85
86
87 if (actionException.getAction() instanceof AbstractAction
88 && (innerException instanceof AuthenticationException
89 || innerException instanceof AccessDeniedException)) {
90 return;
91 }
92
93
94 cause = innerException;
95 } else {
96
97 if (LOG.isErrorEnabled()) {
98 LOG.error("Global application exception [" + tname + "]", cause);
99 }
100
101 }
102
103 String message = cause.getMessage();
104
105 if (cause instanceof ApplicationTechnicalException) {
106 cause = cause.getCause();
107 }
108
109 if (cause instanceof JAXXRuntimeException) {
110 cause = cause.getCause();
111 message = cause.getMessage();
112 }
113
114
115 ApplicationUIContext.getInstance().getErrorHelper().showErrorDialog(message, cause);
116
117
118 }
119 }