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