errors_handler_controller.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. App.ErrorsHandlerController = Em.Controller.extend(App.UserPref, {
  20. name: 'errorsHandlerController',
  21. /**
  22. * @const
  23. */
  24. ERROR_STORAGE_SIZE: 500000,
  25. /**
  26. * @const
  27. */
  28. MAX_TRACE_LENGTH: 1000,
  29. init: function () {
  30. var oldError = window.onerror || Em.K;
  31. var self = this;
  32. window.onerror = function (err, url, lineNumber, colNumber, Err) {
  33. oldError.call(this, err, url, lineNumber, colNumber, Err);
  34. self.saveErrorLogs(err, url, lineNumber, colNumber, Err);
  35. };
  36. return this._super();
  37. },
  38. /**
  39. * load logs from server
  40. */
  41. loadErrorLogs: function() {
  42. this.getUserPref('errors');
  43. },
  44. /**
  45. * @method getUserPrefSuccessCallback
  46. * @param {object|null} data
  47. */
  48. getUserPrefSuccessCallback: function(data) {
  49. if (data) {
  50. localStorage.setObject('errors', data);
  51. }
  52. },
  53. /**
  54. * save error logs to localStorage and server
  55. * @param {string} err
  56. * @param {string} url
  57. * @param {number} lineNumber
  58. * @param {number} colNumber
  59. * @param {Error} Err
  60. */
  61. saveErrorLogs: function(err, url, lineNumber, colNumber, Err) {
  62. var ls = localStorage.getObject('errors') || {};
  63. var key = new Date().getTime();
  64. var stackTrace = Em.get(Err || {}, 'stack');
  65. if (stackTrace) {
  66. stackTrace = stackTrace.replace(/http:\/\/.*:8080\/javascripts/g, "").substr(0, this.MAX_TRACE_LENGTH);
  67. }
  68. var val = {
  69. file: url,
  70. line: lineNumber,
  71. col: colNumber,
  72. error: err,
  73. stackTrace: stackTrace
  74. };
  75. //overwrite errors if storage full
  76. if (JSON.stringify(ls).length > this.ERROR_STORAGE_SIZE) {
  77. delete ls[Object.keys(ls).sort()[0]];
  78. }
  79. ls[key] = val;
  80. localStorage.setObject('errors', ls);
  81. this.postUserPref('errors', ls);
  82. }
  83. });