public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            if (actionExecutedContext.Exception is APIException)
            {
                APIException exceptionData = (APIException)actionExecutedContext.Exception;

                //The Response Message Set by the Action During Ececution
                var res = exceptionData.Message;

                //Define the Response Message

                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.Accepted)
                {
                    Content      = new StringContent(exceptionData.ToString()),
                    StatusCode   = HttpStatusCode.Accepted,
                    ReasonPhrase = res
                };

                response.Headers.Add("ValidationMessageType", ((byte)exceptionData.MessageType).ToString());

                //Create the Error Response

                actionExecutedContext.Response = response;

                string exception = actionExecutedContext.Exception.ToString();
                ErrorLogDL.InsertErrorLog(exception, actionExecutedContext.Exception.Message.ToString());
            }
            else
            {
                ///exception loging

                string exception = actionExecutedContext.Exception.ToString();
                ErrorLogDL.InsertErrorLog(exception, actionExecutedContext.Exception.Message.ToString());

                var res = actionExecutedContext.Exception.Message;
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content      = new StringContent(res),
                    StatusCode   = HttpStatusCode.InternalServerError,
                    ReasonPhrase = res
                };

                //Create the Error Response
                actionExecutedContext.Response = response;
            }
        }
示例#2
0
        private static HttpResponseMessage BuildApiResponse(HttpRequestMessage request, HttpResponseMessage response, bool isLicenseValid)
        {
            object content;
            ///string errorMessage = null;
            //HttpError error = null;
            int  ValidationMessageType = 1; // enum ValidationMessageType
            bool IsAPIException        = false;

            if (isLicenseValid == false)
            {
                ValidationMessageType = (int)KI.RIS.Enumerators.Common.ValidationMessageType.Blocking; // enum ValidationMessageType
                                                                                                       //  response = new HttpResponseMessage(HttpStatusCode.Accepted);
                response.StatusCode = HttpStatusCode.Accepted;
                content             = GlobalCaching.LicenseExpiredMessage;
                IsAPIException      = true;
                response.Headers.Add("ValidationMessageType", ((byte)KI.RIS.Enumerators.Common.ValidationMessageType.Blocking).ToString());
            }
            else
            {
                ValidationMessageType = 1; // enum ValidationMessageType
                                           //  Info = 1,
                                           //Blocking = 2,
                                           //Warning = 3,
                                           //YesNoCancel = 4,
                                           //OkCancel = 5


                IEnumerable <string> values = null;


                if (response.TryGetContentValue(out content)) //  && !response.IsSuccessStatusCode
                {
                    ///it will if the Controller handle the exact HttpStatusCodes eg: Resource -  public IHttpActionResult Delete(Int64 resourceMasterId, int categoryType)
                    if (content is APIException)
                    {
                        APIException objException = (APIException)content;
                        IsAPIException        = true;
                        ValidationMessageType = (int)objException.MessageType;
                        content = objException.Message;
                    }
                }

                if (response.StatusCode == HttpStatusCode.InternalServerError)
                {
                    content = MessageLib.Error; // "Please contact technical support team..............."; /// This message hardcode in client side
                                                //content = MessageLib.Error;
                }
                else if (!IsAPIException && response.StatusCode == HttpStatusCode.Accepted)
                {
                    content = response.ReasonPhrase;
                    HttpHeaders headers = response.Headers;
                    headers.TryGetValues("ValidationMessageType", out values);
                    ValidationMessageType = Convert.ToInt16(((string[])values)[0]);
                }
            }
            var newResponse = request.CreateResponse(response.StatusCode
                                                     , new ApiResponse(response.StatusCode, content, ValidationMessageType, response.RequestMessage, response));

            foreach (var header in response.Headers)
            {
                newResponse.Headers.Add(header.Key, header.Value);
            }



            //if (error != null)
            //{
            //    newResponse.Headers.Add("ExceptionType", error.ExceptionType);
            //    newResponse.Headers.Add("ExceptionMessage", error.ExceptionMessage);
            //}

            return(newResponse);
        }