示例#1
0
        /// <summary>
        /// Converts the provided context and exception to a custom serializable response object.
        /// </summary>
        /// <param name="context">The context where the error occured.</param>
        /// <param name="ex">The exception that occured.</param>
        /// <returns>A custom serializable response object.</returns>
        private BadResponse GetResponse(HttpContext context, Exception ex)
        {
            var response = new BadResponse
            {
                Error = new Error
                {
                    Code    = context.Response.StatusCode,
                    Message = ex.Message
                }
            };

            if (ex.InnerException == null)
            {
                return(response);
            }

            IList <AdditionalError> errors = new List <AdditionalError>();
            Exception inner = ex.InnerException;

            while (inner != null)
            {
                errors.Add(new AdditionalError
                {
                    Domain  = context.Request.Path,
                    Message = inner.Message,
                    Reason  = inner.GetType().Name
                });

                inner = inner.InnerException;
            }

            response.Error.Errors = errors.ToArray();
            return(response);
        }
示例#2
0
 /// <summary>
 /// Serializes the provided serializable response object.
 /// </summary>
 /// <param name="response">The serializable response object.</param>
 /// <returns>A serialized response as a string.</returns>
 private string GetResponseBody(BadResponse response) =>
 JsonConvert.SerializeObject(response, new JsonSerializerSettings
 {
     ContractResolver  = new CamelCasePropertyNamesContractResolver(),
     NullValueHandling = NullValueHandling.Ignore
 });