• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wurstscript / WurstScript / 221

22 Nov 2023 03:20PM CUT coverage: 62.578% (-0.02%) from 62.602%
221

Pull #1081

circleci

Frotty
update wc3libs
Pull Request #1081: More performance improvements

17311 of 27663 relevant lines covered (62.58%)

0.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/ErrorReportingIO.java
1
package de.peeeq.wurstio;
2

3
import de.peeeq.wurstio.gui.AboutDialog;
4
import de.peeeq.wurstio.gui.GuiUtils;
5
import de.peeeq.wurstio.utils.FileUtils;
6
import de.peeeq.wurstscript.ErrorReporting;
7
import de.peeeq.wurstscript.WLogger;
8
import de.peeeq.wurstscript.utils.Utils;
9

10
import javax.swing.*;
11
import java.awt.*;
12
import java.io.*;
13
import java.net.*;
14

15
public class ErrorReportingIO extends ErrorReporting {
×
16

17
    @Override
18
    public void handleSevere(final Throwable t, final String sourcecode) {
19
        WLogger.severe(t);
×
20

21
        try {
22
            UIManager.setLookAndFeel(
×
23
                    UIManager.getSystemLookAndFeelClassName());
×
24
        } catch (Exception e) {
×
25
            // ignore
26
        }
×
27

28
        String title = "Sor!";
×
29
        String message = "You have encountered a bug in the Wurst Compiler.\n" +
×
30
                "Your version is: " + AboutDialog.version + "\n" +
31
                "The Error message is: " + t.getMessage() + "\n" + Utils.printExceptionWithStackTrace(t) + "\n\n" +
×
32
                "What do you want to do in order to help us fix this bug?";
33

34
        Object[] options = {
×
35
                "Nothing",
36
                "Send automatic error report",
37
                "Create manual bug report"
38
        };
39
        JFrame parent = new JFrame();
×
40
        parent.pack();
×
41
        parent.setVisible(true);
×
42
        GuiUtils.setWindowToCenterOfScreen(parent);
×
43
        int n = JOptionPane.showOptionDialog(parent,
×
44
                message,
45
                title,
46
                JOptionPane.YES_NO_OPTION,
47
                JOptionPane.QUESTION_MESSAGE,
48
                null,     //do not use a custom Icon
49
                options,  //the titles of buttons
50
                options[1]); //default button titles
51

52
        if (n == 1) {
×
53
            final boolean results[] = new boolean[3];
×
54
            Thread threads[] = new Thread[4];
×
55

56
            threads[0] = new Thread(() -> results[0] = sendErrorReport(t, ""));
×
57

58
            threads[1] = new Thread(() -> results[1] = sendErrorReport(t, "\n\nLog: \n\n" + WLogger.getLog()));
×
59

60
            threads[2] = new Thread(() -> {
×
61
                try {
62
                    Thread.sleep(500);
×
63
                } catch (InterruptedException e) {
×
64
                }
×
65
                results[1] = sendErrorReport(t, "\n\nSource Code: \n\n" + sourcecode);
×
66
            });
×
67

68
            threads[3] = new Thread(() -> {
×
69
                String customMessage = showMultilineMessageDialog();
×
70
                results[2] = sendErrorReport(t, "Custom message:\n\n" + customMessage);
×
71
            });
×
72

73
            for (Thread tr : threads) {
×
74
                tr.start();
×
75
            }
76

77
            for (Thread tr : threads) {
×
78
                try {
79
                    tr.join();
×
80
                } catch (InterruptedException e) {
×
81
                    e.printStackTrace();
×
82
                }
×
83
            }
84

85

86
            try {
87
                FileUtils.write(sourcecode, new File("errorreport_source.wurst"));
×
88
            } catch (IOException e) {
×
89
                WLogger.severe(e);
×
90
            }
×
91

92
            if (results[0] && results[1] && results[2]) {
×
93
                JOptionPane.showMessageDialog(parent, "Thank you!");
×
94
            } else if (results[0]) {
×
95
                JOptionPane.showMessageDialog(parent, "Error Report could only be sent partially.");
×
96
            } else {
97
                JOptionPane.showMessageDialog(parent, "Error report could not be sent.");
×
98
            }
99
        } else if (n == 2) {
×
100
            Desktop desk = Desktop.getDesktop();
×
101
            try {
102
                desk.browse(new URI("https://github.com/peq/WurstScript/issues"));
×
103
            } catch (Exception e) {
×
104
                WLogger.severe(e);
×
105
                JOptionPane.showMessageDialog(parent, "Could not open browser.");
×
106
            }
×
107
        }
108
        parent.setVisible(false);
×
109
        parent.dispose();
×
110
    }
×
111

112
    @Override
113
    public boolean sendErrorReport(Throwable t, String sourcecode) {
114

115
        HttpURLConnection connection = null;
×
116
        try {
117

118
            // Construct data
119
            String data = URLEncoder.encode("errormessage", "UTF-8") + "=" + URLEncoder.encode(t.getMessage(), "UTF-8");
×
120
            data += "&" + URLEncoder.encode("stacktrace", "UTF-8") + "=" + URLEncoder.encode(Utils.printExceptionWithStackTrace(t), "UTF-8");
×
121
            data += "&" + URLEncoder.encode("version", "UTF-8") + "=" + URLEncoder.encode(AboutDialog.version, "UTF-8");
×
122
            data += "&" + URLEncoder.encode("source", "UTF-8") + "=" + URLEncoder.encode(sourcecode, "UTF-8");
×
123

124
            String request = "http://peeeq.de/wursterrors.php";
×
125
            URL url = new URL(request);
×
126
            connection = (HttpURLConnection) url.openConnection();
×
127
            connection.setDoOutput(true);
×
128
            connection.setDoInput(true);
×
129
            connection.setInstanceFollowRedirects(false);
×
130
            connection.setRequestMethod("POST");
×
131
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
×
132
            connection.setRequestProperty("charset", "utf-8");
×
133
            connection.setRequestProperty("Content-Length", "" + Integer.toString(data.getBytes().length));
×
134
            connection.setUseCaches(false);
×
135
            DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
×
136
            wr.writeBytes(data);
×
137
            wr.flush();
×
138
            wr.close();
×
139
//                        String output = CharStreams.toString(new InputStreamReader(connection.getInputStream()));
140

141
            // Get response data.
142
            InputStream input = connection.getInputStream();
×
143
            int ch;
144
            StringBuilder output = new StringBuilder();
×
145
            while (((ch = input.read())) >= 0) {
×
146
                output.append((char) ch);
×
147
            }
148
            input.close();
×
149

150

151
            if (!output.toString().startsWith("Success")) {
×
152
                handleError("Could not send error report:\n" + output);
×
153
                return false;
×
154
            } else {
155
                return true;
×
156
            }
157
        } catch (MalformedURLException e) {
×
158
            WLogger.severe(e);
×
159
            handleError("Malformed URL\n" + e.getMessage());
×
160
        } catch (ProtocolException e) {
×
161
            WLogger.severe(e);
×
162
            handleError("ProtocolException\n" + e.getMessage());
×
163
        } catch (FileNotFoundException e) {
×
164
            WLogger.severe(e);
×
165
            handleError("Error reporting URL not found...\n" + e.getMessage());
×
166
        } catch (IOException e) {
×
167
            WLogger.severe(e);
×
168
            handleError("IOException\n" + e.getMessage());
×
169
        } finally {
170
            if (connection != null) {
×
171
                connection.disconnect();
×
172
            }
173
        }
174
        return false;
×
175
    }
176

177
    private static void handleError(String msg) {
178
        WLogger.severe(msg);
×
179
        System.err.println(msg);
×
180
    }
×
181

182
    private String showMultilineMessageDialog() {
183
        final JTextArea textArea = new JTextArea();
×
184
        textArea.setRows(8);
×
185
        textArea.setLineWrap(true);
×
186
        textArea.setWrapStyleWord(true);
×
187
        JScrollPane areaScrollPane = new JScrollPane(textArea);
×
188
        JComponent inputs[] = {
×
189
                new JLabel("Please add some contact information here in case we have further questions regarding this problem."),
190
                new JLabel("This can be your hive user-name or your mail address."),
191
                new JLabel("You can also add more information on how to reproduce the problem."),
192
                areaScrollPane
193
        };
194
        int r = JOptionPane.showConfirmDialog(null, inputs, "My custom dialog", JOptionPane.OK_CANCEL_OPTION);
×
195
        if (r == JOptionPane.OK_OPTION) {
×
196
            return textArea.getText();
×
197
        } else {
198
            return "(cancel" + r + " selected) ";
×
199
        }
200
    }
201
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc