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