protected void MarkResultIsCancelledIfNeeded(ResultBase result, Exception ex) { if (_context.Token.IsCancellationRequested && ResultBase.NonCriticalForCancellation(ex)) { // Set the cancellation flag only when the error is non-critical. result.IsCancelled = true; } }
protected T RunOperationAndConvertExceptionToError <T>(Func <T> operation) where T : ResultBase { try { return(operation()); } catch (Exception ex) { var result = new ErrorResult(ex).AsResult <T>(); if (_context.Token.IsCancellationRequested && ResultBase.NonCriticalForCancellation(ex)) { result.IsCancelled = true; } return(result); } }
private async Task <T> RunOperationAndConvertExceptionToErrorAsync <T>(Func <Task <T> > operation) where T : ResultBase { try { return(await operation()); } catch (Exception ex) { var result = new ErrorResult(ex).AsResult <T>(); if (Token.IsCancellationRequested && ResultBase.NonCriticalForCancellation(ex)) { result.IsCancelled = true; } return(result); } }
protected async Task <T> RunOperationAndConvertExceptionToErrorAsync <T>(Func <Task <T> > operation) where T : ResultBase { try { return(await WithOptionalTimeoutAsync(operation(), _timeout)); } catch (Exception ex) { var result = new ErrorResult(ex).AsResult <T>(); if (_context.Token.IsCancellationRequested && ResultBase.NonCriticalForCancellation(ex)) { result.IsCancelled = true; } return(result); } }
protected async Task <T> RunOperationAndConvertExceptionToErrorAsync <T>(Func <Task <T> > operation) where T : ResultBase { try { using var timer = CreatePeriodicTimerIfNeeded(); return(await operation()); } catch (Exception ex) { var result = new ErrorResult(ex).AsResult <T>(); if (_context.Token.IsCancellationRequested && ResultBase.NonCriticalForCancellation(ex)) { result.IsCancelled = true; } return(result); } }
/// <summary> /// Run the call/code. /// </summary> protected TResult Run(Func <TResult> func) { try { _stopwatch.Start(); Result = func(); } catch (Exception exception) { Result = CreateErrorResult(exception); if (_token.IsCancellationRequested && ResultBase.NonCriticalForCancellation(exception)) { Result.IsCancelled = true; } } finally { _stopwatch.Stop(); Result.Duration = _stopwatch.Elapsed; } return(Result); }
protected internal async Task <TResult> RunAsync(Func <Task <TResult> > asyncFunc) { try { _stopwatch.Start(); Result = await asyncFunc(); } catch (Exception exception) { Result = CreateErrorResult(exception); if (_token.IsCancellationRequested && ResultBase.NonCriticalForCancellation(exception)) { Result.MarkCancelled(); } } finally { _stopwatch.Stop(); Result.SetDuration(_stopwatch.Elapsed); } return(Result); }