From 2d6dc75a2cb5fa81189920d0f7fdc7b7f12132ab Mon Sep 17 00:00:00 2001 From: fja-odoo Date: Thu, 25 Jul 2019 08:20:59 +0000 Subject: [FIX] web, *: make crash_manager use services correctly * = website_forum, stock Fix for: https://github.com/odoo/odoo/pull/32132 crash_manager.js was using the services while bypassing ServiceProviderMixin That means all services are duplicated, each having their own state. To fix this issue, CrashManager now extends AbstractService. Also crash_manager is now registered in serviceRegistry instead of being instanciated on the fly. The crash_manager now being a Service raise one issue with the tests: The services are not run for the tests but some tests needs the crash_manager 'unhandledrejection' listener. For that matter the services are reenable in qunit. Also the crash_manager's 'unhandledrejection' listener has to have precedence over the qunit listener as it will handle rejections wich have no reason or are not Errors. Part of: https://github.com/odoo/odoo/pull/35209 --- addons/web/static/src/js/services/crash_manager.js | 47 ++++++++++++---------- 1 file changed, 26 insertions(+), 21 deletions(-) (limited to 'addons/web/static/src/js/services') diff --git a/addons/web/static/src/js/services/crash_manager.js b/addons/web/static/src/js/services/crash_manager.js index 701d97f0a53..4e4d4c9b0ff 100644 --- a/addons/web/static/src/js/services/crash_manager.js +++ b/addons/web/static/src/js/services/crash_manager.js @@ -1,12 +1,10 @@ odoo.define('web.CrashManager', function (require) { "use strict"; +const AbstractService = require('web.AbstractService'); var ajax = require('web.ajax'); var core = require('web.core'); var Dialog = require('web.Dialog'); -var mixins = require('web.mixins'); -var ServicesMixin = require('web.ServicesMixin'); -var ServiceProviderMixin = require('web.ServiceProviderMixin'); var Widget = require('web.Widget'); var _t = core._t; @@ -23,6 +21,14 @@ var map_title ={ karma_error: _lt("Karma Error"), }; +// Register this eventlistener before qunit does. +// Some errors needs to be negated by the crash_manager. +window.addEventListener('unhandledrejection', ev => + core.bus.trigger('crash_manager_unhandledrejection', ev) +); + +let active = true; + /** * An extension of Dialog Widget to render the warnings and errors on the website. * Extend it with your template of choice like ErrorDialog/WarningDialog @@ -78,13 +84,13 @@ var WarningDialog = CrashManagerDialog.extend({ }, }); -var CrashManager = core.Class.extend(mixins.EventDispatcherMixin, ServicesMixin, ServiceProviderMixin,{ +var CrashManager = AbstractService.extend({ init: function () { var self = this; - this.active = true; + active = true; this.isConnected = true; - mixins.EventDispatcherMixin.init.call(this); + this._super.apply(this, arguments); // crash manager integration core.bus.on('rpc_error', this, this.rpc_error); @@ -128,7 +134,7 @@ var CrashManager = core.Class.extend(mixins.EventDispatcherMixin, ServicesMixin, // listen to unhandled rejected promises, and throw an error when the // promise has been rejected due to a crash - window.addEventListener('unhandledrejection', function (ev) { + core.bus.on('crash_manager_unhandledrejection', this, function (ev) { if (ev.reason && ev.reason instanceof Error) { var traceback = ev.reason.stack; self.show_error({ @@ -144,13 +150,12 @@ var CrashManager = core.Class.extend(mixins.EventDispatcherMixin, ServicesMixin, ev.preventDefault(); } }); - ServiceProviderMixin.init.call(this); }, enable: function () { - this.active = true; + active = true; }, disable: function () { - this.active = false; + active = false; }, handleLostConnection: function () { var self = this; @@ -175,7 +180,12 @@ var CrashManager = core.Class.extend(mixins.EventDispatcherMixin, ServicesMixin, }, delay); }, rpc_error: function(error) { - if (!this.active) { + // Some qunit tests produces errors before the DOM is set. + // This produces an error loop as the modal/toast has no DOM to attach to. + if (!document.body) { + return; + } + if (!active) { return; } if (this.connection_lost) { @@ -229,7 +239,7 @@ var CrashManager = core.Class.extend(mixins.EventDispatcherMixin, ServicesMixin, } }, show_warning: function (error, options) { - if (!this.active) { + if (!active) { return; } var message = error.data ? error.data.message : error.message; @@ -243,7 +253,7 @@ var CrashManager = core.Class.extend(mixins.EventDispatcherMixin, ServicesMixin, }, options)); }, show_error: function (error) { - if (!this.active) { + if (!active) { return; } var dialog = new ErrorDialog(this, { @@ -371,17 +381,12 @@ core.crash_registry.add('504', function (cm) { }; }); +core.serviceRegistry.add('crash_manager', CrashManager); + return { CrashManager: CrashManager, ErrorDialog: ErrorDialog, WarningDialog: WarningDialog, + disable: () => active = false, }; }); - -odoo.define('web.crash_manager', function (require) { -"use strict"; - -var CrashManager = require('web.CrashManager').CrashManager; -return new CrashManager(); - -}); -- cgit v1.2.3