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

Fix NRE from GetExceptionCodeAsync #3304

Merged
merged 2 commits into from
Apr 25, 2023
Merged
Changes from 1 commit
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
31 changes: 25 additions & 6 deletions common/src/service/ExceptionHandlingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,16 @@ 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)
{
// First we will attempt to retrieve the error code from the response content.
string responseContentStr = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
string responseContentStr = "";
if (response.Content != null)
{
// First we will attempt to retrieve the error code from the response content.
responseContentStr = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
else
{
return ErrorCode.InvalidErrorCode;
}
brycewang-microsoft marked this conversation as resolved.
Show resolved Hide resolved

// 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 +128,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 != null && responseContent.Message != null)
brycewang-microsoft marked this conversation as resolved.
Show resolved Hide resolved
brycewang-microsoft marked this conversation as resolved.
Show resolved Hide resolved
{
messageFields = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseContent.Message);
}

if (messageFields != null
brycewang-microsoft marked this conversation as resolved.
Show resolved Hide resolved
&& messageFields.TryGetValue(CommonConstants.ErrorCode, out string errorCodeObj))
Expand Down Expand Up @@ -157,7 +169,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 +197,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