source: josm/trunk/src/org/openstreetmap/josm/tools/BugReportExceptionHandler.java@ 3779

Last change on this file since 3779 was 3478, checked in by bastiK, 14 years ago

1) do not show bug report window for bugs that are thrown while bug handling is in progress; 2) do not paint the MapView while bug handling is in progress. (This prevents the cascaded error messages when exceptions is thrown by paint())

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