public override void Handle(ExceptionHandlerContext context)
        {
            var resolver  = context.Request.GetUriProcessResolver();
            var suContext = context.Request.GetServiceUnitContext();

            if (resolver == null || suContext == null)
            {
                base.Handle(context);
                return;
            }

            WriteErrorLog(suContext, context);

            var response = new ServiceUnitResponse(HttpStatusCode.InternalServerError)
            {
                Data = context.Exception
            };

            foreach (var handler in resolver.HandleErrorPipeline)
            {
                response = handler.Invoke(suContext, response);
            }

            context.Result = new ResponseMessageResult(context.Request.CreateResponse(response.StatusCode, response.Data));
        }
Exemplo n.º 2
0
        private void BuildHandleErrorPipeLine()
        {
            HandleErrorPipeline = new List <Func <ServiceUnitContext, ServiceUnitResponse, ServiceUnitResponse> >();


            HandleErrorPipeline.Add((suContext, response) => {
                Exception exception = response.Data as Exception;

                HttpStatusCode statusCode = HttpStatusCode.InternalServerError;

                if ((exception is DbUpdateConcurrencyException) || (exception.InnerException is DbUpdateConcurrencyException))
                {
                    statusCode = HttpStatusCode.Conflict;
                }
                else if (exception is DbEntityValidationException)
                {
                    statusCode = HttpStatusCode.BadRequest;
                }

                WebApiErrorResponse apiError    = WebApiErrorResponse.Create(exception);
                ServiceUnitResponse newResponse = new ServiceUnitResponse(statusCode)
                {
                    Data = apiError
                };

                return(newResponse);
            });
        }
        public ServiceUnitResponse InvokeAction(ServiceUnitContext suContext, ActionDefinition action)
        {
            var response = new ServiceUnitResponse(HttpStatusCode.OK);

            response.Data = action.Method.Invoke(action.Instance, new object[0]);

            return(response);
        }
Exemplo n.º 4
0
 private ServiceUnitResponse HandleError(ServiceUnitContext context,
                                         IUriProcessResolver resolver,
                                         ServiceUnitResponse response)
 {
     if (resolver != null)
     {
         foreach (var handler in resolver.HandleErrorPipeline)
         {
             handler.Invoke(context, response);
         }
     }
     return(response);
 }
        private void BuildHandleErrorPipeLine()
        {
            HandleErrorPipeline = new List <Func <ServiceUnitContext, ServiceUnitResponse, ServiceUnitResponse> >();


            HandleErrorPipeline.Add((suContext, response) => {
                Exception exception = response.Data as Exception;

                //401 or 403
                if (response.StatusCode == HttpStatusCode.Forbidden || response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    response.Data = new WebApiErrorResponse()
                    {
                        ErrorType   = WebApiErrorTypes.AuthorizationError,
                        Message     = Resources.RoleAuthorizedErrorMessage,
                        Description = string.Empty
                    };
                    return(response);
                }

                HttpStatusCode statusCode = HttpStatusCode.InternalServerError;

                if ((exception is DbUpdateConcurrencyException) || (exception.InnerException is DbUpdateConcurrencyException))
                {
                    statusCode = HttpStatusCode.Conflict;
                }
                else if (exception is DbEntityValidationException)
                {
                    statusCode = HttpStatusCode.BadRequest;
                }

                WebApiErrorResponse apiError    = WebApiErrorResponse.Create(exception);
                ServiceUnitResponse newResponse = new ServiceUnitResponse(statusCode)
                {
                    Data = apiError
                };

                return(newResponse);
            });
        }
Exemplo n.º 6
0
        private ServiceUnitResponse ExecuteInternal(ServiceUnitContext suContext, IUriProcessResolver resolver)
        {
            var executionType            = resolver.GetExecutionType(suContext);
            ServiceUnitResponse response = null;

            if (executionType == null)
            {
                response = new ServiceUnitResponse(HttpStatusCode.NotFound);
                HandleError(suContext, resolver, response);
                return(response);
            }

            var instance = resolver.CreateInstance(suContext, executionType);

            if (instance == null)
            {
                response = new ServiceUnitResponse(HttpStatusCode.NotFound);
                HandleError(suContext, resolver, response);
                return(response);
            }

            var action = resolver.GetAction(suContext, instance);

            if (action == null)
            {
                response = new ServiceUnitResponse(HttpStatusCode.NotFound);
                HandleError(suContext, resolver, response);
                return(response);
            }

            try
            {
                IRoutingAuthorization auth = suContext.ServiceContainer.GetService <IRoutingAuthorization>();
                var authResult             = auth.Authorize();
                if (!authResult.IsAuthorized)
                {
                    response = new ServiceUnitResponse(authResult.Status);
                    HandleError(suContext, resolver, response);
                    return(response);
                }

                response = resolver.InvokeAction(suContext, action);
                if (response == null)
                {
                    return(new ServiceUnitResponse(HttpStatusCode.NotFound));
                }

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    HandleError(suContext, resolver, response);
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(suContext, ex);
                return(HandleError(suContext, resolver, new ServiceUnitResponse(HttpStatusCode.InternalServerError)
                {
                    Data = ex
                }));
            }

            return(response);
        }