##// END OF EJS Templates
working on cancelation and error handling
cin -
r186:75103928da09 ref20160224
parent child
Show More
@@ -77,19 +77,14 namespace Implab {
77 77 /// <param name="error">Исключение возникшее при выполнении операции</param>
78 78 /// <exception cref="InvalidOperationException">Данное обещание уже выполнено</exception>
79 79 protected void SetError(Exception error) {
80 while (error is PromiseTransientException)
81 error = error.InnerException;
82
83 var isCancel = error is OperationCanceledException;
84
85 80 if (BeginTransit()) {
86 m_error = isCancel ? error.InnerException : error;
87 CompleteTransit(isCancel ? CANCELLED_STATE : REJECTED_STATE);
81 m_error = error;
82 CompleteTransit(REJECTED_STATE);
88 83
89 84 Signal();
90 85 } else {
91 86 WaitTransition();
92 if (!isCancel || m_state == SUCCEEDED_STATE)
87 if (m_state == SUCCEEDED_STATE)
93 88 throw new InvalidOperationException("The promise is already resolved");
94 89 }
95 90 }
@@ -140,11 +135,11 namespace Implab {
140 135 case SUCCEEDED_STATE:
141 136 return;
142 137 case CANCELLED_STATE:
143 throw new OperationCanceledException();
138 throw new OperationCanceledException("The operation has been cancelled", m_error);
144 139 case REJECTED_STATE:
145 140 throw new TargetInvocationException(m_error);
146 141 default:
147 throw new ApplicationException(String.Format("Invalid promise state {0}", m_state));
142 throw new ApplicationException(String.Format("The promise state {0} is invalid", m_state));
148 143 }
149 144 }
150 145 #endregion
@@ -134,7 +134,7 namespace Implab {
134 134 }
135 135
136 136 protected void SetResult() {
137 BeginSetResult();
137 if(BeginSetResult())
138 138 EndSetResult();
139 139 }
140 140 }
@@ -20,35 +20,52 namespace Implab {
20 20 HandleErrorInternal(error);
21 21 }
22 22
23
24
25 23 public override void CancelOperation(Exception reason) {
26 24 if (LockCancelation()) {
25 if (!(reason is OperationCanceledException))
26 reason = reason != null ? new OperationCanceledException(null, reason) : new OperationCanceledException();
27
27 28 if (m_cancel != null) {
28 29 try {
29 m_cancel(reason).On(SetResult, SetError, SetCancelled);
30 m_cancel(reason).On(SetResult, HandleErrorInternal, HandleCancelInternal);
30 31 } catch (Exception err) {
31 32 HandleErrorInternal(err);
32 33 }
33 34 } else {
34 SetCancelled(reason);
35 HandleErrorInternal(reason);
35 36 }
36 37 }
37 38 }
38 39
39 protected void HandleErrorInternal(Exception error) {
40 void HandleCancelInternal(Exception reason) {
41 if (!(reason is OperationCanceledException))
42 reason = reason != null ? new OperationCanceledException(null, reason) : new OperationCanceledException();
43 HandleErrorInternal(reason);
44 }
45
46 void HandleErrorInternal(Exception error) {
40 47 if (m_error != null) {
41 48 try {
42 49 var p = m_error(error);
43 50 p.On(SetResult,SetError,SetCancelled);
44 51 CancellationRequested(p.Cancel);
45 52 } catch (Exception err) {
46 SetError(err);
53 error = err;
47 54 }
48 55 } else {
56 SetErrorInternal(error);
57 }
58 }
59
60 void SetErrorInternal(Exception error) {
61 while (error is PromiseTransientException)
62 error = error.InnerException;
63
64 if (error is OperationCanceledException)
65 SetCancelled(error);
66 else
49 67 SetError(error);
50 68 }
51 }
52 69
53 70 protected bool LockCancelation() {
54 71 return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
@@ -139,14 +139,7 namespace Implab.Components {
139 139 throw new PromiseTransientException(e);
140 140 },
141 141 r => {
142 lock(m_stateMachine) {
143 if (m_pending == promise) {
144 Move(Commands.Fail);
145 m_pending = null;
146 m_lastError = new OperationCanceledException("The operation has been cancelled", r);
147 }
148
149 }
142 // handle cancellation as exception
150 143 throw new OperationCanceledException("The operation has been cancelled", r);
151 144 }
152 145 );
@@ -201,7 +194,7 namespace Implab.Components {
201 194 if (current == null) {
202 195 stop.Resolve();
203 196 } else {
204 current.On(stop.Resolve, stop.Reject, stop.CancelOperation);
197 current.On(stop.Resolve, stop.Reject, e => stop.Resolve());
205 198 current.Cancel();
206 199 }
207 200 }
General Comments 0
You need to be logged in to leave comments. Login now