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) {
18 if (exception.name === null || typeof(exception.name) === 'undefined') {
19 exception.name = ErrorReport._extractExceptionName(exception);
21 ErrorReport._last_exception = exception;
22 $.get('error_report.php', {
24 server: PMA_commonParams.get('server'),
28 if (data.success !== true) {
29 PMA_ajaxShowMessage(data.error, false);
32 if (data.report_setting === 'ask') {
33 ErrorReport._showErrorNotification();
34 } else if (data.report_setting === 'always') {
35 report_data = ErrorReport._get_report_data(exception);
36 post_data = $.extend(report_data, {
37 send_error_report: true,
40 $.post('error_report.php', post_data, function (data) {
41 if (data.success === false) {
42 // in the case of an error, show the error message returned.
43 PMA_ajaxShowMessage(data.error, false);
45 PMA_ajaxShowMessage(data.message, false);
52 * Shows the modal dialog previewing the report
54 * @param exception object error report info
58 _showReportDialog: function (exception) {
59 var report_data = ErrorReport._get_report_data(exception);
61 /* Remove the hidden dialogs if there are*/
62 if ($('#error_report_dialog').length !== 0) {
63 $('#error_report_dialog').remove();
65 var $div = $('<div id="error_report_dialog"></div>');
66 $div.css('z-index', '1000');
68 var button_options = {};
70 button_options[PMA_messages.strSendErrorReport] = function () {
71 var $dialog = $(this);
72 var post_data = $.extend(report_data, {
73 send_error_report: true,
74 description: $('#report_description').val(),
75 always_send: $('#always_send_checkbox')[0].checked
77 $.post('error_report.php', post_data, function (data) {
78 $dialog.dialog('close');
79 if (data.success === false) {
80 // in the case of an error, show the error message returned.
81 PMA_ajaxShowMessage(data.error, false);
83 PMA_ajaxShowMessage(data.message, 3000);
88 button_options[PMA_messages.strCancel] = function () {
89 $(this).dialog('close');
92 $.post('error_report.php', report_data, function (data) {
93 if (data.success === false) {
94 // in the case of an error, show the error message returned.
95 PMA_ajaxShowMessage(data.error, false);
97 // Show dialog if the request was successful
101 title: PMA_messages.strSubmitErrorReport,
104 buttons: button_options,
113 * Shows the small notification that asks for user permission
117 _showErrorNotification: function () {
118 ErrorReport._removeErrorNotification();
121 '<div style="position:fixed;bottom:0;left:0;right:0;margin:0;' +
122 'z-index:1000" class="error" id="error_notification"></div>'
124 PMA_getImage('s_error') + PMA_messages.strErrorOccurred
127 var $buttons = $('<div class="floatright"></div>');
129 var button_html = '<button id="show_error_report">';
130 button_html += PMA_messages.strShowReportDetails;
131 button_html += '</button>';
133 button_html += '<a id="change_error_settings">';
134 button_html += PMA_getImage('s_cog', PMA_messages.strChangeReportSettings);
135 button_html += '</a>';
137 button_html += '<a href="#" id="ignore_error">';
138 button_html += PMA_getImage('b_close', PMA_messages.strIgnore);
139 button_html += '</a>';
141 $buttons.html(button_html);
143 $div.append($buttons);
144 $div.appendTo(document.body);
145 $('#change_error_settings').on('click', ErrorReport._redirect_to_settings);
146 $('#show_error_report').on('click', ErrorReport._createReportDialog);
147 $('#ignore_error').on('click', ErrorReport._removeErrorNotification);
150 * Removes the notification if it was displayed before
154 _removeErrorNotification: function (e) {
156 // don't remove the hash fragment by navigating to #
159 $('#error_notification').fadeOut(function () {
164 * Extracts Exception name from message if it exists
168 _extractExceptionName: function (exception) {
169 if (exception.message === null || typeof(exception.message) === 'undefined') {
173 var reg = /([a-zA-Z]+):/;
174 var regex_result = reg.exec(exception.message);
175 if (regex_result && regex_result.length === 2) {
176 return regex_result[1];
182 * Shows the modal dialog previewing the report
186 _createReportDialog: function () {
187 ErrorReport._removeErrorNotification();
188 ErrorReport._showReportDialog(ErrorReport._last_exception);
191 * Redirects to the settings page containing error report
196 _redirect_to_settings: function () {
197 window.location.href = 'prefs_forms.php';
200 * Returns the report data to send to the server
202 * @param exception object exception info
206 _get_report_data: function (exception) {
208 'ajax_request': true,
209 'exception': exception,
210 'current_url': window.location.href,
211 'exception_type': 'js'
213 if (AJAX.scriptHandler._scripts.length > 0) {
214 report_data.scripts = AJAX.scriptHandler._scripts.map(
223 * Wraps all global functions that start with PMA_
227 wrap_global_functions: function () {
228 for (var key in window) {
229 if (key.indexOf('PMA_') === 0) {
230 var global = window[key];
231 if (typeof(global) === 'function') {
232 window[key] = ErrorReport.wrap_function(global);
238 * Wraps given function in error reporting code and returns wrapped function
240 * @param func function to be wrapped
244 wrap_function: function (func) {
246 var new_func = function () {
248 return func.apply(this, arguments);
253 new_func.wrapped = true;
254 // Set guid of wrapped function same as original function, so it can be removed
255 // See bug#4146 (problem with jquery draggable and sortable)
256 new_func.guid = func.guid = func.guid || new_func.guid || jQuery.guid++;
263 * Automatically wraps the callback in AJAX.registerOnload
267 _wrap_ajax_onload_callback: function () {
268 var oldOnload = AJAX.registerOnload;
269 AJAX.registerOnload = function (file, func) {
270 func = ErrorReport.wrap_function(func);
271 oldOnload.call(this, file, func);
275 * Automatically wraps the callback in $.fn.on
279 _wrap_$_on_callback: function () {
281 $.fn.on = function () {
282 for (var i = 1; i <= 3; i++) {
283 if (typeof(arguments[i]) === 'function') {
284 arguments[i] = ErrorReport.wrap_function(arguments[i]);
288 return oldOn.apply(this, arguments);
292 * Wraps all global functions that start with PMA_
293 * also automatically wraps the callback in AJAX.registerOnload
297 set_up_error_reporting: function () {
298 ErrorReport.wrap_global_functions();
299 ErrorReport._wrap_ajax_onload_callback();
300 ErrorReport._wrap_$_on_callback();
305 AJAX.registerOnload('error_report.js', function () {
306 TraceKit.report.subscribe(ErrorReport.error_handler);
307 ErrorReport.set_up_error_reporting();
308 ErrorReport.wrap_global_functions();