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

Last change on this file since 6113 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

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