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

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

fix #10198 - wrong layout of bug report dialog with Gnome 3 (at least it should fix t, cannot reproduce)

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