RequestFailedException
An exception thrown when service request fails.
using Azure.Core;
using Azure.Core.Pipeline;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
using System.Text.Json.Serialization;
namespace Azure
{
[Serializable]
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class RequestFailedException : Exception, ISerializable
{
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.Nullable(0)]
internal class ErrorResponse
{
[JsonPropertyName("error")]
public ResponseError Error { get; set; }
}
[System.Runtime.CompilerServices.Nullable(0)]
private readonly struct ErrorDetails
{
public string Message { get; }
[System.Runtime.CompilerServices.Nullable(2)]
[field: System.Runtime.CompilerServices.Nullable(2)]
public string ErrorCode {
[System.Runtime.CompilerServices.NullableContext(2)]
get;
}
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1
})]
[field: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1
})]
public IDictionary<string, string> Data {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1
})]
get;
}
public ErrorDetails(string message, [System.Runtime.CompilerServices.Nullable(2)] string errorCode, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1
})] IDictionary<string, string> data)
{
Message = message;
ErrorCode = errorCode;
Data = data;
}
}
private const string DefaultMessage = "Service request failed.";
internal const string NoContentOnSuccessMessage = "Service request succeeded. Response content and headers are not included to avoid logging sensitive data.";
[System.Runtime.CompilerServices.Nullable(2)]
private readonly Response _response;
public int Status { get; }
[System.Runtime.CompilerServices.Nullable(2)]
[field: System.Runtime.CompilerServices.Nullable(2)]
public string ErrorCode {
[System.Runtime.CompilerServices.NullableContext(2)]
get;
}
public RequestFailedException(string message)
: this(0, message)
{
}
public RequestFailedException(string message, [System.Runtime.CompilerServices.Nullable(2)] Exception innerException)
: this(0, message, innerException)
{
}
[EditorBrowsable(EditorBrowsableState.Never)]
public RequestFailedException(int status, string message)
: this(status, message, null)
{
}
[EditorBrowsable(EditorBrowsableState.Never)]
public RequestFailedException(int status, string message, [System.Runtime.CompilerServices.Nullable(2)] Exception innerException)
: this(status, message, null, innerException)
{
}
[System.Runtime.CompilerServices.NullableContext(2)]
[EditorBrowsable(EditorBrowsableState.Never)]
public RequestFailedException(int status, [System.Runtime.CompilerServices.Nullable(1)] string message, string errorCode, Exception innerException)
: base(message, innerException)
{
Status = status;
ErrorCode = errorCode;
}
private RequestFailedException(int status, [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
2
})] (string Message, ResponseError Error) details)
: this(status, details.Message, details.Error?.Code, null)
{
}
[System.Runtime.CompilerServices.NullableContext(2)]
private RequestFailedException(int status, ErrorDetails details, Exception innerException)
: this(status, details.Message, details.ErrorCode, innerException)
{
if (details.Data != null) {
foreach (KeyValuePair<string, string> datum in details.Data) {
Data.Add(datum.Key, datum.Value);
}
}
}
public RequestFailedException(Response response)
: this(response, null)
{
}
public RequestFailedException(Response response, [System.Runtime.CompilerServices.Nullable(2)] Exception innerException)
: this(response, innerException, null)
{
}
[System.Runtime.CompilerServices.NullableContext(2)]
public RequestFailedException([System.Runtime.CompilerServices.Nullable(1)] Response response, Exception innerException, RequestFailedDetailsParser detailsParser)
: this(response.Status, CreateExceptionDetails(response, detailsParser), innerException)
{
_response = response;
}
protected RequestFailedException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Status = info.GetInt32("Status");
ErrorCode = info.GetString("ErrorCode");
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
Argument.AssertNotNull(info, "info");
info.AddValue("Status", Status);
info.AddValue("ErrorCode", ErrorCode);
base.GetObjectData(info, context);
}
[System.Runtime.CompilerServices.NullableContext(2)]
public Response GetRawResponse()
{
return _response;
}
private static ErrorDetails CreateExceptionDetails(Response response, [System.Runtime.CompilerServices.Nullable(2)] RequestFailedDetailsParser parser)
{
BufferResponseIfNeeded(response);
if (parser == null)
parser = response.RequestFailedDetailsParser;
if (!(parser?.TryParse(response, out ResponseError error, out IDictionary<string, string> data) ?? DefaultRequestFailedDetailsParser.TryParseDetails(response, out error, out data))) {
error = null;
data = null;
}
StringBuilder stringBuilder = new StringBuilder();
AppendStatusAndReason(response, error, stringBuilder);
AppendErrorCodeAndAdditionalInfo(error, data, stringBuilder);
if (response.IsError)
AppendContentAndHeaders(response, stringBuilder);
else
stringBuilder.AppendLine().AppendLine("Service request succeeded. Response content and headers are not included to avoid logging sensitive data.");
return new ErrorDetails(stringBuilder.ToString(), error?.Code, data);
}
private static void AppendContentAndHeaders(Response response, StringBuilder messageBuilder)
{
ResponseHeaders headers;
if (response.ContentStream is MemoryStream) {
headers = response.Headers;
if (ContentTypeUtilities.TryGetTextEncoding(headers.ContentType, out Encoding _))
messageBuilder.AppendLine().AppendLine("Content:").AppendLine(((object)response.Content).ToString());
}
messageBuilder.AppendLine().AppendLine("Headers:");
headers = response.Headers;
foreach (HttpHeader item in headers) {
string str = response.Sanitizer.SanitizeHeader(item.Name, item.Value);
string value = item.Name + ": " + str;
messageBuilder.AppendLine(value);
}
}
private static void AppendErrorCodeAndAdditionalInfo([System.Runtime.CompilerServices.Nullable(2)] ResponseError error, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1
})] IDictionary<string, string> additionalInfo, StringBuilder messageBuilder)
{
if (!string.IsNullOrWhiteSpace(error?.Code))
messageBuilder.Append("ErrorCode: ").Append(error?.Code).AppendLine();
if (additionalInfo != null && additionalInfo.Count > 0) {
messageBuilder.AppendLine().AppendLine("Additional Information:");
foreach (KeyValuePair<string, string> item in additionalInfo) {
messageBuilder.Append(item.Key).Append(": ").AppendLine(item.Value);
}
}
}
private static void AppendStatusAndReason(Response response, [System.Runtime.CompilerServices.Nullable(2)] ResponseError error, StringBuilder messageBuilder)
{
messageBuilder.AppendLine(error?.Message ?? "Service request failed.").Append("Status: ").Append(response.Status.ToString(CultureInfo.InvariantCulture));
if (!string.IsNullOrEmpty(response.ReasonPhrase))
messageBuilder.Append(" (").Append(response.ReasonPhrase).AppendLine(")");
else
messageBuilder.AppendLine();
}
private static void BufferResponseIfNeeded(Response response)
{
Stream contentStream = response.ContentStream;
if ((contentStream != null && !(contentStream is MemoryStream)) || 1 == 0) {
MemoryStream memoryStream = new MemoryStream();
response.ContentStream.CopyTo(memoryStream);
response.ContentStream.Dispose();
memoryStream.Position = 0;
response.ContentStream = memoryStream;
}
}
}
}