Example #1
0
        private static HttpResponseMessage BuildApiReponse(HttpRequestMessage request, HttpResponseMessage response)
        {
            object content;
            string errorMessage = null;

            if (response.TryGetContentValue(out content) && !response.IsSuccessStatusCode)
            {
                HttpError error = content as HttpError;
                if (error != null)
                {
                    content = null;
                    var log = log4net.LogManager.GetLogger("API");
                    foreach (var key in error)
                    {
                        log.Error(key.Key + ":" + key.Value.ToStringSafe());
                    }
                    errorMessage = error.ExceptionMessage;

                    if (String.IsNullOrEmpty(errorMessage))
                    {
                        errorMessage = error.Message;
                    }

                    errorMessage += GetInnerErrorMsg(error);
                    log.Error(errorMessage);
                }
            }

            if (response.Content is StreamContent)
            {
                return(response);
            }
            Type contentType = content != null?content.GetType() : null;

            if (contentType != null && contentType.IsGenericType && contentType.GetGenericTypeDefinition() == typeof(JsonResultDataEntity <>))
            {
                return(response);
            }
            var wrapperData = new JsonResultDataEntity <Object>();

            wrapperData.IsAuthorized = response.StatusCode != System.Net.HttpStatusCode.Unauthorized;
            wrapperData.Code         = ((int)response.StatusCode >= 200 && (int)response.StatusCode <= 300) ? 0 : 1;
            wrapperData.Message      = errorMessage;
            wrapperData.Data         = content;
            var newResponse = request.CreateResponse(System.Net.HttpStatusCode.OK, wrapperData);

            foreach (var header in response.Headers)
            {
                newResponse.Headers.Add(header.Key, header.Value);
            }
            return(newResponse);
        }
        public void OnException(ExceptionContext filterContext)
        {
            if (filterContext.ExceptionHandled || filterContext.HttpContext.IsCustomErrorEnabled)
            {
                return;
            }

            if (IsAjax(filterContext))
            {
                JsonResultDataEntity <string> result = new JsonResultDataEntity <string>();
                result.Message = filterContext.Exception.Message;
                result.Code    = 1;
                filterContext.ExceptionHandled = true;
                filterContext.Result           = new JsonResult()
                {
                    Data = result, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
            else
            {
                string controllerName = (String)filterContext.RouteData.Values["controller"];
                string actionName     = (String)filterContext.RouteData.Values["action"];
                System.Web.Mvc.HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
                filterContext.ExceptionHandled = true;
                filterContext.Result           = new ViewResult()
                {
                    ViewName = "Error", ViewData = new ViewDataDictionary(model), TempData = filterContext.Controller.TempData
                };
            }


            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();
            //filterContext.HttpContext.Response.StatusCode = 500;
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            string errorMessages = "";
            var    ex            = filterContext.Exception;

            while (ex != null)
            {
                errorMessages += ex.Message + ex.StackTrace;
                ex             = ex.InnerException;
            }
            log4net.LogManager.GetLogger("Error").Error(errorMessages);
        }
Example #3
0
        public ActionResult SaveForm(DealModelViewModel vm)
        {
            JsonResultDataEntity <string> result = new JsonResultDataEntity <string>();

            if (vm.Notes.NoteList != null)
            {
                vm.Notes.NoteList = vm.Notes.NoteList.OrderBy(n => n.PaymentOrdinal).ToList();
            }
            var tuple = SaveModel(vm);

            if (tuple.Item1)
            {
                result.Code = 0;
            }
            else
            {
                result.Code    = 1;
                result.Message = tuple.Item2;
            }

            return(Json(result));
        }