Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CosmosException: Fixes the headers not matching CosmosException property values #2262

Merged
merged 5 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,10 @@ private async ValueTask RefreshCachedTokenWithRetryHelperAsync(ITrace trace)

throw CosmosExceptionFactory.CreateRequestTimeoutException(
message: ClientResources.FailedToGetAadToken,
subStatusCode: (int)SubStatusCodes.FailedToGetAadToken,
headers: new Headers()
{
SubStatusCode = SubStatusCodes.FailedToGetAadToken,
},
innerException: lastException,
trace: getTokenTrace);
}
Expand Down Expand Up @@ -253,7 +256,10 @@ private async ValueTask RefreshCachedTokenWithRetryHelperAsync(ITrace trace)

throw CosmosExceptionFactory.CreateUnauthorizedException(
message: ClientResources.FailedToGetAadToken,
subStatusCode: (int)SubStatusCodes.FailedToGetAadToken,
headers: new Headers()
{
SubStatusCode = SubStatusCodes.FailedToGetAadToken,
},
innerException: lastException,
trace: trace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ public FeedRangePartitionKeyRange(string partitionKeyRangeId)
{
throw CosmosExceptionFactory.Create(
statusCode: HttpStatusCode.Gone,
subStatusCode: (int)SubStatusCodes.PartitionKeyRangeGone,
message: $"The PartitionKeyRangeId: \"{this.PartitionKeyRangeId}\" is not valid for the current container {containerRid} .",
stackTrace: string.Empty,
activityId: string.Empty,
requestCharge: 0,
retryAfter: null,
headers: null,
headers: new Headers()
{
SubStatusCode = SubStatusCodes.PartitionKeyRangeGone,
},
error: null,
innerException: null,
trace: NoOpTrace.Singleton);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ private async Task<HttpResponseMessage> SendHttpHelperAsync(
$"GatewayStoreClient Request Timeout. Start Time UTC:{startDateTimeUtc}; Total Duration:{(DateTime.UtcNow - startDateTimeUtc).TotalMilliseconds} Ms; Request Timeout {requestTimeout.TotalMilliseconds} Ms; Http Client Timeout:{this.httpClient.Timeout.TotalMilliseconds} Ms; Activity id: {System.Diagnostics.Trace.CorrelationManager.ActivityId};";
throw CosmosExceptionFactory.CreateRequestTimeoutException(
message,
headers: new Headers()
{
ActivityId = System.Diagnostics.Trace.CorrelationManager.ActivityId.ToString()
},
innerException: operationCanceledException,
trace: helperTrace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ public static CosmosException CreateFromException(Exception exception)
}

return CosmosExceptionFactory.CreateInternalServerErrorException(
subStatusCode: default,
message: exception.Message,
stackTrace: exception.StackTrace,
activityId: EmptyGuidString,
requestCharge: 0,
retryAfter: null,
headers: null,
headers: new Headers()
{
ActivityId = EmptyGuidString,
},
trace: NoOpTrace.Singleton,
innerException: exception);
}
Expand All @@ -81,13 +80,9 @@ private static CosmosException CreateFromExceptionWithStackTrace(ExceptionWithSt
CosmosException cosmosException = ExceptionToCosmosException.CreateFromException(exceptionWithStackTrace.InnerException);
return CosmosExceptionFactory.Create(
cosmosException.StatusCode,
cosmosException.SubStatusCode,
cosmosException.Message,
exceptionWithStackTrace.StackTrace,
cosmosException.ActivityId,
cosmosException.RequestCharge,
cosmosException.RetryAfter,
cosmosException.Headers,
headers: cosmosException.Headers,
cosmosException.Trace,
cosmosException.Error,
cosmosException.InnerException);
Expand All @@ -103,15 +98,18 @@ private QueryExceptionConverter()

public override CosmosException Visit(MalformedContinuationTokenException malformedContinuationTokenException) => CosmosExceptionFactory.CreateBadRequestException(
message: malformedContinuationTokenException.Message,
headers: new Headers(),
stackTrace: malformedContinuationTokenException.StackTrace,
innerException: malformedContinuationTokenException);

public override CosmosException Visit(UnexpectedQueryPartitionProviderException unexpectedQueryPartitionProviderException) => CosmosExceptionFactory.CreateInternalServerErrorException(
message: $"{nameof(CosmosException)} due to {nameof(UnexpectedQueryPartitionProviderException)}",
headers: new Headers(),
innerException: unexpectedQueryPartitionProviderException);

public override CosmosException Visit(ExpectedQueryPartitionProviderException expectedQueryPartitionProviderException) => CosmosExceptionFactory.CreateBadRequestException(
message: expectedQueryPartitionProviderException.Message,
headers: new Headers(),
stackTrace: expectedQueryPartitionProviderException.StackTrace,
innerException: expectedQueryPartitionProviderException);
}
Expand All @@ -127,6 +125,7 @@ private ChangeFeedExceptionConverter()
internal override CosmosException Visit(
MalformedChangeFeedContinuationTokenException malformedChangeFeedContinuationTokenException) => CosmosExceptionFactory.CreateBadRequestException(
message: malformedChangeFeedContinuationTokenException.Message,
headers: new Headers(),
stackTrace: malformedChangeFeedContinuationTokenException.StackTrace,
innerException: malformedChangeFeedContinuationTokenException);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ public static async Task<PartitionedQueryExecutionInfo> GetQueryPlanWithServiceI

throw CosmosExceptionFactory.CreateBadRequestException(
message: tryGetQueryPlan.Exception.ToString(),
stackTrace: tryGetQueryPlan.Exception.StackTrace);
headers: new Headers(),
stackTrace: tryGetQueryPlan.Exception.StackTrace,
trace: trace);
j82w marked this conversation as resolved.
Show resolved Hide resolved
}

return tryGetQueryPlan.Result;
Expand Down
31 changes: 15 additions & 16 deletions Microsoft.Azure.Cosmos/src/Query/Core/QueryResponseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public static QueryResponseCore CreateFromException(Exception exception)
else
{
CosmosException unkownCosmosException = CosmosExceptionFactory.CreateInternalServerErrorException(
subStatusCode: default,
headers: new Headers()
{
SubStatusCode = SubStatusCodes.PartitionKeyRangeGone,
ActivityId = QueryResponseCore.EmptyGuidString
},
message: exception.Message,
stackTrace: exception.StackTrace,
activityId: QueryResponseCore.EmptyGuidString,
requestCharge: 0,
retryAfter: null,
headers: null,
trace: NoOpTrace.Singleton,
innerException: exception);

Expand Down Expand Up @@ -113,17 +113,13 @@ private static QueryResponseCore CreateFromExceptionWithStackTrace(ExceptionWith
statusCode: queryResponseCore.StatusCode,
subStatusCodes: queryResponseCore.SubStatusCode,
cosmosException: CosmosExceptionFactory.Create(
cosmosException.StatusCode,
cosmosException.SubStatusCode,
cosmosException.Message,
exceptionWithStackTrace.StackTrace,
cosmosException.ActivityId,
cosmosException.RequestCharge,
cosmosException.RetryAfter,
cosmosException.Headers,
cosmosException.Trace,
cosmosException.Error,
cosmosException.InnerException),
statusCode: cosmosException.StatusCode,
message: cosmosException.Message,
stackTrace: exceptionWithStackTrace.StackTrace,
headers: cosmosException.Headers,
trace: cosmosException.Trace,
error: cosmosException.Error,
innerException: cosmosException.InnerException),
requestCharge: queryResponseCore.RequestCharge,
activityId: queryResponseCore.ActivityId);

Expand All @@ -142,6 +138,7 @@ public override CosmosException Visit(MalformedContinuationTokenException malfor
{
return CosmosExceptionFactory.CreateBadRequestException(
message: malformedContinuationTokenException.Message,
headers: new Headers(),
stackTrace: malformedContinuationTokenException.StackTrace,
innerException: malformedContinuationTokenException);
}
Expand All @@ -150,13 +147,15 @@ public override CosmosException Visit(UnexpectedQueryPartitionProviderException
{
return CosmosExceptionFactory.CreateInternalServerErrorException(
message: $"{nameof(CosmosException)} due to {nameof(UnexpectedQueryPartitionProviderException)}",
headers: new Headers(),
innerException: unexpectedQueryPartitionProviderException);
}

public override CosmosException Visit(ExpectedQueryPartitionProviderException expectedQueryPartitionProviderException)
{
return CosmosExceptionFactory.CreateBadRequestException(
message: expectedQueryPartitionProviderException.Message,
headers: new Headers(),
stackTrace: expectedQueryPartitionProviderException.StackTrace,
innerException: expectedQueryPartitionProviderException);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ public async Task<IReadOnlyList<FeedRange>> GetFeedRangesAsync(
{
throw CosmosExceptionFactory.CreateInternalServerErrorException(
$"Container rid {containerRId} did not have a partition key range after refresh",
headers: new Headers(),
trace: trace);
}

Expand All @@ -299,6 +300,7 @@ public async Task<IReadOnlyList<FeedRange>> GetFeedRangesAsync(
{
throw CosmosExceptionFactory.CreateInternalServerErrorException(
$"Container rid {containerRId} returned partitionKeyRanges null after Container RID refresh",
headers: new Headers(),
trace: trace);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,20 @@ public class CosmosException : Exception
internal CosmosException(
HttpStatusCode statusCode,
string message,
int subStatusCode,
string stackTrace,
string activityId,
double requestCharge,
TimeSpan? retryAfter,
Headers headers,
ITrace trace,
Error error,
Exception innerException)
: base(CosmosException.GetMessageHelper(
statusCode,
subStatusCode,
message,
activityId), innerException)
headers,
message), innerException)
{
this.ResponseBody = message;
this.stackTrace = stackTrace;
this.ActivityId = activityId;
this.StatusCode = statusCode;
this.SubStatusCode = subStatusCode;
this.RetryAfter = retryAfter;
this.RequestCharge = requestCharge;
this.Headers = headers;
this.Headers = headers ?? new Headers();
this.Error = error;
this.Trace = trace;
}
Expand All @@ -65,12 +56,19 @@ public CosmosException(
: base(message)
{
this.stackTrace = null;
this.SubStatusCode = subStatusCode;
this.StatusCode = statusCode;
this.RequestCharge = requestCharge;
this.ActivityId = activityId;
this.Headers = new Headers();
this.ResponseBody = message;
this.Trace = NoOpTrace.Singleton;
this.Headers = new Headers()
{
SubStatusCode = (SubStatusCodes)subStatusCode,
RequestCharge = requestCharge,
};

if (!string.IsNullOrEmpty(activityId))
{
this.Headers.ActivityId = activityId;
}
}

/// <summary>
Expand All @@ -88,28 +86,28 @@ public CosmosException(
/// Gets the request completion sub status code from the Azure Cosmos DB service.
/// </summary>
/// <value>The request completion status code</value>
public virtual int SubStatusCode { get; }
public virtual int SubStatusCode => Headers.GetIntValueOrDefault(this.Headers.SubStatusCodeLiteral);

/// <summary>
/// Gets the request charge for this request from the Azure Cosmos DB service.
/// </summary>
/// <value>
/// The request charge measured in request units.
/// </value>
public virtual double RequestCharge { get; }
public virtual double RequestCharge => this.Headers.RequestCharge;

/// <summary>
/// Gets the activity ID for the request from the Azure Cosmos DB service.
/// </summary>
/// <value>
/// The activity ID for the request.
/// </value>
public virtual string ActivityId { get; }
public virtual string ActivityId => this.Headers.ActivityId;

/// <summary>
/// Gets the retry after time. This tells how long a request should wait before doing a retry.
/// </summary>
public virtual TimeSpan? RetryAfter { get; }
public virtual TimeSpan? RetryAfter => this.Headers.RetryAfter;

/// <summary>
/// Gets the response headers
Expand Down Expand Up @@ -184,28 +182,23 @@ internal ResponseMessage ToCosmosResponseMessage(RequestMessage request)
cosmosException: this,
statusCode: this.StatusCode,
trace: this.Trace);
if (this.SubStatusCode != 0)
{
responseMessage.Headers.SubStatusCode = (SubStatusCodes)this.SubStatusCode;
j82w marked this conversation as resolved.
Show resolved Hide resolved
}

return responseMessage;
}

private static string GetMessageHelper(
HttpStatusCode statusCode,
int subStatusCode,
string responseBody,
string activityId)
Headers headers,
string responseBody)
{
StringBuilder stringBuilder = new StringBuilder();

stringBuilder.Append($"Response status code does not indicate success: ");
stringBuilder.Append($"{statusCode} ({(int)statusCode})");
stringBuilder.Append("; Substatus: ");
stringBuilder.Append(subStatusCode);
stringBuilder.Append(headers?.SubStatusCodeLiteral ?? "0" );
stringBuilder.Append("; ActivityId: ");
stringBuilder.Append(activityId ?? string.Empty);
stringBuilder.Append(headers?.ActivityId ?? string.Empty);
stringBuilder.Append("; Reason: (");
stringBuilder.Append(responseBody ?? string.Empty);
stringBuilder.Append(");");
Expand Down
Loading