1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.Toolkit;
|
---|
9 | import java.awt.datatransfer.Clipboard;
|
---|
10 | import java.awt.datatransfer.ClipboardOwner;
|
---|
11 | import java.awt.datatransfer.StringSelection;
|
---|
12 | import java.awt.datatransfer.Transferable;
|
---|
13 | import java.io.PrintWriter;
|
---|
14 | import java.io.StringWriter;
|
---|
15 | import java.net.URL;
|
---|
16 |
|
---|
17 | import javax.swing.JLabel;
|
---|
18 | import javax.swing.JOptionPane;
|
---|
19 | import javax.swing.JPanel;
|
---|
20 | import javax.swing.JScrollPane;
|
---|
21 | import javax.swing.JTextArea;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.actions.ShowStatusReportAction;
|
---|
25 | import org.openstreetmap.josm.gui.JMultilineLabel;
|
---|
26 | import 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 | */
|
---|
33 | public 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 | }
|
---|