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

Last change on this file since 2876 was 2859, checked in by Gubaer, 14 years ago

minor clean up

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