From 333fb9da40507c43c06e8e4a7727046f9b739f9a Mon Sep 17 00:00:00 2001 From: Cody Jackson Date: Mon, 3 Feb 2020 16:17:53 -0700 Subject: [PATCH] Make unhandled exceptions display the fail whale Anytime we have an unhandled exception we will now navigate to the fail whale page. rancher/rancher#25198 --- app/fail-whale/route.js | 8 ++++++++ app/initializers/uncaught-exceptions.js | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 app/initializers/uncaught-exceptions.js diff --git a/app/fail-whale/route.js b/app/fail-whale/route.js index d8b8c3d96..e81c4fa21 100644 --- a/app/fail-whale/route.js +++ b/app/fail-whale/route.js @@ -1,5 +1,6 @@ import { inject as service } from '@ember/service'; import Route from '@ember/routing/route'; +import { run } from '@ember/runloop'; export default Route.extend({ storeReset: service(), @@ -12,6 +13,13 @@ export default Route.extend({ afterModel(model) { if ( model ) { this.get('storeReset').reset(); + + // This ensures the loading over/underlays are hidden even if a failure + // happens in the middle of loading. + run.scheduleOnce('afterRender', function() { // eslint-disable-line + $('#loading-underlay').hide(); // eslint-disable-line + $('#loading-overlay').hide(); // eslint-disable-line + }); } else { this.transitionTo('authenticated'); } diff --git a/app/initializers/uncaught-exceptions.js b/app/initializers/uncaught-exceptions.js new file mode 100644 index 000000000..d65c6234c --- /dev/null +++ b/app/initializers/uncaught-exceptions.js @@ -0,0 +1,20 @@ +export function initialize(application) { + const errorHandler = (message, file, line, column, error) => { + const router = application.__container__.lookup('router:main'); + const controller = application.__container__.lookup('controller:application'); + + controller.setProperties({ error }); + router.transitionTo('failWhale'); + } + + window.onerror = errorHandler; + + window.addEventListener('unhandledrejection', () => { + errorHandler(null, null, null, null, 'Promise was rejected.'); + }); +} + +export default { + name: 'uncaught-exceptions', + initialize +};