public static void Handle() { AggregateException ex = new AggregateException(); ex = new AggregateException(new[] { new ArgumentException(), new ArgumentException(), new ArgumentException() }); int handledCount = 0; ex.Handle((e) => { if (e is ArgumentException) { handledCount++; return true; } return false; }); Assert.Equal(handledCount, ex.InnerExceptions.Count); }
public static void HandleInvalidCases() { AggregateException ex = new AggregateException(); Assert.Throws<ArgumentNullException>(() => ex.Handle(null)); ex = new AggregateException(new[] { new Exception(), new ArgumentException(), new ArgumentException() }); int handledCount = 0; Assert.Throws<AggregateException>( () => ex.Handle((e) => { if (e is ArgumentException) { handledCount++; return true; } return false; })); }
private void ApnsBroker_OnNotificationFailed(ApnsNotification notification, AggregateException exception) { exception.Handle(ex => { // See what kind of exception it was to further diagnose if(ex is ApnsNotificationException) { var notificationException = ex as ApnsNotificationException; // Deal with the failed notification var apnsNotification = notificationException.Notification; var statusCode = notificationException.ErrorStatusCode; Debug.WriteLine($"Apple Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}"); } else { // Inner exception might hold more useful information like an ApnsConnectionException Debug.WriteLine($"Apple Notification Failed for some unknown reason : {ex.InnerException}"); } // Mark it as handled return true; }); }
protected void AssertLinkedInApiException(AggregateException ae, string expectedMessage, LinkedInApiError error) { ae.Handle(ex => { if (ex is LinkedInApiException) { Assert.AreEqual(expectedMessage, ex.Message); Assert.AreEqual(error, ((LinkedInApiException)ex).Error); return true; } return false; }); }
private void GcmBroker_OnNotificationFailed(GcmNotification notification, AggregateException exception) { exception.Handle(ex => { // See what kind of exception it was to further diagnose if(ex is GcmNotificationException) { var notificationException = ex as GcmNotificationException; // Deal with the failed notification var gcmNotification = notificationException.Notification; var description = notificationException.Description; Debug.WriteLine($"GCM Notification Failed: ID={gcmNotification.MessageId}, Desc={description}"); } else if(ex is GcmMulticastResultException) { var multicastException = ex as GcmMulticastResultException; foreach(var succeededNotification in multicastException.Succeeded) { Debug.WriteLine($"GCM Notification Failed: ID={succeededNotification.MessageId}"); } foreach(var failedKvp in multicastException.Failed) { var n = failedKvp.Key; var e = failedKvp.Value; Debug.WriteLine($"GCM Notification Failed: ID={n.MessageId}, Desc={e.Message}"); } } else if(ex is DeviceSubscriptionExpiredException) { var expiredException = ex as DeviceSubscriptionExpiredException; var oldId = expiredException.OldSubscriptionId; var newId = expiredException.NewSubscriptionId; Debug.WriteLine($"Device RegistrationId Expired: {oldId}"); if(!string.IsNullOrWhiteSpace(newId)) { // If this value isn't null, our subscription changed and we should update our database Debug.WriteLine($"Device RegistrationId Changed To: {newId}"); } } else if(ex is RetryAfterException) { var retryException = (RetryAfterException)ex; // If you get rate limited, you should stop sending messages until after the RetryAfterUtc date Debug.WriteLine($"GCM Rate Limited, don't send more until after {retryException.RetryAfterUtc}"); } else { Debug.WriteLine("GCM Notification Failed for some unknown reason"); } // Mark it as handled return true; }); }
private void HandleDatabaseException(AggregateException exception) { exception.Handle(e => { if (!(e is Domain.Exceptions.DomainValidationException)) { _logger.Log(Level.Error, "Data operation failed to successfully complete, exception: {0}.", e.GetType().Name); MessengerInstance.Send<DialogMessage>(new DialogMessage("An error occured and the test was not saved", msg => { })); } return true; }); }
// tests helpers #if NET_4_0 || SILVERLIGHT_5 private void AssertDropboxApiException(AggregateException ae, string expectedMessage, DropboxApiError error) { ae.Handle(ex => { if (ex is DropboxApiException) { Assert.AreEqual(expectedMessage, ex.Message); Assert.AreEqual(error, ((DropboxApiException)ex).Error); return true; } return false; }); }
/// <summary> /// Converts an AggregateException to a printable form, including inner exceptions etc. /// </summary> public static string AggregateExceptionToString(AggregateException aex, bool includeStackTrace) { var sb = new StringBuilder(); aex.Handle(ex => { if (ex is TargetInvocationException) { while (ex.InnerException != null) { sb.Append(ex.InnerException.Message + "\n"); if (includeStackTrace) sb.Append(ex.InnerException.StackTrace + "\n"); ex = ex.InnerException; } } else { sb.Append(ex.Message + "\n"); if (includeStackTrace) sb.Append(ex.StackTrace + "\n"); } return true; }); return sb.ToString(); }
private void _HandleException(AggregateException e) { e.Handle((ex) => { Debug.WriteLine(e); return true; }); }