1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools.bugreport;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.GraphicsEnvironment;
|
---|
8 | import java.awt.GridBagConstraints;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.io.PrintWriter;
|
---|
12 | import java.io.StringWriter;
|
---|
13 |
|
---|
14 | import javax.swing.JButton;
|
---|
15 | import javax.swing.JCheckBox;
|
---|
16 | import javax.swing.JLabel;
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 | import javax.swing.SwingUtilities;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.actions.ReportBugAction;
|
---|
23 | import org.openstreetmap.josm.actions.ShowStatusReportAction;
|
---|
24 | import org.openstreetmap.josm.data.Version;
|
---|
25 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
26 | import org.openstreetmap.josm.gui.preferences.plugin.PluginPreference;
|
---|
27 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
28 | import org.openstreetmap.josm.gui.widgets.UrlLabel;
|
---|
29 | import org.openstreetmap.josm.plugins.PluginDownloadTask;
|
---|
30 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
31 | import org.openstreetmap.josm.tools.GBC;
|
---|
32 | import org.openstreetmap.josm.tools.WikiReader;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * An exception handler that asks the user to send a bug report.
|
---|
36 | *
|
---|
37 | * @author imi
|
---|
38 | * @since 40
|
---|
39 | */
|
---|
40 | public final class BugReportExceptionHandler implements Thread.UncaughtExceptionHandler {
|
---|
41 |
|
---|
42 | private static boolean handlingInProgress;
|
---|
43 | private static volatile BugReporterThread bugReporterThread;
|
---|
44 | private static int exceptionCounter;
|
---|
45 | private static boolean suppressExceptionDialogs;
|
---|
46 |
|
---|
47 | static final class BugReporterThread extends Thread {
|
---|
48 |
|
---|
49 | private final class BugReporterWorker implements Runnable {
|
---|
50 | private final PluginDownloadTask pluginDownloadTask;
|
---|
51 |
|
---|
52 | private BugReporterWorker(PluginDownloadTask pluginDownloadTask) {
|
---|
53 | this.pluginDownloadTask = pluginDownloadTask;
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public void run() {
|
---|
58 | // Then ask for submitting a bug report, for exceptions thrown from a plugin too, unless updated to a new version
|
---|
59 | if (pluginDownloadTask == null) {
|
---|
60 | askForBugReport(e);
|
---|
61 | } else {
|
---|
62 | // Ask for restart to install new plugin
|
---|
63 | PluginPreference.notifyDownloadResults(
|
---|
64 | Main.parent, pluginDownloadTask, !pluginDownloadTask.getDownloadedPlugins().isEmpty());
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | private final Throwable e;
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Constructs a new {@code BugReporterThread}.
|
---|
73 | * @param t the exception
|
---|
74 | */
|
---|
75 | private BugReporterThread(Throwable t) {
|
---|
76 | super("Bug Reporter");
|
---|
77 | this.e = t;
|
---|
78 | }
|
---|
79 |
|
---|
80 | static void askForBugReport(final Throwable e) {
|
---|
81 | String[] buttonTexts = new String[] {tr("Do nothing"), tr("Report Bug")};
|
---|
82 | String[] buttonIcons = new String[] {"cancel", "bug"};
|
---|
83 | int defaultButtonIdx = 1;
|
---|
84 | String message = tr("An unexpected exception occurred.<br>" +
|
---|
85 | "This is always a coding error. If you are running the latest<br>" +
|
---|
86 | "version of JOSM, please consider being kind and file a bug report."
|
---|
87 | );
|
---|
88 | // Check user is running current tested version, the error may already be fixed
|
---|
89 | int josmVersion = Version.getInstance().getVersion();
|
---|
90 | if (josmVersion != Version.JOSM_UNKNOWN_VERSION) {
|
---|
91 | try {
|
---|
92 | int latestVersion = Integer.parseInt(new WikiReader().
|
---|
93 | read(Main.getJOSMWebsite()+"/wiki/TestedVersion?format=txt").trim());
|
---|
94 | if (latestVersion > josmVersion) {
|
---|
95 | buttonTexts = new String[] {tr("Do nothing"), tr("Update JOSM"), tr("Report Bug")};
|
---|
96 | buttonIcons = new String[] {"cancel", "download", "bug"};
|
---|
97 | defaultButtonIdx = 2;
|
---|
98 | message = tr("An unexpected exception occurred. This is always a coding error.<br><br>" +
|
---|
99 | "However, you are running an old version of JOSM ({0}),<br>" +
|
---|
100 | "instead of using the current tested version (<b>{1}</b>).<br><br>"+
|
---|
101 | "<b>Please update JOSM</b> before considering to file a bug report.",
|
---|
102 | String.valueOf(josmVersion), String.valueOf(latestVersion));
|
---|
103 | }
|
---|
104 | } catch (IOException | NumberFormatException ex) {
|
---|
105 | Main.warn("Unable to detect latest version of JOSM: "+ex.getMessage());
|
---|
106 | }
|
---|
107 | }
|
---|
108 | // Build panel
|
---|
109 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
110 | pnl.add(new JLabel("<html>" + message + "</html>"), GBC.eol());
|
---|
111 | JCheckBox cbSuppress = null;
|
---|
112 | if (exceptionCounter > 1) {
|
---|
113 | cbSuppress = new JCheckBox(tr("Suppress further error dialogs for this session."));
|
---|
114 | pnl.add(cbSuppress, GBC.eol());
|
---|
115 | }
|
---|
116 | if (GraphicsEnvironment.isHeadless()) {
|
---|
117 | return;
|
---|
118 | }
|
---|
119 | // Show dialog
|
---|
120 | ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Unexpected Exception"), buttonTexts);
|
---|
121 | ed.setButtonIcons(buttonIcons);
|
---|
122 | ed.setIcon(JOptionPane.ERROR_MESSAGE);
|
---|
123 | ed.setCancelButton(1);
|
---|
124 | ed.setDefaultButton(defaultButtonIdx);
|
---|
125 | ed.setContent(pnl);
|
---|
126 | ed.setFocusOnDefaultButton(true);
|
---|
127 | ed.showDialog();
|
---|
128 | if (cbSuppress != null && cbSuppress.isSelected()) {
|
---|
129 | suppressExceptionDialogs = true;
|
---|
130 | }
|
---|
131 | if (ed.getValue() <= 1) {
|
---|
132 | // "Do nothing"
|
---|
133 | return;
|
---|
134 | } else if (ed.getValue() < buttonTexts.length) {
|
---|
135 | // "Update JOSM"
|
---|
136 | try {
|
---|
137 | Main.platform.openUrl(Main.getJOSMWebsite());
|
---|
138 | } catch (IOException ex) {
|
---|
139 | Main.warn("Unable to access JOSM website: "+ex.getMessage());
|
---|
140 | }
|
---|
141 | } else {
|
---|
142 | // "Report bug"
|
---|
143 | try {
|
---|
144 | JPanel p = buildPanel(e);
|
---|
145 | JOptionPane.showMessageDialog(Main.parent, p, tr("You have encountered a bug in JOSM"), JOptionPane.ERROR_MESSAGE);
|
---|
146 | } catch (RuntimeException ex) {
|
---|
147 | Main.error(ex);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Override
|
---|
153 | public void run() {
|
---|
154 | // Give the user a chance to deactivate the plugin which threw the exception (if it was thrown from a plugin)
|
---|
155 | SwingUtilities.invokeLater(new BugReporterWorker(PluginHandler.updateOrdisablePluginAfterException(e)));
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | @Override
|
---|
160 | public void uncaughtException(Thread t, Throwable e) {
|
---|
161 | handleException(e);
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Handles the given exception
|
---|
166 | * @param e the exception
|
---|
167 | */
|
---|
168 | public static void handleException(final Throwable e) {
|
---|
169 | if (handlingInProgress || suppressExceptionDialogs)
|
---|
170 | return; // we do not handle secondary exceptions, this gets too messy
|
---|
171 | if (bugReporterThread != null && bugReporterThread.isAlive())
|
---|
172 | return;
|
---|
173 | handlingInProgress = true;
|
---|
174 | exceptionCounter++;
|
---|
175 | try {
|
---|
176 | Main.error(e);
|
---|
177 | if (Main.parent != null) {
|
---|
178 | if (e instanceof OutOfMemoryError) {
|
---|
179 | // do not translate the string, as translation may raise an exception
|
---|
180 | JOptionPane.showMessageDialog(Main.parent, "JOSM is out of memory. " +
|
---|
181 | "Strange things may happen.\nPlease restart JOSM with the -Xmx###M option,\n" +
|
---|
182 | "where ### is the number of MB assigned to JOSM (e.g. 256).\n" +
|
---|
183 | "Currently, " + Runtime.getRuntime().maxMemory()/1024/1024 + " MB are available to JOSM.",
|
---|
184 | "Error",
|
---|
185 | JOptionPane.ERROR_MESSAGE
|
---|
186 | );
|
---|
187 | return;
|
---|
188 | }
|
---|
189 |
|
---|
190 | bugReporterThread = new BugReporterThread(e);
|
---|
191 | bugReporterThread.start();
|
---|
192 | }
|
---|
193 | } finally {
|
---|
194 | handlingInProgress = false;
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | static JPanel buildPanel(final Throwable e) {
|
---|
199 | StringWriter stack = new StringWriter();
|
---|
200 | e.printStackTrace(new PrintWriter(stack));
|
---|
201 |
|
---|
202 | String text = ShowStatusReportAction.getReportHeader() + stack.getBuffer().toString();
|
---|
203 | text = text.replaceAll("\r", "");
|
---|
204 |
|
---|
205 | JPanel p = new JPanel(new GridBagLayout());
|
---|
206 | p.add(new JMultilineLabel(
|
---|
207 | tr("You have encountered an error in JOSM. Before you file a bug report " +
|
---|
208 | "make sure you have updated to the latest version of JOSM here:")),
|
---|
209 | GBC.eol().fill(GridBagConstraints.HORIZONTAL));
|
---|
210 | p.add(new UrlLabel(Main.getJOSMWebsite(), 2), GBC.eop().insets(8, 0, 0, 0));
|
---|
211 | p.add(new JMultilineLabel(
|
---|
212 | tr("You should also update your plugins. If neither of those help please " +
|
---|
213 | "file a bug report in our bugtracker using this link:")),
|
---|
214 | GBC.eol().fill(GridBagConstraints.HORIZONTAL));
|
---|
215 | p.add(new JButton(new ReportBugAction(text)), GBC.eop().insets(8, 0, 0, 0));
|
---|
216 | p.add(new JMultilineLabel(
|
---|
217 | tr("There the error information provided below should already be " +
|
---|
218 | "filled in for you. Please include information on how to reproduce " +
|
---|
219 | "the error and try to supply as much detail as possible.")),
|
---|
220 | GBC.eop().fill(GridBagConstraints.HORIZONTAL));
|
---|
221 | p.add(new JMultilineLabel(
|
---|
222 | tr("Alternatively, if that does not work you can manually fill in the information " +
|
---|
223 | "below at this URL:")), GBC.eol().fill(GridBagConstraints.HORIZONTAL));
|
---|
224 | p.add(new UrlLabel(Main.getJOSMWebsite()+"/newticket", 2), GBC.eop().insets(8, 0, 0, 0));
|
---|
225 |
|
---|
226 | // Wiki formatting for manual copy-paste
|
---|
227 | DebugTextDisplay textarea = new DebugTextDisplay(text);
|
---|
228 |
|
---|
229 | if (textarea.copyToClippboard()) {
|
---|
230 | p.add(new JLabel(tr("(The text has already been copied to your clipboard.)")),
|
---|
231 | GBC.eop().fill(GridBagConstraints.HORIZONTAL));
|
---|
232 | }
|
---|
233 |
|
---|
234 | p.add(textarea, GBC.eop().fill());
|
---|
235 |
|
---|
236 | for (Component c: p.getComponents()) {
|
---|
237 | if (c instanceof JMultilineLabel) {
|
---|
238 | ((JMultilineLabel) c).setMaxWidth(400);
|
---|
239 | }
|
---|
240 | }
|
---|
241 | return p;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Determines if an exception is currently being handled
|
---|
246 | * @return {@code true} if an exception is currently being handled, {@code false} otherwise
|
---|
247 | */
|
---|
248 | public static boolean exceptionHandlingInProgress() {
|
---|
249 | return handlingInProgress;
|
---|
250 | }
|
---|
251 | }
|
---|