1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * general function, usually for data manipulation pages
9 * @var object stores the last exception info
11 _last_exception: null,
13 * handles thrown error exceptions based on user preferences
17 error_handler: function (exception) {
19 if (JSON.stringify(ErrorReport._last_exception) === JSON.stringify(exception)) {
22 if (exception.name === null || typeof(exception.name) === 'undefined') {
23 exception.name = ErrorReport._extractExceptionName(exception);
25 ErrorReport._last_exception = exception;
26 $.get('error_report.php', {
28 server: PMA_commonParams.get('server'),
32 if (data.success !== true) {
33 PMA_ajaxShowMessage(data.error, false);
36 if (data.report_setting === 'ask') {
37 ErrorReport._showErrorNotification();
38 } else if (data.report_setting === 'always') {
39 report_data = ErrorReport._get_report_data(exception);
40 post_data = $.extend(report_data, {
41 send_error_report: true,
44 $.post('error_report.php', post_data, function (data) {
45 if (data.success === false) {
46 // in the case of an error, show the error message returned.
47 PMA_ajaxShowMessage(data.error, false);
49 PMA_ajaxShowMessage(data.message, false);
56 * Shows the modal dialog previewing the report
58 * @param exception object error report info
62 _showReportDialog: function (exception) {
63 var report_data = ErrorReport._get_report_data(exception);
65 /* Remove the hidden dialogs if there are*/
66 if ($('#error_report_dialog').length !== 0) {
67 $('#error_report_dialog').remove();
69 var $div = $('<div id="error_report_dialog"></div>');
70 $div.css('z-index', '1000');
72 var button_options = {};
74 button_options[PMA_messages.strSendErrorReport] = function () {
75 var $dialog = $(this);
76 var post_data = $.extend(report_data, {
77 send_error_report: true,
78 description: $('#report_description').val(),
79 always_send: $('#always_send_checkbox')[0].checked
81 $.post('error_report.php', post_data, function (data) {
82 $dialog.dialog('close');
83 if (data.success === false) {
84 // in the case of an error, show the error message returned.
85 PMA_ajaxShowMessage(data.error, false);
87 PMA_ajaxShowMessage(data.message, 3000);
92 button_options[PMA_messages.strCancel] = function () {
93 $(this).dialog('close');
96 $.post('error_report.php', report_data, function (data) {
97 if (data.success === false) {
98 // in the case of an error, show the error message returned.
99 PMA_ajaxShowMessage(data.error, false);
101 // Show dialog if the request was successful
103 .append(data.message)
105 title: PMA_messages.strSubmitErrorReport,
108 buttons: button_options,
117 * Shows the small notification that asks for user permission
121 _showErrorNotification: function () {
122 ErrorReport._removeErrorNotification();
125 '<div style="position:fixed;bottom:0;left:0;right:0;margin:0;' +
126 'z-index:1000" class="error" id="error_notification"></div>'
128 PMA_getImage('s_error') + PMA_messages.strErrorOccurred
131 var $buttons = $('<div class="floatright"></div>');
133 var button_html = '<button id="show_error_report">';
134 button_html += PMA_messages.strShowReportDetails;
135 button_html += '</button>';
137 button_html += '<a id="change_error_settings">';
138 button_html += PMA_getImage('s_cog', PMA_messages.strChangeReportSettings);
139 button_html += '</a>';
141 button_html += '<a href="#" id="ignore_error">';
142 button_html += PMA_getImage('b_close', PMA_messages.strIgnore);
143 button_html += '</a>';
145 $buttons.html(button_html);
147 $div.append($buttons);
148 $div.appendTo(document.body);
149 $(document).on('click', '#change_error_settings', ErrorReport._redirect_to_settings);
150 $(document).on('click', '#show_error_report', ErrorReport._createReportDialog);
151 $(document).on('click', '#ignore_error', ErrorReport._removeErrorNotification);
154 * Removes the notification if it was displayed before
158 _removeErrorNotification: function (e) {
160 // don't remove the hash fragment by navigating to #
163 $('#error_notification').fadeOut(function () {
168 * Extracts Exception name from message if it exists
172 _extractExceptionName: function (exception) {
173 if (exception.message === null || typeof(exception.message) === 'undefined') {
177 var reg = /([a-zA-Z]+):/;
178 var regex_result = reg.exec(exception.message);
179 if (regex_result && regex_result.length === 2) {
180 return regex_result[1];
186 * Shows the modal dialog previewing the report
190 _createReportDialog: function () {
191 ErrorReport._removeErrorNotification();
192 ErrorReport._showReportDialog(ErrorReport._last_exception);
195 * Redirects to the settings page containing error report
200 _redirect_to_settings: function () {
201 window.location.href = 'prefs_forms.php';
204 * Returns the report data to send to the server
206 * @param exception object exception info
210 _get_report_data: function (exception) {
212 'ajax_request': true,
213 'exception': exception,
214 'current_url': window.location.href,
215 'exception_type': 'js'
217 if (AJAX.scriptHandler._scripts.length > 0) {
218 report_data.scripts = AJAX.scriptHandler._scripts.map(
227 * Wraps all global functions that start with PMA_
231 wrap_global_functions: function () {
232 for (var key in window) {
233 if (key.indexOf('PMA_') === 0) {
234 var global = window[key];
235 if (typeof(global) === 'function') {
236 window[key] = ErrorReport.wrap_function(global);
242 * Wraps given function in error reporting code and returns wrapped function
244 * @param func function to be wrapped
248 wrap_function: function (func) {
250 var new_func = function () {
252 return func.apply(this, arguments);
257 new_func.wrapped = true;
258 // Set guid of wrapped function same as original function, so it can be removed
259 // See bug#4146 (problem with jquery draggable and sortable)
260 new_func.guid = func.guid = func.guid || new_func.guid || jQuery.guid++;
267 * Automatically wraps the callback in AJAX.registerOnload
271 _wrap_ajax_onload_callback: function () {
272 var oldOnload = AJAX.registerOnload;
273 AJAX.registerOnload = function (file, func) {
274 func = ErrorReport.wrap_function(func);
275 oldOnload.call(this, file, func);
279 * Automatically wraps the callback in $.fn.on
283 _wrap_$_on_callback: function () {
285 $.fn.on = function () {
286 for (var i = 1; i <= 3; i++) {
287 if (typeof(arguments[i]) === 'function') {
288 arguments[i] = ErrorReport.wrap_function(arguments[i]);
292 return oldOn.apply(this, arguments);
296 * Wraps all global functions that start with PMA_
297 * also automatically wraps the callback in AJAX.registerOnload
301 set_up_error_reporting: function () {
302 ErrorReport.wrap_global_functions();
303 ErrorReport._wrap_ajax_onload_callback();
304 ErrorReport._wrap_$_on_callback();
309 AJAX.registerOnload('error_report.js', function () {
310 TraceKit.report.subscribe(ErrorReport.error_handler);
311 ErrorReport.set_up_error_reporting();
312 ErrorReport.wrap_global_functions();