| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others |
|---|
| 2 | package org.openstreetmap.josm.tools; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 5 | |
|---|
| 6 | import java.awt.Component; |
|---|
| 7 | import java.awt.GridBagLayout; |
|---|
| 8 | import java.io.PrintWriter; |
|---|
| 9 | import java.io.StringWriter; |
|---|
| 10 | import java.net.URL; |
|---|
| 11 | import java.nio.ByteBuffer; |
|---|
| 12 | |
|---|
| 13 | import javax.swing.JCheckBox; |
|---|
| 14 | import javax.swing.JLabel; |
|---|
| 15 | import javax.swing.JOptionPane; |
|---|
| 16 | import javax.swing.JPanel; |
|---|
| 17 | import javax.swing.JScrollPane; |
|---|
| 18 | import javax.swing.JTextArea; |
|---|
| 19 | import javax.swing.SwingUtilities; |
|---|
| 20 | |
|---|
| 21 | import org.openstreetmap.josm.Main; |
|---|
| 22 | import org.openstreetmap.josm.actions.ShowStatusReportAction; |
|---|
| 23 | import org.openstreetmap.josm.gui.ExtendedDialog; |
|---|
| 24 | import org.openstreetmap.josm.gui.JMultilineLabel; |
|---|
| 25 | import org.openstreetmap.josm.plugins.PluginHandler; |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * An exception handler that asks the user to send a bug report. |
|---|
| 29 | * |
|---|
| 30 | * @author imi |
|---|
| 31 | */ |
|---|
| 32 | public final class BugReportExceptionHandler implements Thread.UncaughtExceptionHandler { |
|---|
| 33 | |
|---|
| 34 | private static boolean handlingInProgress = false; |
|---|
| 35 | private static int exceptionCounter = 0; |
|---|
| 36 | private static boolean suppressExceptionDialogs = false; |
|---|
| 37 | |
|---|
| 38 | public void uncaughtException(Thread t, Throwable e) { |
|---|
| 39 | handleException(e); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | //http://stuffthathappens.com/blog/2007/10/15/one-more-note-on-uncaught-exception-handlers/ |
|---|
| 43 | public void handle(Throwable t) { |
|---|
| 44 | handleException(t); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | public static void handleException(final Throwable e) { |
|---|
| 48 | if (handlingInProgress) |
|---|
| 49 | return; // we do not handle secondary exceptions, this gets too messy |
|---|
| 50 | if (suppressExceptionDialogs) |
|---|
| 51 | return; |
|---|
| 52 | handlingInProgress = true; |
|---|
| 53 | exceptionCounter++; |
|---|
| 54 | try { |
|---|
| 55 | e.printStackTrace(); |
|---|
| 56 | if (Main.parent != null) { |
|---|
| 57 | if (e instanceof OutOfMemoryError) { |
|---|
| 58 | // do not translate the string, as translation may raise an exception |
|---|
| 59 | JOptionPane.showMessageDialog(Main.parent, "JOSM is out of memory. " + |
|---|
| 60 | "Strange things may happen.\nPlease restart JOSM with the -Xmx###M option,\n" + |
|---|
| 61 | "where ### is the number of MB assigned to JOSM (e.g. 256).\n" + |
|---|
| 62 | "Currently, " + Runtime.getRuntime().maxMemory()/1024/1024 + " MB are available to JOSM.", |
|---|
| 63 | "Error", |
|---|
| 64 | JOptionPane.ERROR_MESSAGE |
|---|
| 65 | ); |
|---|
| 66 | return; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | SwingUtilities.invokeLater(new Runnable() { |
|---|
| 71 | @Override |
|---|
| 72 | public void run() { |
|---|
| 73 | // Give the user a chance to deactivate the plugin which threw the exception (if it |
|---|
| 74 | // was thrown from a plugin) |
|---|
| 75 | // |
|---|
| 76 | PluginHandler.disablePluginAfterException(e); |
|---|
| 77 | |
|---|
| 78 | // Then ask for submitting a bug report, for exceptions thrown from a plugin too |
|---|
| 79 | // |
|---|
| 80 | ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Unexpected Exception"), new String[] {tr("Do nothing"), tr("Report Bug")}); |
|---|
| 81 | ed.setIcon(JOptionPane.ERROR_MESSAGE); |
|---|
| 82 | JPanel pnl = new JPanel(new GridBagLayout()); |
|---|
| 83 | pnl.add(new JLabel( |
|---|
| 84 | "<html>" |
|---|
| 85 | + tr("An unexpected exception occurred.<br>" + |
|---|
| 86 | "This is always a coding error. If you are running the latest<br>" + |
|---|
| 87 | "version of JOSM, please consider being kind and file a bug report." |
|---|
| 88 | ) |
|---|
| 89 | + "</html>"), GBC.eol()); |
|---|
| 90 | JCheckBox cbSuppress = null; |
|---|
| 91 | if (exceptionCounter > 1) { |
|---|
| 92 | cbSuppress = new JCheckBox(tr("Suppress further error dialogs for this session.")); |
|---|
| 93 | pnl.add(cbSuppress, GBC.eol()); |
|---|
| 94 | } |
|---|
| 95 | ed.setContent(pnl); |
|---|
| 96 | ed.showDialog(); |
|---|
| 97 | if (cbSuppress != null && cbSuppress.isSelected()) { |
|---|
| 98 | suppressExceptionDialogs = true; |
|---|
| 99 | } |
|---|
| 100 | if (ed.getValue() != 2) return; |
|---|
| 101 | |
|---|
| 102 | try { |
|---|
| 103 | final int maxlen = 6000; |
|---|
| 104 | StringWriter stack = new StringWriter(); |
|---|
| 105 | e.printStackTrace(new PrintWriter(stack)); |
|---|
| 106 | |
|---|
| 107 | String text = ShowStatusReportAction.getReportHeader() |
|---|
| 108 | + stack.getBuffer().toString(); |
|---|
| 109 | String urltext = text.replaceAll("\r",""); /* strip useless return chars */ |
|---|
| 110 | if(urltext.length() > maxlen) |
|---|
| 111 | { |
|---|
| 112 | urltext = urltext.substring(0,maxlen); |
|---|
| 113 | int idx = urltext.lastIndexOf("\n"); |
|---|
| 114 | /* cut whole line when not loosing too much */ |
|---|
| 115 | if(maxlen-idx < 200) { |
|---|
| 116 | urltext = urltext.substring(0,idx+1); |
|---|
| 117 | } |
|---|
| 118 | urltext += "...<snip>...\n"; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | URL url = new URL("http://josm.openstreetmap.de/josmticket?" + |
|---|
| 122 | "tdata="+Base64.encode(ByteBuffer.wrap(urltext.getBytes("UTF8")), true)); |
|---|
| 123 | |
|---|
| 124 | JPanel p = new JPanel(new GridBagLayout()); |
|---|
| 125 | p.add(new JMultilineLabel( |
|---|
| 126 | tr("You have encountered an error in JOSM. Before you file a bug report " + |
|---|
| 127 | "make sure you have updated to the latest version of JOSM here:")), GBC.eol()); |
|---|
| 128 | p.add(new UrlLabel("http://josm.openstreetmap.de/#Download",2), GBC.eop().insets(8,0,0,0)); |
|---|
| 129 | p.add(new JMultilineLabel( |
|---|
| 130 | tr("You should also update your plugins. If neither of those help please " + |
|---|
| 131 | "file a bug report in our bugtracker using this link:")), GBC.eol()); |
|---|
| 132 | p.add(new UrlLabel(url.toString(), "http://josm.openstreetmap.de/josmticket?...",2), GBC.eop().insets(8,0,0,0)); |
|---|
| 133 | p.add(new JMultilineLabel( |
|---|
| 134 | tr("There the error information provided below should already be " + |
|---|
| 135 | "filled in for you. Please include information on how to reproduce " + |
|---|
| 136 | "the error and try to supply as much detail as possible.")), GBC.eop()); |
|---|
| 137 | p.add(new JMultilineLabel( |
|---|
| 138 | tr("Alternatively, if that does not work you can manually fill in the information " + |
|---|
| 139 | "below at this URL:")), GBC.eol()); |
|---|
| 140 | p.add(new UrlLabel("http://josm.openstreetmap.de/newticket",2), GBC.eop().insets(8,0,0,0)); |
|---|
| 141 | if (Utils.copyToClipboard(text)) { |
|---|
| 142 | p.add(new JLabel(tr("(The text has already been copied to your clipboard.)")), GBC.eop()); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | JTextArea info = new JTextArea(text, 18, 60); |
|---|
| 146 | info.setCaretPosition(0); |
|---|
| 147 | info.setEditable(false); |
|---|
| 148 | p.add(new JScrollPane(info), GBC.eop()); |
|---|
| 149 | |
|---|
| 150 | for (Component c: p.getComponents()) { |
|---|
| 151 | if (c instanceof JMultilineLabel) { |
|---|
| 152 | ((JMultilineLabel)c).setMaxWidth(400); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | JOptionPane.showMessageDialog(Main.parent, p, tr("You have encountered a bug in JOSM"), JOptionPane.ERROR_MESSAGE); |
|---|
| 157 | } catch (Exception e1) { |
|---|
| 158 | e1.printStackTrace(); |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | }); |
|---|
| 162 | } |
|---|
| 163 | } finally { |
|---|
| 164 | handlingInProgress = false; |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | public static boolean exceptionHandlingInProgress() { |
|---|
| 168 | return handlingInProgress; |
|---|
| 169 | } |
|---|
| 170 | } |
|---|