public override string ToString() { return (typeof(MirroringException).FullName + ": " + Message + Environment.NewLine + StackTrace + Environment.NewLine + string.Join(Environment.NewLine, InnerExceptions.Select(exception => Environment.NewLine + "---> " + exception.ToString()))); }
[PreventExecutionContextLeaks] // Workaround for https://github.com/nunit/nunit/issues/3283 public static void Handler_should_be_called_one_additional_time_to_handle_its_own_exception() { var taskException = new Exception(); var handlerException = new Exception(); var watcher = new CallbackWatcher(); AmbientTasks.BeginContext(ex => { watcher.OnCallback(out var callCount); if (callCount == 1) { throw handlerException; } ex.ShouldBe(handlerException); }); using (watcher.ExpectCallback(count: 2)) AmbientTasks.Add(Task.FromException(taskException)); var waitAllTask = AmbientTasks.WaitAllAsync(); waitAllTask.Status.ShouldBe(TaskStatus.Faulted); var aggregateException = waitAllTask.Exception !.InnerExceptions.ShouldHaveSingleItem().ShouldBeOfType <AggregateException>(); aggregateException.InnerExceptions.ShouldHaveSingleItem().ShouldBeSameAs(taskException); }
public async Task Abort_WhileEnumerating_EnumerationAborted() { var completed = false; var stream = new MessageStreamEnumerable <int>(); using var enumerator = stream.GetEnumerator(); // ReSharper disable once AccessToDisposedClosure var enumerationTask = Task.Run( () => { enumerator.MoveNext(); completed = true; }); completed.Should().BeFalse(); stream.Abort(); // Give the other thread a chance to exit the MoveNext await AsyncTestingUtil.WaitAsync(() => enumerationTask.IsCompleted); completed.Should().BeFalse(); enumerationTask.Status.Should().Be(TaskStatus.Faulted); enumerationTask.Exception !.InnerExceptions.First().Should().BeAssignableTo <OperationCanceledException>(); }
/// <inheritdoc/> public void Dispose() { if (_cancellationTokenSource != null) { _cancellationTokenSource.Cancel(false); _cancellationTokenSource.Dispose(); _cancellationTokenSource = null; } if (_cas == 0) { // Never locked return; } var key = LockDocument.GetKey(Name); _collection.RemoveAsync(key, new RemoveOptions().Cas(_cas)) .ContinueWith(t => { if (t.Exception !.InnerExceptions.OfType <DocumentNotFoundException>().Any()) { _logger.LogDebug("Did not release lock '{name}' for holder '{holder}' because it was already released.", Name, Holder); }
void RecordInnerException(Exception ex) { if (ex == null) { return; } InnerExceptions.Add(DetailsFor(ex)); RecordInnerException(ex.InnerException); }
private string GetStackTrace() { var useFullStack = new StackTrace(this, true).GetFrames()? .Any(s => s.GetMethod().GetCustomAttributes <FullStackTraceAttribute>().Any()) ?? false; if (useFullStack) { return(FullStackTrace); } return(InnerExceptions?.FirstOrDefault()?.StackTrace ?? FullStackTrace); }
void RecordInnerException(Exception ex) { if (ex == null) { return; } InnerExceptions.Add(new ExceptionDetail { Message = ex.Message, StackTrace = ex.StackTrace }); RecordInnerException(ex.InnerException); }
public static Task <int> Get_Task_TResult() { var task = Task.Run(() => { Thread.Sleep(1_000); return(263_414_974); }); if (task.IsCompletedSuccessfully) { Console.WriteLine(task.Result); } if (task.IsFaulted) { Console.WriteLine(task.Exception !.InnerExceptions.Select(e => e.Message)); } return(task); }
public ExceptionInfo(Exception exception, bool isNestedException = false) { if (exception == null) { throw new ArgumentNullException(nameof(exception)); } ErrorType = exception.GetType().Name; ErrorMessage = exception.Message; if (!string.IsNullOrEmpty(exception.StackTrace)) { StackTrace stackTrace = new StackTrace(exception, true); StackTrace = stackTrace.ToString(); // Only extract the stack frames like this for the top-level exception // This is used for Xray Exception serialization if (isNestedException || stackTrace?.GetFrames() == null) { StackFrames = new StackFrameInfo[0]; } else { StackFrames = ( from sf in stackTrace.GetFrames() where sf != null select new StackFrameInfo(sf) ).ToArray(); } } if (exception.InnerException != null) { InnerException = new ExceptionInfo(exception.InnerException, true); } AggregateException aggregateException = exception as AggregateException; if (aggregateException != null && aggregateException.InnerExceptions != null) { foreach (var innerEx in aggregateException.InnerExceptions) { InnerExceptions.Add(new ExceptionInfo(innerEx, true)); } } }
[PreventExecutionContextLeaks] // Workaround for https://github.com/nunit/nunit/issues/3283 public static void Exception_from_user_delegate_is_in_task_from_next_call_to_WaitAllAsync_when_there_is_no_BeginContext_handler([Values] PostOverload overload) { var exception = new Exception(); using (SynchronizationContextAssert.ExpectSinglePost(postedAction => postedAction.Invoke())) { Should.Throw <Exception>(() => AmbientTasksPost(overload, () => throw exception)); } var waitAllTask = AmbientTasks.WaitAllAsync(); waitAllTask.Status.ShouldBe(TaskStatus.Faulted); var aggregateException = waitAllTask.Exception !.InnerExceptions.ShouldHaveSingleItem().ShouldBeOfType <AggregateException>(); aggregateException.InnerExceptions.ShouldHaveSingleItem().ShouldBeSameAs(exception); }
[PreventExecutionContextLeaks] // Workaround for https://github.com/nunit/nunit/issues/3283 public static void WaitAllAsync_should_have_single_AggregateException_with_all_exceptions_from_each_task_all_faulted_synchronously() { var task1Exceptions = new[] { new Exception("Task 1 exception 1"), new Exception("Task 1 exception 2") }; var task2Exceptions = new[] { new Exception("Task 2 exception 1"), new Exception("Task 2 exception 2") }; var source1 = new TaskCompletionSource <object?>(); var source2 = new TaskCompletionSource <object?>(); AmbientTasks.Add(source1.Task); AmbientTasks.Add(source2.Task); source1.SetException(task1Exceptions); source2.SetException(task2Exceptions); var waitAllTask = AmbientTasks.WaitAllAsync(); waitAllTask.Status.ShouldBe(TaskStatus.Faulted); var aggregateException = waitAllTask.Exception !.InnerExceptions.ShouldHaveSingleItem().ShouldBeOfType <AggregateException>(); aggregateException.InnerExceptions.ShouldBe(task1Exceptions.Concat(task2Exceptions)); }
[PreventExecutionContextLeaks] // Workaround for https://github.com/nunit/nunit/issues/3283 public static void WaitAllAsync_should_have_single_AggregateException_with_all_three_exceptions_when_handler_throws_exception_twice() { var taskException = new Exception(); var handlerException1 = new Exception(); var handlerException2 = new Exception(); var watcher = new CallbackWatcher(); AmbientTasks.BeginContext(ex => { watcher.OnCallback(out var callCount); throw callCount == 1 ? handlerException1 : handlerException2; }); using (watcher.ExpectCallback(count: 2)) AmbientTasks.Add(Task.FromException(taskException)); var waitAllTask = AmbientTasks.WaitAllAsync(); waitAllTask.Status.ShouldBe(TaskStatus.Faulted); var aggregateException = waitAllTask.Exception !.InnerExceptions.ShouldHaveSingleItem().ShouldBeOfType <AggregateException>(); aggregateException.InnerExceptions.ShouldBe(new[] { taskException, handlerException1, handlerException2 }); }
internal void DataflowBlockCompleted(IDataflowBlock block) { Debug.Assert(block != null, "Block needed for the ETW event."); if (IsEnabled(EventLevel.Informational, ALL_KEYWORDS)) { Task?completionTask = Common.GetPotentiallyNotSupportedCompletionTask(block); bool blockIsCompleted = completionTask != null && completionTask.IsCompleted; Debug.Assert(blockIsCompleted, "Block must be completed for this event to be valid."); if (blockIsCompleted) { var reason = (BlockCompletionReason)completionTask !.Status; string exceptionData = string.Empty; if (completionTask.IsFaulted) { try { exceptionData = string.Join(Environment.NewLine, completionTask.Exception !.InnerExceptions.Select(e => e.ToString())); } catch { } } DataflowBlockCompleted(Common.GetBlockId(block), reason, exceptionData); } } }
/// <summary> /// Add an inner exception to the <see cref="IocContainerException.InnerExceptions"/> /// </summary> /// <param name="exception">The <see cref="ConstructorNotMatchingException"/></param> public void AddInnerException(ConstructorNotMatchingException exception) => InnerExceptions.Add(exception);
public CustomException(IEnumerable <Exception> innerExceptions) : this() { InnerExceptions.AddRange(innerExceptions); }
public CommandRevertException Collapse(CommandRevertException revertException) { return(new CommandRevertException(InnerExceptions.Concat(revertException.InnerExceptions))); }