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

Last change on this file since 5874 was 5874, checked in by Don-vip, 11 years ago

see #8570, #7406 - I/O refactorization:

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