error_report.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. "use strict";
  2. /* global TraceKit */
  3. // js/vendor/tracekit.js
  4. /**
  5. * general function, usually for data manipulation pages
  6. *
  7. */
  8. var ErrorReport = {
  9. /**
  10. * @var object stores the last exception info
  11. */
  12. lastException: null,
  13. /**
  14. * handles thrown error exceptions based on user preferences
  15. *
  16. * @return void
  17. */
  18. errorHandler: function errorHandler(exception) {
  19. // issue: 14359
  20. if (JSON.stringify(ErrorReport.lastException) === JSON.stringify(exception)) {
  21. return;
  22. }
  23. if (exception.name === null || typeof exception.name === 'undefined') {
  24. exception.name = ErrorReport.extractExceptionName(exception);
  25. }
  26. ErrorReport.lastException = exception;
  27. $.post('index.php?route=/error-report', {
  28. 'ajax_request': true,
  29. 'server': CommonParams.get('server'),
  30. 'get_settings': true,
  31. 'exception_type': 'js'
  32. }, function (data) {
  33. if (data.success !== true) {
  34. Functions.ajaxShowMessage(data.error, false);
  35. return;
  36. }
  37. if (data.report_setting === 'ask') {
  38. ErrorReport.showErrorNotification();
  39. } else if (data.report_setting === 'always') {
  40. var reportData = ErrorReport.getReportData(exception);
  41. var postData = $.extend(reportData, {
  42. 'send_error_report': true,
  43. 'automatic': true
  44. });
  45. $.post('index.php?route=/error-report', postData, function (data) {
  46. if (data.success === false) {
  47. // in the case of an error, show the error message returned.
  48. Functions.ajaxShowMessage(data.error, false);
  49. } else {
  50. Functions.ajaxShowMessage(data.message, false);
  51. }
  52. });
  53. }
  54. });
  55. },
  56. /**
  57. * Shows the modal dialog previewing the report
  58. *
  59. * @param exception object error report info
  60. *
  61. * @return void
  62. */
  63. showReportDialog: function showReportDialog(exception) {
  64. var reportData = ErrorReport.getReportData(exception);
  65. /* Remove the hidden dialogs if there are*/
  66. if ($('#error_report_dialog').length !== 0) {
  67. $('#error_report_dialog').remove();
  68. }
  69. var $div = $('<div id="error_report_dialog"></div>');
  70. $div.css('z-index', '1000');
  71. var buttonOptions = {};
  72. buttonOptions[Messages.strSendErrorReport] = function () {
  73. var $dialog = $(this);
  74. var postData = $.extend(reportData, {
  75. 'send_error_report': true,
  76. 'description': $('#report_description').val(),
  77. 'always_send': $('#always_send_checkbox')[0].checked
  78. });
  79. $.post('index.php?route=/error-report', postData, function (data) {
  80. $dialog.dialog('close');
  81. if (data.success === false) {
  82. // in the case of an error, show the error message returned.
  83. Functions.ajaxShowMessage(data.error, false);
  84. } else {
  85. Functions.ajaxShowMessage(data.message, 3000);
  86. }
  87. });
  88. };
  89. buttonOptions[Messages.strCancel] = function () {
  90. $(this).dialog('close');
  91. };
  92. $.post('index.php?route=/error-report', reportData, function (data) {
  93. if (data.success === false) {
  94. // in the case of an error, show the error message returned.
  95. Functions.ajaxShowMessage(data.error, false);
  96. } else {
  97. // Show dialog if the request was successful
  98. $div.append(data.message).dialog({
  99. title: Messages.strSubmitErrorReport,
  100. width: 650,
  101. modal: true,
  102. buttons: buttonOptions,
  103. close: function close() {
  104. $(this).remove();
  105. }
  106. });
  107. }
  108. });
  109. },
  110. /**
  111. * Shows the small notification that asks for user permission
  112. *
  113. * @return void
  114. */
  115. showErrorNotification: function showErrorNotification() {
  116. ErrorReport.removeErrorNotification();
  117. var $div = $('<div class="alert alert-danger userPermissionModal" role="alert" id="error_notification"></div>').append(Functions.getImage('s_error') + Messages.strErrorOccurred);
  118. var $buttons = $('<div class="floatright"></div>');
  119. var buttonHtml = '<button class="btn btn-primary" id="show_error_report">';
  120. buttonHtml += Messages.strShowReportDetails;
  121. buttonHtml += '</button>';
  122. buttonHtml += '<a id="change_error_settings">';
  123. buttonHtml += Functions.getImage('s_cog', Messages.strChangeReportSettings);
  124. buttonHtml += '</a>';
  125. buttonHtml += '<a href="#" id="ignore_error">';
  126. buttonHtml += Functions.getImage('b_close', Messages.strIgnore);
  127. buttonHtml += '</a>';
  128. $buttons.html(buttonHtml);
  129. $div.append($buttons);
  130. $div.appendTo(document.body);
  131. $(document).on('click', '#change_error_settings', ErrorReport.redirectToSettings);
  132. $(document).on('click', '#show_error_report', ErrorReport.createReportDialog);
  133. $(document).on('click', '#ignore_error', ErrorReport.removeErrorNotification);
  134. },
  135. /**
  136. * Removes the notification if it was displayed before
  137. *
  138. * @return void
  139. */
  140. removeErrorNotification: function removeErrorNotification(e) {
  141. if (e) {
  142. // don't remove the hash fragment by navigating to #
  143. e.preventDefault();
  144. }
  145. $('#error_notification').fadeOut(function () {
  146. $(this).remove();
  147. });
  148. },
  149. /**
  150. * Extracts Exception name from message if it exists
  151. *
  152. * @return String
  153. */
  154. extractExceptionName: function extractExceptionName(exception) {
  155. if (exception.message === null || typeof exception.message === 'undefined') {
  156. return '';
  157. }
  158. var reg = /([a-zA-Z]+):/;
  159. var regexResult = reg.exec(exception.message);
  160. if (regexResult && regexResult.length === 2) {
  161. return regexResult[1];
  162. }
  163. return '';
  164. },
  165. /**
  166. * Shows the modal dialog previewing the report
  167. *
  168. * @return void
  169. */
  170. createReportDialog: function createReportDialog() {
  171. ErrorReport.removeErrorNotification();
  172. ErrorReport.showReportDialog(ErrorReport.lastException);
  173. },
  174. /**
  175. * Redirects to the settings page containing error report
  176. * preferences
  177. *
  178. * @return void
  179. */
  180. redirectToSettings: function redirectToSettings() {
  181. window.location.href = 'index.php?route=/preferences/features';
  182. },
  183. /**
  184. * Returns the report data to send to the server
  185. *
  186. * @param exception object exception info
  187. *
  188. * @return object
  189. */
  190. getReportData: function getReportData(exception) {
  191. if (exception && exception.stack && exception.stack.length) {
  192. for (var i = 0; i < exception.stack.length; i++) {
  193. var stack = exception.stack[i];
  194. if (stack.context && stack.context.length) {
  195. for (var j = 0; j < stack.context.length; j++) {
  196. if (stack.context[j].length > 80) {
  197. stack.context[j] = stack.context[j].substring(-1, 75) + '//...';
  198. }
  199. }
  200. }
  201. }
  202. }
  203. var reportData = {
  204. 'server': CommonParams.get('server'),
  205. 'ajax_request': true,
  206. 'exception': exception,
  207. 'url': window.location.href,
  208. 'exception_type': 'js'
  209. };
  210. if (AJAX.scriptHandler.scripts.length > 0) {
  211. reportData.scripts = AJAX.scriptHandler.scripts.map(function (script) {
  212. return script;
  213. });
  214. }
  215. return reportData;
  216. },
  217. /**
  218. * Wraps all global functions that start with PMA_
  219. *
  220. * @return void
  221. */
  222. wrapGlobalFunctions: function wrapGlobalFunctions() {
  223. for (var key in window) {
  224. if (key.indexOf('PMA_') === 0) {
  225. var global = window[key];
  226. if (typeof global === 'function') {
  227. window[key] = ErrorReport.wrapFunction(global);
  228. }
  229. }
  230. }
  231. },
  232. /**
  233. * Wraps given function in error reporting code and returns wrapped function
  234. *
  235. * @param func function to be wrapped
  236. *
  237. * @return function
  238. */
  239. wrapFunction: function wrapFunction(func) {
  240. if (!func.wrapped) {
  241. var newFunc = function newFunc() {
  242. try {
  243. return func.apply(this, arguments);
  244. } catch (x) {
  245. TraceKit.report(x);
  246. }
  247. };
  248. newFunc.wrapped = true; // Set guid of wrapped function same as original function, so it can be removed
  249. // See bug#4146 (problem with jquery draggable and sortable)
  250. newFunc.guid = func.guid = func.guid || newFunc.guid || jQuery.guid++;
  251. return newFunc;
  252. } else {
  253. return func;
  254. }
  255. },
  256. /**
  257. * Automatically wraps the callback in AJAX.registerOnload
  258. *
  259. * @return void
  260. */
  261. wrapAjaxOnloadCallback: function wrapAjaxOnloadCallback() {
  262. var oldOnload = AJAX.registerOnload;
  263. AJAX.registerOnload = function (file, func) {
  264. var wrappedFunction = ErrorReport.wrapFunction(func);
  265. oldOnload.call(this, file, wrappedFunction);
  266. };
  267. },
  268. /**
  269. * Automatically wraps the callback in $.fn.on
  270. *
  271. * @return void
  272. */
  273. wrapJqueryOnCallback: function wrapJqueryOnCallback() {
  274. var oldOn = $.fn.on;
  275. $.fn.on = function () {
  276. for (var i = 1; i <= 3; i++) {
  277. if (typeof arguments[i] === 'function') {
  278. arguments[i] = ErrorReport.wrapFunction(arguments[i]);
  279. break;
  280. }
  281. }
  282. return oldOn.apply(this, arguments);
  283. };
  284. },
  285. /**
  286. * Wraps all global functions that start with PMA_
  287. * also automatically wraps the callback in AJAX.registerOnload
  288. *
  289. * @return void
  290. */
  291. setUpErrorReporting: function setUpErrorReporting() {
  292. ErrorReport.wrapGlobalFunctions();
  293. ErrorReport.wrapAjaxOnloadCallback();
  294. ErrorReport.wrapJqueryOnCallback();
  295. }
  296. };
  297. AJAX.registerOnload('error_report.js', function () {
  298. TraceKit.report.subscribe(ErrorReport.errorHandler);
  299. ErrorReport.setUpErrorReporting();
  300. ErrorReport.wrapGlobalFunctions();
  301. });