Skip to content

Commit

Permalink
Fix NRE from GetExceptionCodeAsync (Azure#3304)
Browse files Browse the repository at this point in the history
* Add some null checks for GetExceptionCodeAsync

* pr comments
  • Loading branch information
brycewang-microsoft authored Apr 25, 2023
1 parent 0aff44e commit 2f317f8
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions common/src/service/ExceptionHandlingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,13 @@ public static Task<string> GetExceptionMessageAsync(HttpResponseMessage response
/// <returns>The fully-qualified error code, or the response status code, if no error code was provided.</returns>
public static async Task<ErrorCode> GetExceptionCodeAsync(HttpResponseMessage response)
{
string responseContentStr = "";
if (response.Content == null)
{
return ErrorCode.InvalidErrorCode;
}
// First we will attempt to retrieve the error code from the response content.
string responseContentStr = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
responseContentStr = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

// There are two things to consider when surfacing service errors to the user, the 6-digit error code and the code description. Ideally, when a backend service
// returns an error, both of these fields are set in the same place. However, IoT hub is returning the 6-digit code in the response content, while
Expand All @@ -120,7 +125,11 @@ public static async Task<ErrorCode> GetExceptionCodeAsync(HttpResponseMessage re

try
{
Dictionary<string, string> messageFields = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseContent.Message);
var messageFields = new Dictionary<string, string>();
if (responseContent?.Message != null)
{
messageFields = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseContent.Message);
}

if (messageFields != null
&& messageFields.TryGetValue(CommonConstants.ErrorCode, out string errorCodeObj))
Expand Down Expand Up @@ -157,7 +166,8 @@ public static async Task<ErrorCode> GetExceptionCodeAsync(HttpResponseMessage re
#endif
{
string[] errorCodeFields = messageField.Split(errorCodeDelimiter);
if (Enum.TryParse(errorCodeFields[1], out ErrorCode errorCode))
if (errorCodeFields.Length > 1
&& Enum.TryParse(errorCodeFields[1], out ErrorCode errorCode))
{
errorCodeValue = (int)errorCode;
}
Expand All @@ -184,8 +194,14 @@ public static async Task<ErrorCode> GetExceptionCodeAsync(HttpResponseMessage re
return ErrorCode.InvalidErrorCode;
}

// Now that we retrieved the integer error code from the response content, we will retrieve the error description from the header.
string headerErrorCodeString = response.Headers.GetFirstValueOrNull(CommonConstants.HttpErrorCodeName);
string headerErrorCodeString = null;

if (response.Headers != null)
{
// Now that we retrieved the integer error code from the response content, we will retrieve the error description from the header.
headerErrorCodeString = response.Headers.GetFirstValueOrNull(CommonConstants.HttpErrorCodeName);
}

if (headerErrorCodeString != null
&& Enum.TryParse(headerErrorCodeString, out ErrorCode headerErrorCode))
{
Expand Down

0 comments on commit 2f317f8

Please sign in to comment.