Ejemplo n.º 1
0
        /// <summary>
        /// Encapsulates the error response into a WebApiClientException
        /// </summary>
        /// <param name="response">The response message containing the error</param>
        private WebApiClientException GetException(HttpResponseMessage response)
        {
            WebApiClientExceptionDetails details = new WebApiClientExceptionDetails();

            //Gets the content string where exception details are defined
            string content = response.Content.ReadAsStringAsync().Result;

            try
            {
                Dictionary <string, IList <string> > modelState = null;

                //Parses the content string into a json object
                var json = JObject.Parse(content);

                //If ModelState is informed, get it
                IDictionary <string, JToken> jModelState = (JObject)json["ModelState"];

                //Transforms the ModelState into a Dictionary
                if (jModelState != null)
                {
                    modelState = ((JObject)json["ModelState"]).ToObject <Dictionary <string, IList <string> > >();
                }

                string message          = (string)json["Message"];
                string exceptionMessage = (string)json["ExceptionMessage"];

                //Creates the details object setting its values
                details.Message       = exceptionMessage ?? message;
                details.ExceptionType = (string)json["ExceptionType"];
                details.StackTrace    = (string)json["StackTrace"];
                details.ModelState    = modelState;
            }
            catch
            {
                if (!string.IsNullOrEmpty(content))
                {
                    //Content isn't in json format but still contains useful information
                    details.Message = content;
                }
            }

            //Sets the reason phrase
            details.Reason = response.ReasonPhrase;

            //Return the exception
            return(new WebApiClientException(response.StatusCode, details));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Creates a new object of WebApiClientException
 /// </summary>
 /// <param name="statusCode">The error code</param>
 /// <param name="details">The object with further exception details</param>
 public WebApiClientException(HttpStatusCode statusCode, WebApiClientExceptionDetails details) : base("Error when trying to call the WebApi. See Details for more information")
 {
     this.StatusCode = statusCode;
     this.Details    = details;
 }