Exemplo n.º 1
0
        protected override void DoProcess(HttpContextBase context)
        {
            Log.Debug("strating request. context: '{0}'", ApiContext);

            //Neeeded to rollback errors
            context.Response.Buffer       = true;
            context.Response.BufferOutput = true;

            try
            {
                Log.Debug("method invoke");
                ApiResponce.Count      = ApiContext.Count;
                ApiResponce.StartIndex = ApiContext.StartIndex;

                if (Method != null)
                {
                    var responce = ApiManager.InvokeMethod(Method, ApiContext);
                    if (responce is Exception)
                    {
                        SetError(context, (Exception)responce, HttpStatusCode.InternalServerError);
                    }
                    else
                    {
                        // success
                        PostProcessResponse(context, responce);
                    }
                }
                else
                {
                    SetError(context, new MissingMethodException("Method not found"), HttpStatusCode.NotFound);
                }
            }
            catch (TargetInvocationException targetInvocationException)
            {
                if (targetInvocationException.InnerException is ItemNotFoundException)
                {
                    SetError(context, targetInvocationException.InnerException, HttpStatusCode.NotFound, "The record could not be found");
                }
                else if (targetInvocationException.InnerException is ArgumentException)
                {
                    SetError(context, targetInvocationException.InnerException, HttpStatusCode.BadRequest, "Invalid arguments");
                }
                else
                {
                    SetError(context, targetInvocationException.InnerException, HttpStatusCode.InternalServerError);
                }
            }
            catch (Exception e)
            {
                SetError(context, e, HttpStatusCode.InternalServerError);
            }

            Exception responseError;

            try
            {
                RespondTo(Method, context);
                return;
            }
            catch (ThreadAbortException e)
            {
                //Do nothing. someone killing response
                Log.Error(e, "thread aborted. response not sent");
                return;
            }
            catch (HttpException exception)
            {
                responseError = exception;
                SetError(context, exception, (HttpStatusCode)exception.GetHttpCode());//Set the code of throwed exception
            }
            catch (Exception exception)
            {
                responseError = exception;
                SetError(context, exception, HttpStatusCode.InternalServerError);
            }
            Log.Error(responseError, "error happened while sending response. can't be here");
            RespondTo(Method, context);//If we got there then something went wrong
        }
Exemplo n.º 2
0
        protected override void DoProcess(HttpContextBase context)
        {
            Log.DebugFormat("strating request. context: '{0}'", ApiContext);

            //Neeeded to rollback errors
            context.Response.Buffer       = true;
            context.Response.BufferOutput = true;

            IApiEntryPoint instance = null;

            try
            {
                Log.Debug("method invoke");
                ApiResponce.Count      = ApiContext.Count;
                ApiResponce.StartIndex = ApiContext.StartIndex;

                if (Method != null)
                {
                    if (!string.IsNullOrEmpty(Method.Name))
                    {
                        instance = Container.ResolveNamed <IApiEntryPoint>(Method.Name, new TypedParameter(typeof(ApiContext), ApiContext));
                    }
                    else
                    {
                        instance = Container.Resolve <IApiEntryPoint>();
                    }

                    var responce = ApiManager.InvokeMethod(Method, ApiContext, instance);
                    if (responce is Exception)
                    {
                        SetError(context, (Exception)responce, HttpStatusCode.InternalServerError);
                    }
                    else
                    {
                        // success
                        PostProcessResponse(context, responce);
                    }
                }
                else
                {
                    SetError(context, new MissingMethodException("Method not found"), HttpStatusCode.NotFound);
                }
            }
            catch (TargetInvocationException targetInvocationException)
            {
                if (targetInvocationException.InnerException is ItemNotFoundException)
                {
                    SetError(context, targetInvocationException.InnerException, HttpStatusCode.NotFound, "The record could not be found");
                }
                else if (targetInvocationException.InnerException is ArgumentException)
                {
                    SetError(context, targetInvocationException.InnerException, HttpStatusCode.BadRequest, "Invalid arguments");
                }
                else if (targetInvocationException.InnerException is SecurityException)
                {
                    SetError(context, targetInvocationException.InnerException, HttpStatusCode.Forbidden, "Access denied");
                }
                else if (targetInvocationException.InnerException is InvalidOperationException)
                {
                    SetError(context, targetInvocationException.InnerException, HttpStatusCode.Forbidden);
                }
                else
                {
                    SetError(context, targetInvocationException.InnerException, HttpStatusCode.InternalServerError);
                }
            }
            catch (ApiArgumentMismatchException e)
            {
                SetError(context, e, HttpStatusCode.BadRequest, "Invalid arguments");
            }
            catch (Exception e)
            {
                SetError(context, e, HttpStatusCode.InternalServerError);
            }

            Exception responseError;

            try
            {
                RespondTo(Method, context);
                return;
            }
            catch (ThreadAbortException e)
            {
                //Do nothing. someone killing response
                Log.Error("thread aborted. response not sent", e);
                return;
            }
            catch (HttpException exception)
            {
                responseError = exception;
                SetError(context, exception, (HttpStatusCode)exception.GetHttpCode());//Set the code of throwed exception
            }
            catch (Exception exception)
            {
                responseError = exception;
                SetError(context, exception, HttpStatusCode.InternalServerError);
            }
            Log.Error("error happened while sending response. can't be here", responseError);
            RespondTo(Method, context);//If we got there then something went wrong
        }