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

Revision 5050, 8.4 KB checked in by akks, 3 months ago (diff)

UrlLabel class simplification by Zverik, better label layout in VersionInfoPanel (see #7450, #7326)

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