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