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

Last change on this file since 6717 was 6643, checked in by Don-vip, 10 years ago

global replacement of e.printStackTrace() by Main.error(e)

  • Property svn:eol-style set to native
File size: 10.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
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.widgets.JMultilineLabel;
27import org.openstreetmap.josm.gui.widgets.JosmTextArea;
28import org.openstreetmap.josm.gui.widgets.UrlLabel;
29import org.openstreetmap.josm.plugins.PluginHandler;
30
31/**
32 * An exception handler that asks the user to send a bug report.
33 *
34 * @author imi
35 */
36public final class BugReportExceptionHandler implements Thread.UncaughtExceptionHandler {
37
38 private static boolean handlingInProgress = false;
39 private static int exceptionCounter = 0;
40 private static boolean suppressExceptionDialogs = false;
41
42 @Override
43 public void uncaughtException(Thread t, Throwable e) {
44 handleException(e);
45 }
46
47 //http://stuffthathappens.com/blog/2007/10/15/one-more-note-on-uncaught-exception-handlers/
48 /**
49 * Handles the given throwable object
50 * @param t The throwable object
51 */
52 public void handle(Throwable t) {
53 handleException(t);
54 }
55
56 /**
57 * Handles the given exception
58 * @param e the exception
59 */
60 public static void handleException(final Throwable e) {
61 if (handlingInProgress)
62 return; // we do not handle secondary exceptions, this gets too messy
63 if (suppressExceptionDialogs)
64 return;
65 handlingInProgress = true;
66 exceptionCounter++;
67 try {
68 Main.error(e);
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
78 );
79 return;
80 }
81
82
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);
90
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());
107 }
108 ed.setContent(pnl);
109 ed.showDialog();
110 if (cbSuppress != null && cbSuppress.isSelected()) {
111 suppressExceptionDialogs = true;
112 }
113 if (ed.getValue() != 2) return;
114
115 try {
116 final int maxlen = 6000;
117 StringWriter stack = new StringWriter();
118 e.printStackTrace(new PrintWriter(stack));
119
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);
126 int idx = urltext.lastIndexOf('\n');
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 }
133
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());
138 p.add(new UrlLabel(Main.JOSM_WEBSITE,2), GBC.eop().insets(8,0,0,0));
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());
142 p.add(getBugReportUrlLabel(urltext), GBC.eop().insets(8,0,0,0));
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());
150 p.add(new UrlLabel(Main.JOSM_WEBSITE+"/newticket",2), GBC.eop().insets(8,0,0,0));
151 if (Utils.copyToClipboard(text)) {
152 p.add(new JLabel(tr("(The text has already been copied to your clipboard.)")), GBC.eop());
153 }
154
155 JosmTextArea info = new JosmTextArea(text, 18, 60);
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 Main.error(e1);
169 }
170 }
171 });
172 }
173 } finally {
174 handlingInProgress = false;
175 }
176 }
177
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 */
182 public static boolean exceptionHandlingInProgress() {
183 return handlingInProgress;
184 }
185
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(Utils.UTF_8));
197 Utils.close(gzip);
198
199 return new URL(Main.JOSM_WEBSITE+"/josmticket?" +
200 "gdata="+Base64.encode(ByteBuffer.wrap(out.toByteArray()), true));
201 } catch (IOException e) {
202 Main.error(e);
203 return null;
204 }
205 }
206
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) {
216 return new UrlLabel(url.toString(), Main.JOSM_WEBSITE+"/josmticket?...", 2);
217 }
218 return null;
219 }
220}
Note: See TracBrowser for help on using the repository browser.