/
GUIException.java
153 lines (131 loc) · 5.07 KB
/
GUIException.java
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*******************************************************************************
* Copyright (c) 2004 Actuate Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Actuate Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.birt.report.designer.core.runtime;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.UnknownHostException;
import java.text.MessageFormat;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.model.api.util.StringUtil;
import org.eclipse.swt.SWTException;
/**
* The subclass which extends from BirtException used for GUI to wrap expected
* internal exception
*/
public class GUIException extends BirtException {
private static final long serialVersionUID = 1L;
private static final String MSG_FILE_NOT_FOUND = Messages.getString("ExceptionHandler.Message.FileNotFound"); //$NON-NLS-1$
private static final String MSG_UNKNOWN_HOST = Messages.getString("ExceptionHandler.Message.UnknownHost"); //$NON-NLS-1$
private static final String MSG_OUT_OF_MEMORY = Messages.getString("ExceptionHandler.Message.OutOfMemory"); //$NON-NLS-1$
private static final String MSG_UNEXPECTED_EXCEPTION_OCURR = Messages
.getString("ExceptionHandler.Meesage.UnexceptedExceptionOccur"); //$NON-NLS-1$
private static final String MSG_CAUSED_BY = Messages.getString("ExceptionHandler.Message.CausedBy"); //$NON-NLS-1$
public static final String GUI_ERROR_CODE_IO = "Error.GUIException.invokedByIOException"; //$NON-NLS-1$
public static final String GUI_ERROR_CODE_SWT = "Error.GUIException.invokedBySWTException"; //$NON-NLS-1$
public static final String GUI_ERROR_CODE_OUT_OF_MEMORY = "Error.GUIException.invokedByOutOfMemory"; //$NON-NLS-1$
public static final String GUI_ERROR_CODE_UNEXPECTED = "Error.GUIException.invokedByUnexpectedException"; //$NON-NLS-1$
/**
* Creates a new instance of GUI exception
*
* @param pluginId the id of the plugin
* @param cause the cause which invoked the exception
*
* @return the GUIException created
*/
public static GUIException createGUIException(String pluginId, Throwable cause) {
String errorCode = GUI_ERROR_CODE_UNEXPECTED;
if (cause instanceof IOException) {
errorCode = GUI_ERROR_CODE_IO;
} else if (cause instanceof OutOfMemoryError) {
errorCode = GUI_ERROR_CODE_OUT_OF_MEMORY;
} else if (cause instanceof SWTException) {
errorCode = GUI_ERROR_CODE_SWT;
}
GUIException ex = new GUIException(pluginId, errorCode, cause);
if (errorCode != GUI_ERROR_CODE_UNEXPECTED) {
ex.setSeverity(BirtException.INFO | BirtException.ERROR);
}
return ex;
}
/**
* Creates a new instance of GUI exception
*
* @param pluginId the id of the plugin
* @param cause the cause which invoked the exception
*
* @return the GUIException created
*/
public static GUIException createGUIException(String pluginId, Throwable cause, String errorCode) {
GUIException ex = new GUIException(pluginId, errorCode, cause);
if (!GUI_ERROR_CODE_UNEXPECTED.equals(errorCode)) {
ex.setSeverity(BirtException.INFO | BirtException.ERROR);
}
return ex;
}
/**
* Creates a new instance of GUI exception with the specified error code
*
* @param pluginId the id of the plugin
* @param errorCode the error code of the exception
* @param cause the cause which invoked the exception
*/
private GUIException(String pluginId, String errorCode, Throwable cause) {
super(pluginId, errorCode, null);
initCause(cause);
}
/*
* (non-Javadoc)
*
* @see java.lang.Throwable#getLocalizedMessage()
*/
public String getMessage() {
String message = Messages.getString(getErrorCode());
if (message.equalsIgnoreCase(getErrorCode())) {
message = getCause().getLocalizedMessage();
if (getCause() instanceof UnknownHostException) {
message = MSG_UNKNOWN_HOST + message;
} else if (getCause() instanceof FileNotFoundException) {
message = MSG_FILE_NOT_FOUND + message;
} else if (getCause() instanceof OutOfMemoryError) {
message = MSG_OUT_OF_MEMORY;
}
if (StringUtil.isBlank(message)) {
message = MessageFormat.format(MSG_CAUSED_BY, new String[] { getCause().getClass().getName() });
}
}
return message;
}
/*
* (non-Javadoc)
*
* @see java.lang.Throwable#getLocalizedMessage()
*/
public String getLocalizedMessage() {
return getMessage();
}
/**
* Returns the reason for error status
*
* @return the reason
*/
public String getReason() {
String reason = null;
if (getCause() instanceof OutOfMemoryError) {
reason = MSG_OUT_OF_MEMORY;
} else if (getCause() instanceof IOException || getCause() instanceof SWTException) {
reason = getLocalizedMessage();
} else {
reason = MSG_UNEXPECTED_EXCEPTION_OCURR;
}
return reason;
}
}