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

Last change on this file since 2751 was 2721, checked in by stoecker, 14 years ago

fixed #4123 - fix too long bug report URL

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