private async Task ThrowIfResponseNotSuccessfulAsync(HttpResponseMessage response) { if (!response.IsSuccessStatusCode) { ManagementServiceError managementServiceError = null; try { managementServiceError = await response.Content.ReadAsAsync <ManagementServiceError>(); } catch (InvalidOperationException) { // ReadAsAsync will synchronously throw InvalidOperationException when there is no default formatter for the ContentType. // We will treat these cases as an unknown error } string errorCode = UnknownErrorCode; string errorMessage = Resources.UnknownErrorMessage; List <ErrorDetail> errorDetails = null; if (managementServiceError != null) { errorCode = managementServiceError.Code; errorMessage = managementServiceError.Message; errorDetails = managementServiceError.Details; } throw new ManagementClientException(response.StatusCode, errorCode, errorMessage, errorDetails); } }
private async Task ThrowIfResponseNotSuccessfulAsync(HttpResponseMessage response) { if (!response.IsSuccessStatusCode) { //Do not treat not found as exception, but rather as null result if (response.StatusCode == HttpStatusCode.NotFound) { return; } if (response.StatusCode == HttpStatusCode.Unauthorized) { throw new UnauthorizedAccessException(); } ManagementServiceError managementServiceError = null; try { managementServiceError = await response.Content.ReadAsAsync <ManagementServiceError>(); } catch (InvalidOperationException) { // ReadAsAsync will synchronously throw InvalidOperationException when there is no default formatter for the ContentType. // We will treat these cases as an unknown error } catch (UnsupportedMediaTypeException) // can't parse the message, create it manually { } var errorCode = UnknownErrorCode; var errorMessage = "An unknown error has occurred during this operation"; List <ErrorDetail> errorDetails = null; if (managementServiceError != null) { errorCode = managementServiceError.Code; errorMessage = managementServiceError.Message + " " + managementServiceError.ExceptionMessage; errorDetails = managementServiceError.Details; } else { errorMessage = response.Content.ReadAsStringAsync().Result ?? errorMessage; } throw new ManagementClientException(response.StatusCode, errorCode, errorMessage, errorDetails); } }