public virtual void SendsIndividualErrorWhenOneOfTheRequestsFails() { Uri serviceUrl = new Uri(BaseAddress + "/DefaultBatch"); Default.Container client = new Default.Container(serviceUrl); client.Format.UseJson(); Default.DefaultBatchCustomer validCustomer = new Default.DefaultBatchCustomer() { Id = 10, Name = "Customer 10" }; Default.DefaultBatchCustomer invalidCustomer = new Default.DefaultBatchCustomer() { Id = -1, Name = "Customer -1" }; client.AddToDefaultBatchCustomer(validCustomer); client.AddToDefaultBatchCustomer(invalidCustomer); DataServiceRequestException exception = Assert.Throws <DataServiceRequestException>(() => { DataServiceResponse response = client.SaveChanges(SaveChangesOptions.Batch); }); Assert.Equal(202, exception.Response.BatchStatusCode); Assert.Equal(1, exception.Response.Count()); }
static string GetErrorCode(DataServiceRequestException ex) { var r = new Regex(@"<code>(\w+)</code>", RegexOptions.IgnoreCase); var match = r.Match(ex.InnerException.Message); return(match.Groups[1].Value); }
// if Batch mode as well private void ProcessDataServiceRequest(DataServiceRequestException dsre) { if (dsre.InnerException != null) { string jsonResponse = dsre.InnerException.Message; // Some messages are not json formatted // check the exact format! (Java vs .Net) try { ODataServiceSdlResponse response = JsonConvert.DeserializeObject <ODataServiceSdlResponse>(jsonResponse); this.errorMessage = response.Error.Message; if (response.Error.InnerMessage != null) { this.errorExtraMessage = response.Error.InnerMessage.Message; } } catch (Exception jsonEx) { this.errorMessage = jsonResponse; } } else { this.errorMessage = dsre.Message; } }
public void RetriesWhenDataServiceRequestExceptionIsThrownForServerBusy() { int executeCount = 0; try { var retryPolicy = this.retryManager.GetRetryPolicy <StorageTransientErrorDetectionStrategy>("Retry 5 times"); retryPolicy.ExecuteAction(() => { executeCount++; var innerException = new DataServiceClientException(Microsoft.WindowsAzure.StorageClient.StorageErrorCodeStrings.ServerBusy); var ex = new DataServiceRequestException(Microsoft.WindowsAzure.StorageClient.StorageErrorCodeStrings.ServerBusy, innerException); throw ex; }); Assert.Fail("Should have thrown DataServiceRequestException"); } catch (DataServiceRequestException) { } catch (Exception) { Assert.Fail("Should have thrown DataServiceRequestException"); } Assert.AreEqual(6, executeCount); }
/// <summary> /// Process a <see cref="DataServiceRequestException"/> and converts it to a PowerShell /// <see cref="ErrorRecord"/>, or <c>null</c> if that is not availaible. /// </summary> /// <param name="ex">The <see cref="DataServiceRequestException"/> that was thrown.</param> private static ErrorRecord AsErrorRecord(this DataServiceRequestException ex) { ErrorRecord errorRecord = null; Exception exceptionToThrow = ex; // Look for Sql exception message in response, return that message if found. foreach (ChangeOperationResponse change in ex.Response) { // Try to parse the extended properties in the Exception ManagementServiceExceptionInfo info; if (ManagementServiceExceptionInfo.TryParse(change, out info)) { if (info.PropertyBag.Contains(DataServiceConstants.SqlMessageTextKey)) { // Set the exception to throw as a new exception with the server message string errorDetails = info.PropertyBag[DataServiceConstants.SqlMessageTextKey].ToString(); errorRecord = new ErrorRecord( new CommunicationException(errorDetails, ex), string.Empty, ErrorCategory.InvalidOperation, null); break; } } } // Fall back to use the message in the message element if availiable if (errorRecord == null) { foreach (ChangeOperationResponse change in ex.Response) { try { XElement errorRoot = XElement.Parse(change.Error.Message); XElement messageElement = errorRoot.Descendants().Single(x => string.Equals(x.Name.LocalName, "message", StringComparison.OrdinalIgnoreCase)); errorRecord = new ErrorRecord( new CommunicationException(messageElement.Value, ex), string.Empty, ErrorCategory.InvalidOperation, null); break; } catch (Exception) { // we hide any parsing error that might have happened because there is no need to expose // additional errors } } } // Return the resulting error record return(errorRecord); }
public void WebRequestTransientErrorDetectionStrategyTestDataServiceRequestException() { // Unfortunately this exception isn't easy to Mock with an actual error code so just // do a basic test var exception = new DataServiceRequestException("Simulated DataServiceRequestException"); bool actual = new WebRequestTransientErrorDetectionStrategy().IsTransient(exception); Assert.IsFalse(actual); }
public ODataError(Exception ex) { if (ex == null) { throw new ArgumentNullException(); } Type errorType = ex.GetType(); // Awful code if (ex is DataServiceRequestException) { DataServiceRequestException dsre = ex as DataServiceRequestException; this.ProcessDataServiceRequest(dsre); return; } if (ex is DataServiceClientException) { DataServiceClientException dsce = ex as DataServiceClientException; this.ProcessDataServiceClient(dsce); return; } if (ex is DataServiceTransportException) { DataServiceTransportException dste = ex as DataServiceTransportException; this.ProcessDataServiceTransport(dste); return; } if (ex is DataServiceQueryException) { DataServiceQueryException dsqe = ex as DataServiceQueryException; this.ProcessDataServiceQuery(dsqe); return; } if (ex is InvalidOperationException) { InvalidOperationException inoper = ex as InvalidOperationException; this.ProcessInvalidOperation(inoper); return; } if (ex is NotImplementedException) { NotImplementedException notimpl = ex as NotImplementedException; this.ProcessNotImplemented(notimpl); return; } }
private static string GetErrorCode(DataServiceRequestException ex) { if (ex != null && ex.InnerException != null) { var match = errorCodeExtractor.Match(ex.InnerException.Message); return(match.Success ? match.Groups[1].Value : null); } else { return(null); } }
/// <summary> /// Gets the error code string from the exception. /// </summary> /// <param name="ex">The exception that contains the error code as a string inside the message.</param> /// <returns>The error code string.</returns> protected static string GetErrorCode(DataServiceRequestException ex) { string value = null; if (ex != null && ex.InnerException != null) { var match = _errorCodeRegex.Match(ex.InnerException.Message); value = match.Groups[1].Value; } return(value); }
/// <summary> /// Indicates whether the specified <see cref="Exception"/> is /// related to an endpoint not being available. /// </summary> /// <param name="exception"></param> /// <returns></returns> public static bool IsEndpointDown(System.Exception exception) { if (exception == null) { throw new ArgumentNullException("exception"); } EndpointNotFoundException endpointNotFoundException = exception as EndpointNotFoundException; if (endpointNotFoundException != null) { return(true); } ServerTooBusyException serverTooBusyException = exception as ServerTooBusyException; if (serverTooBusyException != null) { WebException webException = serverTooBusyException.InnerException as WebException; return(IsEndpointDown(webException)); } TimeoutException timeoutException = exception as TimeoutException; if (timeoutException != null) { return(true); } DataServiceTransportException dataServiceTransportException = exception as DataServiceTransportException; if (dataServiceTransportException != null) { WebException webException = dataServiceTransportException.InnerException as WebException; return(IsEndpointDown(webException)); } DataServiceRequestException dataServiceRequestException = exception as DataServiceRequestException; if (dataServiceRequestException != null) { DataServiceClientException dataServiceClientException = dataServiceRequestException.InnerException as DataServiceClientException; if (dataServiceClientException != null) { WebException webException = dataServiceClientException.InnerException as WebException; return(IsEndpointDown(webException)); } } return(false); }
/// <summary> /// Retrieves the exception details contained in the exception and wrap it in a PowerShell <see cref="ErrorRecord"/>. /// </summary> /// <param name="exception">The exception containing the error details.</param> /// <param name="errorRecord">An output parameter for the error record containing the error details.</param> /// <param name="requestId">An output parameter for the request Id present in the reponse headers.</param> internal static ErrorRecord RetrieveExceptionDetails( Exception exception, out string requestId) { ErrorRecord errorRecord = null; requestId = null; // Look for known exceptions through the exceptions and inner exceptions Exception innerException = exception; while (innerException != null) { CloudException cloudException = innerException as CloudException; if (cloudException != null) { errorRecord = cloudException.AsErrorRecord(out requestId); break; } DataServiceRequestException dataServiceRequestException = innerException as DataServiceRequestException; if (dataServiceRequestException != null) { errorRecord = dataServiceRequestException.AsErrorRecord(); break; } DataServiceClientException dataServiceClientException = innerException as DataServiceClientException; if (dataServiceClientException != null) { errorRecord = dataServiceClientException.AsErrorRecord(); break; } WebException webException = innerException as WebException; if (webException != null) { errorRecord = webException.AsErrorRecord(out requestId); break; } innerException = innerException.InnerException; } // If it's here, it was an unknown exception, wrap the original exception as is. if (errorRecord == null) { errorRecord = new ErrorRecord(exception, string.Empty, ErrorCategory.NotSpecified, null); } return(errorRecord); }
protected static string GetErrorCode(DataServiceRequestException ex) { if (ex != null && ex.InnerException != null) { var match = errorCodeRegex.Match(ex.InnerException.Message); return(match.Groups[1].Value); } else { return(null); } }
private static string GetErrorCode(DataServiceRequestException ex) { if (ex != null && ex.InnerException != null) { var regEx = new Regex(@"<code>(\w+)</code>", RegexOptions.IgnoreCase); var match = regEx.Match(ex.InnerException.Message); return(match.Groups[1].Value); } else { return(null); } }
private void PurgeTraceLogTable() { var storageAccount = new CloudStorageAccount(this.tableClient.Credentials, null, null, this.tableClient.BaseUri); var tableStorage = storageAccount.CreateCloudTableClient(); tableStorage.DeleteTableIfExist(CommonConsts.DiagnosticTraceLogsTableName); while (true) { try { if (!tableStorage.DoesTableExist(CommonConsts.DiagnosticTraceLogsTableName)) { tableStorage.CreateTableIfNotExist(CommonConsts.DiagnosticTraceLogsTableName); break; } } catch (StorageClientException ex) { Exception error = ex; bool terminate = true; while (error.InnerException != null) { if (error.InnerException is DataServiceRequestException) { DataServiceRequestException dsError = error.InnerException as DataServiceRequestException; if (dsError.IsTableBeingDeleted()) { terminate = false; } } error = error.InnerException; } if (terminate) { break; } } Thread.Sleep(TimeSpan.FromSeconds(5)); } }
public void ErrorCodeExtraction() { // HACK: just reproducing the code being tested, no direct linking var r = new Regex(@"<code>(\w+)</code>", RegexOptions.IgnoreCase); var errorMessage = @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?> <error xmlns=""http://schemas.microsoft.com/ado/2007/08/dataservices/metadata""> <code>OperationTimedOut</code> <message xml:lang=""en-US"">Operation could not be completed within the specified time. RequestId:f8e1e934-99ca-4a6f-bca7-e8e5fbd059ea Time:2010-01-15T12:37:25.1611631Z</message> </error>"; var ex = new DataServiceRequestException("", new Exception(errorMessage)); Assert.AreEqual("OperationTimedOut", GetErrorCode(ex)); }
internal bool HandleException(Exception exception) { Nullable <HttpStatusCode> httpStatusCode = null; DataServiceRequestException requestException = exception as DataServiceRequestException; if (requestException != null) { OperationResponse opResponse = requestException.Response.FirstOrDefault(); httpStatusCode = opResponse != null ? (HttpStatusCode)opResponse.StatusCode : (HttpStatusCode)requestException.Response.BatchStatusCode; } DataServiceClientException clientException = exception as DataServiceClientException; if (!httpStatusCode.HasValue && clientException != null) { httpStatusCode = (HttpStatusCode)clientException.StatusCode; } DataServiceQueryException queryException = exception as DataServiceQueryException; if (!httpStatusCode.HasValue && queryException != null) { httpStatusCode = (HttpStatusCode)queryException.Response.StatusCode; } ODataErrorException odataException = exception as ODataErrorException; if (!httpStatusCode.HasValue && odataException != null) { var _errorCode = odataException.Error; } if (exception is AdalException || exception is ODataErrorException) { return(false); } return(true); }
public void StorageTransientErrorDetectionStrategyTestDataServiceRequestException() { List <String> allStorageErrorCodeStrings = GetAllStorageErrorStringConstants(); StorageTransientErrorDetectionStrategy strategy = new StorageTransientErrorDetectionStrategy(); foreach (string errorString in allStorageErrorCodeStrings) { var innerException = new Exception(FormatErrorString(errorString)); var exception = new DataServiceRequestException("Simulated DataServiceRequestException", innerException); if (strategy.IsTransient(exception)) { Assert.IsTrue(SupportedRetryableStorageErrorStrings.Contains(errorString)); } else { Assert.IsFalse(SupportedRetryableStorageErrorStrings.Contains(errorString)); } } }
public static bool IsTableBeingDeleted(this DataServiceRequestException ex) { return(IsErrorStringMatch(GetErrorCode(ex), TableErrorCodeStrings.TableBeingDeleted)); }
/// <summary> /// Translates the data service exception. /// </summary> /// <param name="e">The exception.</param> /// <param name="reqResult">The request result.</param> /// <param name="parseError">The delegate used to parse the error to get extended error information.</param> /// <returns> /// The translated exception. /// </returns> internal static StorageException TranslateDataServiceException(Exception e, RequestResult reqResult, Func <Stream, IDictionary <string, string>, StorageExtendedErrorInformation> parseError) { try { // The exception thrown is based on whether it is a change/query operation. DataServiceRequestException dsre = FindInnerExceptionOfType <DataServiceRequestException>(e); DataServiceQueryException dsqe = FindInnerExceptionOfType <DataServiceQueryException>(e); if (dsre == null && dsqe == null) { InvalidOperationException ioe = TableUtilities.FindInnerExceptionOfType <InvalidOperationException>(e); if (ioe != null && !(ioe is WebException) && string.CompareOrdinal(ioe.Source, "Microsoft.Data.Services.Client") == 0 && ioe.Message.Contains("type is not compatible with the expected")) { return(new StorageException(reqResult, e.Message, e) { IsRetryable = false }); } return(null); } else if (dsre != null) { DataServiceResponse response = dsre.Response; // Get the batch status code first in case batch does not contain any responses. reqResult.HttpStatusCode = response.BatchStatusCode; IDictionary <string, string> headers; foreach (OperationResponse operationResponse in response) { reqResult.HttpStatusCode = operationResponse.StatusCode; // The exception thrown will contain the first error in the group of requests. if (reqResult.HttpStatusCode >= 300) { headers = operationResponse.Headers; // Strip off the extra exception type at the beginning. string innerException = dsre.InnerException.ToString().Replace("System.Data.Services.Client.DataServiceClientException: ", string.Empty); using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(innerException))) { reqResult.ExtendedErrorInformation = parseError(stream, headers); } break; } } return(new StorageException( reqResult, reqResult.ExtendedErrorInformation != null ? reqResult.ExtendedErrorInformation.ErrorCode : dsre.Message, dsre)); } else { QueryOperationResponse response = dsqe.Response; reqResult.HttpStatusCode = response.StatusCode; string innerException = dsqe.InnerException.Message; using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(innerException))) { reqResult.ExtendedErrorInformation = parseError(stream, response.Headers); } return(new StorageException( reqResult, reqResult.ExtendedErrorInformation != null ? reqResult.ExtendedErrorInformation.ErrorCode : dsqe.Message, dsqe)); } } catch (Exception) { return(new StorageException(reqResult, e.Message, e)); } }
public static bool IsEntityAlreadyExists(this DataServiceRequestException ex) { return(IsErrorStringMatch(GetErrorCode(ex), TableErrorCodeStrings.EntityAlreadyExists)); }
protected override bool CheckIsTransient(Exception ex) { if (IsRetriableWebException(ex, operationIdempotentOnRetry: true, retryOnUnauthorizedErrors: true)) { return(true); } DataServiceRequestException dataServiceException = ex as DataServiceRequestException; if (dataServiceException != null) { if (IsErrorStringMatch(GetErrorCode(dataServiceException), StorageErrorCodeStrings.InternalError, StorageErrorCodeStrings.ServerBusy, StorageErrorCodeStrings.OperationTimedOut, TableErrorCodeStrings.TableServerOutOfMemory)) { return(true); } } DataServiceQueryException dataServiceQueryException = ex as DataServiceQueryException; if (dataServiceQueryException != null) { if (IsErrorStringMatch(GetErrorCode(dataServiceQueryException), StorageErrorCodeStrings.InternalError, StorageErrorCodeStrings.ServerBusy, StorageErrorCodeStrings.OperationTimedOut, TableErrorCodeStrings.TableServerOutOfMemory)) { return(true); } } DataServiceClientException dataServiceClientException = ex.FindInnerException <DataServiceClientException>(); if (dataServiceClientException != null) { if (IsRetriableHttpStatusCode(dataServiceClientException.StatusCode, operationIdempotentOnRetry: true, retryOnUnauthorizedErrors: true)) { return(true); } if (IsErrorStringMatch(dataServiceClientException.Message, StorageErrorCodeStrings.InternalError, StorageErrorCodeStrings.ServerBusy, StorageErrorCodeStrings.OperationTimedOut, TableErrorCodeStrings.TableServerOutOfMemory)) { return(true); } } var serverException = ex as StorageException; if (serverException != null) { if (IsErrorStringMatch(serverException, StorageErrorCodeStrings.InternalError, StorageErrorCodeStrings.ServerBusy, StorageErrorCodeStrings.OperationTimedOut, TableErrorCodeStrings.TableServerOutOfMemory)) { return(true); } } if (IsTimeoutException(ex)) { return(true); } if (IsSocketException(ex)) { return(true); } if ((ex.FindInnerException <IOException>() != null) && !(ex is FileLoadException)) { return(true); } return(false); }