Welcome to mirror list, hosted at ThFree Co, Russian Federation.

error_report.js « src « js - github.com/phpmyadmin/phpmyadmin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d91c8f108ebbc80821e12d93d4d47e44f13b974c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
 * general function, usually for data manipulation pages
 *
 */
var ErrorReport = {
    /**
     * @var {object}, stores the last exception info
     */
    lastException: null,
    /**
     * @var object stores the Error Report Data to prevent unnecessary data fetching
     */
    errorReportData: null,
    /**
     * @var object maintains unique keys already used
     */
    keyDict: {},
    /**
     * handles thrown error exceptions based on user preferences
     *
     * @param {object} data
     * @param {any} exception
     * @return {void}
     */
    errorDataHandler: function (data, exception) {
        if (data.success !== true) {
            Functions.ajaxShowMessage(data.error, false);
            return;
        }
        if (data.report_setting === 'ask') {
            ErrorReport.showErrorNotification();
        } else if (data.report_setting === 'always') {
            var reportData = ErrorReport.getReportData(exception);
            var postData = $.extend(reportData, {
                'send_error_report': true,
                'automatic': true
            });
            $.post('index.php?route=/error-report', postData, function (data) {
                if (data.success === false) {
                    // in the case of an error, show the error message returned.
                    Functions.ajaxShowMessage(data.error, false);
                } else {
                    Functions.ajaxShowMessage(data.message, false);
                }
            });
        }
    },
    errorHandler: function (exception) {
        // issue: 14359
        if (JSON.stringify(ErrorReport.lastException) === JSON.stringify(exception)) {
            return;
        }
        if (exception.name === null || typeof(exception.name) === 'undefined') {
            exception.name = ErrorReport.extractExceptionName(exception);
        }
        ErrorReport.lastException = exception;
        if (ErrorReport.errorReportData === null) {
            $.post('index.php?route=/error-report', {
                'ajax_request': true,
                'server': window.CommonParams.get('server'),
                'get_settings': true,
                'exception_type': 'js'
            }, function (data) {
                ErrorReport.errorReportData = data;
                ErrorReport.errorDataHandler(data, exception);
            });
        } else {
            ErrorReport.errorDataHandler(ErrorReport.errorReportData, exception);
        }
    },
    /**
     * Shows the modal dialog previewing the report
     *
     * @param exception object error report info
     *
     * @return {void}
     */
    showReportDialog: function (exception) {
        const reportData = ErrorReport.getReportData(exception);

        const sendErrorReport = function () {
            const postData = $.extend(reportData, {
                'send_error_report': true,
                'description': $('#errorReportDescription').val(),
                'always_send': $('#errorReportAlwaysSendCheckbox')[0].checked
            });
            $.post('index.php?route=/error-report', postData, function (data) {
                if (data.success === false) {
                    Functions.ajaxShowMessage(data.error, false);
                } else {
                    Functions.ajaxShowMessage(data.message, 3000);
                }
            });
            $('#errorReportModal').modal('hide');
        };

        $.post('index.php?route=/error-report', reportData).done(function (data) {
            // Delete the modal to refresh it in case the user changed SendErrorReports value
            if (document.getElementById('errorReportModal') !== null) {
                $('#errorReportModal').remove();
            }

            $('body').append($(data.report_modal));
            const $errorReportModal = $('#errorReportModal');
            $errorReportModal.on('show.bs.modal', function () {
                // Prevents multiple onClick events
                $('#errorReportModalConfirm').off('click', sendErrorReport);
                $('#errorReportModalConfirm').on('click', sendErrorReport);
                $('#errorReportModal .modal-body').html(data.message);
            });
            $errorReportModal.modal('show');
        });
    },
    /**
     * Shows the small notification that asks for user permission
     *
     * @return {void}
     */
    showErrorNotification: function () {
        var key = Math.random().toString(36).substring(2, 12);
        while (key in ErrorReport.keyDict) {
            key = Math.random().toString(36).substring(2, 12);
        }
        ErrorReport.keyDict[key] = 1;

        var $div = $(
            '<div class="alert alert-danger" role="alert" id="error_notification_' + key + '"></div>'
        ).append(
            Functions.getImage('s_error') + Messages.strErrorOccurred
        );

        var $buttons = $('<div class="float-end"></div>');
        var buttonHtml  = '<button class="btn btn-primary" id="show_error_report_' + key + '">';
        buttonHtml += Messages.strShowReportDetails;
        buttonHtml += '</button>';

        var settingsUrl = 'index.php?route=/preferences/features&server=' + window.CommonParams.get('server');
        buttonHtml += '<a class="ajax" href="' + settingsUrl + '">';
        buttonHtml += Functions.getImage('s_cog', Messages.strChangeReportSettings);
        buttonHtml += '</a>';

        buttonHtml += '<a href="#" id="ignore_error_' + key + '" data-notification-id="' + key + '">';
        buttonHtml += Functions.getImage('b_close', Messages.strIgnore);
        buttonHtml += '</a>';

        $buttons.html(buttonHtml);

        $div.append($buttons);
        // eslint-disable-next-line compat/compat
        $div.appendTo(document.body);
        $(document).on('click', '#show_error_report_' + key, ErrorReport.createReportDialog);
        $(document).on('click', '#ignore_error_' + key, ErrorReport.removeErrorNotification);
    },
    /**
     * Removes the notification if it was displayed before
     *
     * @param {Event} e
     * @return {void}
     */
    removeErrorNotification: function (e) {
        if (e) {
            // don't remove the hash fragment by navigating to #
            e.preventDefault();
        }
        $('#error_notification_' + $(this).data('notification-id')).fadeOut(function () {
            $(this).remove();
        });
    },
    /**
     * Extracts Exception name from message if it exists
     *
     * @param exception
     * @return {string}
     */
    extractExceptionName: function (exception) {
        if (exception.message === null || typeof(exception.message) === 'undefined') {
            return '';
        }

        var reg = /([a-zA-Z]+):/;
        var regexResult = reg.exec(exception.message);
        if (regexResult && regexResult.length === 2) {
            return regexResult[1];
        }

        return '';
    },
    /**
     * Shows the modal dialog previewing the report
     *
     * @return {void}
     */
    createReportDialog: function () {
        ErrorReport.removeErrorNotification();
        ErrorReport.showReportDialog(ErrorReport.lastException);
    },
    /**
     * Returns the report data to send to the server
     *
     * @param exception object exception info
     *
     * @return {object}
     */
    getReportData: function (exception) {
        if (exception && exception.stack && exception.stack.length) {
            for (var i = 0; i < exception.stack.length; i++) {
                var stack = exception.stack[i];
                if (stack.context && stack.context.length) {
                    for (var j = 0; j < stack.context.length; j++) {
                        if (stack.context[j].length >  80) {
                            stack.context[j] = stack.context[j].substring(-1, 75) + '//...';
                        }
                    }
                }
            }
        }
        var reportData = {
            'server': window.CommonParams.get('server'),
            'ajax_request': true,
            'exception': exception,
            'url': window.location.href,
            'exception_type': 'js'
        };
        if (window.AJAX.scriptHandler.scripts.length > 0) {
            reportData.scripts = window.AJAX.scriptHandler.scripts.map(
                function (script) {
                    return script;
                }
            );
        }
        return reportData;
    },
    /**
     * Wraps given function in error reporting code and returns wrapped function
     *
     * @param {Function} func function to be wrapped
     *
     * @return {Function}
     */
    wrapFunction: function (func) {
        if (!func.wrapped) {
            var newFunc = function () {
                try {
                    return func.apply(this, arguments);
                } catch (x) {
                    window.TraceKit.report(x);
                }
            };
            newFunc.wrapped = true;
            // Set guid of wrapped function same as original function, so it can be removed
            // See bug#4146 (problem with jquery draggable and sortable)
            newFunc.guid = func.guid = func.guid || newFunc.guid || jQuery.guid++;
            return newFunc;
        } else {
            return func;
        }
    },
    /**
     * Automatically wraps the callback in window.AJAX.registerOnload
     *
     * @return {void}
     */
    wrapAjaxOnloadCallback: function () {
        var oldOnload = window.AJAX.registerOnload;
        window.AJAX.registerOnload = function (file, func) {
            var wrappedFunction = ErrorReport.wrapFunction(func);
            oldOnload.call(this, file, wrappedFunction);
        };
    },
    /**
     * Automatically wraps the callback in $.fn.on
     *
     * @return {void}
     */
    wrapJqueryOnCallback: function () {
        var oldOn = $.fn.on;
        $.fn.on = function () {
            for (var i = 1; i <= 3; i++) {
                if (typeof(arguments[i]) === 'function') {
                    arguments[i] = ErrorReport.wrapFunction(arguments[i]);
                    break;
                }
            }
            return oldOn.apply(this, arguments);
        };
    },
    /**
     * Wraps the callback in window.AJAX.registerOnload automatically
     *
     * @return {void}
     */
    setUpErrorReporting: function () {
        ErrorReport.wrapAjaxOnloadCallback();
        ErrorReport.wrapJqueryOnCallback();
    }
};

window.AJAX.registerOnload('error_report.js', function () {
    window.TraceKit.report.subscribe(ErrorReport.errorHandler);
    ErrorReport.setUpErrorReporting();
});