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

Last change on this file since 4077 was 3840, checked in by stoecker, 13 years ago

switch to internal base64 again

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