/// <summary> /// Valides http response message and throws appropriate exceptions when errors have occured. /// E.g. a NfieldErrorException is thrown if the response contains a NfieldErrorCode. /// </summary> public static HttpResponseMessage ValidateResponse(this HttpResponseMessage response) { if (response.IsSuccessStatusCode) { return(response); } Dictionary <string, object> httpErrorDictionary = null; if (response.Content != null) { var contentString = response.Content.ReadAsStringAsync().Result; try { httpErrorDictionary = JsonConvert.DeserializeObject <Dictionary <string, object> >(contentString); } catch (Exception) { // it's not json } } object temp; if (httpErrorDictionary != null && httpErrorDictionary.TryGetValue("NfieldErrorCode", out temp)) { NfieldErrorCode nfieldErrorCode = (NfieldErrorCode)Enum.Parse(typeof(NfieldErrorCode), temp.ToString()); string message = httpErrorDictionary.TryGetValue("Message", out temp) ? temp.ToString() : response.ReasonPhrase; throw new NfieldErrorException(response.StatusCode, nfieldErrorCode, message); } throw new NfieldHttpResponseException(response.StatusCode, response.ReasonPhrase); }
public void TestValidateResponse_ServerReturnsNfieldErrorCodeAndMessage_ThrowsNfieldErrorExceptionWithErrorCodeAndMessage() { // Arrange const HttpStatusCode httpStatusCode = HttpStatusCode.NotFound; const NfieldErrorCode nfieldErrorCode = NfieldErrorCode.ArgumentIsNull; const string message = "Message #1"; var serverResponse = new HttpResponseMessage(httpStatusCode); var httpErrorMock = new Dictionary <string, object>() { { "NfieldErrorCode", nfieldErrorCode }, { "Message", message } }; HttpContent content = new ObjectContent <Dictionary <string, object> >(httpErrorMock, new JsonMediaTypeFormatter()); serverResponse.Content = content; // Act Func <object> actCode = () => serverResponse.ValidateResponse(); // Assert var ex = Assert.Throws <NfieldErrorException>(actCode); Assert.Equal(ex.HttpStatusCode, httpStatusCode); Assert.Equal(ex.NfieldErrorCode, nfieldErrorCode); Assert.Equal(ex.Message, message); }
/// <summary> /// Initializes a new instance of the <see cref="NfieldHttpResponseException"/> class with a http status code, a nfield error code and an error message. /// </summary> /// <param name="httpStatusCode">The http status code that was sent by the server during the error.</param> /// <param name="nfieldErrorCode">The error Nfield specific error code.</param> /// <param name="message">A message that describes the error.</param> public NfieldErrorException(HttpStatusCode httpStatusCode, NfieldErrorCode nfieldErrorCode, string message) : base(httpStatusCode, message) { NfieldErrorCode = nfieldErrorCode; }
/// <summary> /// Initializes a new instance of the <see cref="NfieldHttpResponseException"/> class with a http status code, a nfield error code and an error message. /// </summary> /// <param name="httpStatusCode">The http status code that was sent by the server during the error.</param> /// <param name="nfieldErrorCode">The error Nfield specific error code.</param> /// <param name="message">A message that describes the error.</param> public NfieldErrorException(HttpStatusCode httpStatusCode, NfieldErrorCode nfieldErrorCode, string message) : base(httpStatusCode, message) { NfieldErrorCode = nfieldErrorCode; }