Bartosz Dziewoński has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/356190 )

Change subject: WindowManager: Handle errors better in #closeWindow
......................................................................

WindowManager: Handle errors better in #closeWindow

* If no window is currently opened, don't throw internal exceptions.
  Generate a fake window instance and pretend it failed to close.
* Actually, we should probably do the same for all other error cases,
  to avoid clobbering the state of the actually open window instance.

Bug: T166527
Change-Id: I15e1115185a02b9244a5f691610e5bc822e844dd
---
M src/WindowManager.js
1 file changed, 25 insertions(+), 27 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/oojs/ui refs/changes/90/356190/1

diff --git a/src/WindowManager.js b/src/WindowManager.js
index 1cc73ef..bb190f5 100644
--- a/src/WindowManager.js
+++ b/src/WindowManager.js
@@ -445,7 +445,8 @@
  * @fires closing
  */
 OO.ui.WindowManager.prototype.closeWindow = function ( win, data ) {
-       var manager = this,
+       var error,
+               manager = this,
                compatClosing = $.Deferred(),
                lifecycle = this.lifecycle,
                compatOpened;
@@ -457,36 +458,33 @@
                win = null;
        }
 
+       // Error handling
+       if ( !lifecycle ) {
+               error = 'Cannot close window: no window is currently open';
+       } else if ( !win ) {
+               error = 'Cannot close window: window is not attached to 
manager';
+       } else if ( win !== this.currentWindow ) {
+               error = 'Cannot close window: window already closed with 
different data';
+       } else if ( this.preparingToClose || this.closing ) {
+               error = 'Cannot close window: window already closing with 
different data';
+       }
+
+       if ( error ) {
+               // This function was called for the wrong window and we don't 
want to mess with the current
+               // window's state.
+               lifecycle = new OO.ui.WindowInstance();
+               // Pretend the window has been opened, so that we can pretend 
to fail to close it.
+               lifecycle.state.opening.resolve( {} );
+               lifecycle.state.opened.resolve( {} );
+       }
+
        // Turn lifecycle into a Thenable for backwards-compatibility with
        // the deprecated nested-promise behaviour (T163510).
        lifecycle.then = compatClosing.then;
 
-       // Error handling
-       if ( !win ) {
-               compatClosing.reject( new OO.ui.Error(
-                       'Cannot close window: window is not attached to manager'
-               ) );
-               lifecycle.state.closing.reject( new OO.ui.Error(
-                       'Cannot close window: window is not attached to manager'
-               ) );
-               return lifecycle;
-       }
-       if ( win !== this.currentWindow ) {
-               compatClosing.reject( new OO.ui.Error(
-                       'Cannot close window: window already closed with 
different data'
-               ) );
-               lifecycle.state.closing.reject( new OO.ui.Error(
-                       'Cannot close window: window already closed with 
different data'
-               ) );
-               return lifecycle;
-       }
-       if ( this.preparingToClose || this.closing ) {
-               compatClosing.reject( new OO.ui.Error(
-                       'Cannot close window: window already closing with 
different data'
-               ) );
-               lifecycle.state.closing.reject( new OO.ui.Error(
-                       'Cannot close window: window already closing with 
different data'
-               ) );
+       if ( error ) {
+               compatClosing.reject( new OO.ui.Error( error ) );
+               lifecycle.state.closing.reject( new OO.ui.Error( error ) );
                return lifecycle;
        }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/356190
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I15e1115185a02b9244a5f691610e5bc822e844dd
Gerrit-PatchSet: 1
Gerrit-Project: oojs/ui
Gerrit-Branch: master
Gerrit-Owner: Bartosz Dziewoński <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to