diff Implab/ActionChainTaskBase.cs @ 186:75103928da09 ref20160224

working on cancelation and error handling
author cin
date Tue, 19 Apr 2016 00:50:14 +0300
parents eb793fbbe4ea
children dd4a3590f9c6
line wrap: on
line diff
--- a/Implab/ActionChainTaskBase.cs	Mon Apr 18 16:41:17 2016 +0300
+++ b/Implab/ActionChainTaskBase.cs	Tue Apr 19 00:50:14 2016 +0300
@@ -20,34 +20,51 @@
                 HandleErrorInternal(error);
         }
 
-
-
         public override void CancelOperation(Exception reason) {
             if (LockCancelation()) {
+                if (!(reason is OperationCanceledException))
+                    reason = reason != null ? new OperationCanceledException(null, reason) : new OperationCanceledException();
+                
                 if (m_cancel != null) {
                     try {
-                        m_cancel(reason).On(SetResult, SetError, SetCancelled);
+                        m_cancel(reason).On(SetResult, HandleErrorInternal, HandleCancelInternal);
                     } catch (Exception err) {
                         HandleErrorInternal(err);
                     }
                 } else {
-                    SetCancelled(reason);
+                    HandleErrorInternal(reason);
                 }
             }
         }
 
-        protected void HandleErrorInternal(Exception error) {
+        void HandleCancelInternal(Exception reason) {
+            if (!(reason is OperationCanceledException))
+                reason = reason != null ? new OperationCanceledException(null, reason) : new OperationCanceledException();
+            HandleErrorInternal(reason);
+        }
+
+        void HandleErrorInternal(Exception error) {
             if (m_error != null) {
                 try {
                     var p = m_error(error);
-                    p.On(SetResult,SetError,SetCancelled);
+                    p.On(SetResult, SetError, SetCancelled);
                     CancellationRequested(p.Cancel);
                 } catch (Exception err) {
-                    SetError(err);
+                    error = err;
                 }
             } else {
+                SetErrorInternal(error);
+            }
+        }
+
+        void SetErrorInternal(Exception error) {
+            while (error is PromiseTransientException)
+                error = error.InnerException;
+
+            if (error is OperationCanceledException)
+                SetCancelled(error);
+            else
                 SetError(error);
-            }
         }
 
         protected bool LockCancelation() {