| 281 | } |
| 282 | |
| 283 | var watchForErrors = function () { |
| 284 | let currentErrors = 0 |
| 285 | const oldOnError = window.onerror |
| 286 | |
| 287 | const showError = function (text) { |
| 288 | if (currentErrors >= 3) { return } |
| 289 | if (app.isProduction() && !me.isAdmin()) { return } // Don't show noty error messages in production when not an admin |
| 290 | if (!me.isAdmin() && (document.location.href.search(/codecombat.com/) !== -1) && (document.location.href.search(/\/editor\//) === -1)) { return } |
| 291 | ++currentErrors |
| 292 | if (!(typeof webkit !== 'undefined' && webkit !== null ? webkit.messageHandlers : undefined)) { // Don't show these notys on iPad |
| 293 | return noty({ |
| 294 | text, |
| 295 | layout: 'topCenter', |
| 296 | type: 'error', |
| 297 | killer: false, |
| 298 | timeout: 5000, |
| 299 | dismissQueue: true, |
| 300 | maxVisible: 3, |
| 301 | callback: { onClose () { return --currentErrors } } |
| 302 | }) |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | window.onerror = function (msg, url, line, col, error) { |
| 307 | if (oldOnError) { oldOnError.apply(window, arguments) } |
| 308 | const message = `Error: ${msg}<br>Check the JS console for more.` |
| 309 | showError(message) |
| 310 | return Backbone.Mediator.publish('application:error', { message: `Line ${line} of ${url}:\n${msg}` }) // For iOS app |
| 311 | } |
| 312 | |
| 313 | // Promise error handling |
| 314 | return window.addEventListener('unhandledrejection', function (err) { |
| 315 | if (err.promise) { |
| 316 | return err.promise.catch(function (e) { |
| 317 | const message = `${e.message}<br>Check the JS console for more.` |
| 318 | return showError(message) |
| 319 | }) |
| 320 | } else { |
| 321 | const message = `${err.message || err}<br>Check the JS console for more.` |
| 322 | return showError(message) |
| 323 | } |
| 324 | }) |
| 325 | } |
| 326 | |
| 327 | window.addIPadSubscription = channel => window.iPadSubscriptions[channel] = true |
| 328 | |