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

wurstscript / WurstScript / 240

14 Jan 2024 02:16PM CUT coverage: 62.353% (-0.04%) from 62.397%
240

Pull #1087

circleci

Frotty
fix lua map config and number issues
Pull Request #1087: Fix lua map config and number issues

17273 of 27702 relevant lines covered (62.35%)

0.62 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
import java.nio.charset.StandardCharsets;
15

16
public class ErrorReportingIO extends ErrorReporting {
×
17

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

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

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

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

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

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

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

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

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

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

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

86

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

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

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

116
        HttpURLConnection connection = null;
×
117
        try {
118

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

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

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

151

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

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

183
    private String showMultilineMessageDialog() {
184
        final JTextArea textArea = new JTextArea();
×
185
        textArea.setRows(8);
×
186
        textArea.setLineWrap(true);
×
187
        textArea.setWrapStyleWord(true);
×
188
        JScrollPane areaScrollPane = new JScrollPane(textArea);
×
189
        JComponent[] inputs = {
×
190
                new JLabel("Please add some contact information here in case we have further questions regarding this problem."),
191
                new JLabel("This can be your hive user-name or your mail address."),
192
                new JLabel("You can also add more information on how to reproduce the problem."),
193
                areaScrollPane
194
        };
195
        int r = JOptionPane.showConfirmDialog(null, inputs, "My custom dialog", JOptionPane.OK_CANCEL_OPTION);
×
196
        if (r == JOptionPane.OK_OPTION) {
×
197
            return textArea.getText();
×
198
        } else {
199
            return "(cancel" + r + " selected) ";
×
200
        }
201
    }
202
}
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