private RestClientResponse <object> GetResponseObject(RestClientResponse response, Type responseType) { try { if (!response.IsSuccessStatusCode && response.Exception != null) { throw response.Exception; } Core.Log.LibVerbose("Deserializing response byte array to an object type."); return((response.ValueInBytes?.Length > 0) ? new RestClientResponse <object>(response, Serializer.Deserialize(response.ValueInBytes, responseType)) : new RestClientResponse <object>(response, null)); } catch (Exception ex) { Core.Log.Write(ex); if (response.IsSuccessStatusCode) { throw; } return(new RestClientResponse <object>(response, null)); } }
private async Task <RestClientResponse> HandleResponseMessageAsync(HttpResponseMessage response) { Core.Log.LibVerbose("Reading the response data from: {0}", response.RequestMessage.RequestUri); var respObj = new RestClientResponse { IsSuccessStatusCode = response.IsSuccessStatusCode, ReasonPhrase = response.ReasonPhrase, StatusCode = response.StatusCode, RequestUri = response.RequestMessage.RequestUri, ValueInBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false) }; if (!respObj.IsSuccessStatusCode) { SerializableHttpError serHttpError = null; if (respObj.ValueInBytes?.Length > 0) { if (Serializer.MimeTypes.Contains(response.Content.Headers.ContentType.MediaType)) { try { serHttpError = (SerializableHttpError)Serializer.Deserialize(respObj.ValueInBytes, typeof(SerializableHttpError)); } catch (Exception ex) { Core.Log.Write(ex); } } else if (response.Content.Headers.ContentType.MediaType == "text/plain") { serHttpError = new SerializableHttpError { Message = Encoding.UTF8.GetString(respObj.ValueInBytes)?.RemoveInvalidXmlChars() }; serHttpError.ExceptionMessage = serHttpError.Message; } } SerializableException sEx = null; if (serHttpError != null) { sEx = new SerializableException { Message = serHttpError.Message?.RemoveInvalidXmlChars(), StackTrace = serHttpError.StackTrace, ExceptionType = serHttpError.ExceptionType }; } var responseText = (respObj.ValueInBytes?.Length > 0) ? Encoding.UTF8.GetString(respObj.ValueInBytes)?.RemoveInvalidXmlChars() : string.Empty; var rce = new RestClientException(responseText, sEx?.GetException()) { RequestUri = respObj.RequestUri, StatusCode = respObj.StatusCode, ReasonPhrase = respObj.ReasonPhrase, ResponseBytes = respObj.ValueInBytes, ResponseText = responseText, ServerException = serHttpError }; respObj.Exception = rce; } Core.Log.LibVerbose("Object response created."); return(respObj); }