/// <summary> /// 处理Api行为执行过程中产生的异常 /// </summary> /// <param name="actionContext">上下文</param> /// <param name="actionfilters">过滤器</param> /// <param name="exception">异常项</param> private void ProcessExecutingException(ActionContext actionContext, IEnumerable<IFilter> actionfilters, Exception exception) { var exceptionContext = new ExceptionContext(actionContext, new ApiExecuteException(actionContext, exception)); this.ExecExceptionFilters(actionfilters, exceptionContext); if (exceptionContext.ExceptionHandled == false) { throw exception; } var result = exceptionContext.Result == null ? new ErrorResult { Status = 500, Errors = exception.Message } : exceptionContext.Result; result.ExecuteResult(actionContext); }
/// <summary> /// 异常触发 /// </summary> /// <param name="filterContext">上下文</param> protected virtual void OnException(ExceptionContext filterContext) { }
/// <summary> /// 执行异常过滤器 /// </summary> /// <param name="actionFilters">Api行为过滤器</param> /// <param name="exceptionContext">上下文</param> private void ExecExceptionFilters(IEnumerable<IFilter> actionFilters, ExceptionContext exceptionContext) { foreach (var filter in this.Server.GlobalFilter.ExceptionFilters) { filter.OnException(exceptionContext); if (exceptionContext.ExceptionHandled) return; } ((IExceptionFilter)this).OnException(exceptionContext); if (exceptionContext.ExceptionHandled) return; foreach (var filter in actionFilters) { var exceptionFilter = filter as IExceptionFilter; if (exceptionFilter != null) { exceptionFilter.OnException(exceptionContext); if (exceptionContext.ExceptionHandled) return; } } }
protected override void OnException(NetworkSocket.Http.ExceptionContext filterContext) { Console.WriteLine(filterContext.Exception); base.OnException(filterContext); }
/// <summary> /// 异常触发 /// </summary> /// <param name="filterContext">上下文</param> void IExceptionFilter.OnException(ExceptionContext filterContext) { this.OnException(filterContext); }
/// <summary> /// 执行全局异常过滤器 /// </summary> /// <param name="exceptionContext">上下文</param> private void ExecGlobalExceptionFilters(ExceptionContext exceptionContext) { if (this.GlobalFilters.Count == 0) { return; } foreach (IFilter filter in this.GlobalFilters) { filter.OnException(exceptionContext); if (exceptionContext.ExceptionHandled == true) break; } if (exceptionContext.ExceptionHandled == false) { throw exceptionContext.Exception; } }
/// <summary> /// 收到Http请求 /// </summary> /// <param name="request">请求对象</param> /// <param name="response">回复对象</param> protected override void OnHttpRequest(HttpRequest request, HttpResponse response) { var route = request.Url.AbsolutePath; var action = this.httpActionList.TryGet(route); var requestContext = new RequestContext(request, response); if (action == null) { var exception = new ApiNotExistException(route); var exceptionContext = new ExceptionContext(requestContext, exception); foreach (var filter in this.GlobalFilter.ExceptionFilters) { filter.OnException(exceptionContext); if (exceptionContext.ExceptionHandled == true) break; } var result = exceptionContext.Result == null ? new ErrorResult { Status = 404, Errors = exception.Message } : exceptionContext.Result; result.ExecuteResult(requestContext); } else { var controller = this.DependencyResolver.GetService(action.DeclaringService) as HttpController; var actionContext = new ActionContext(requestContext, action); controller.Server = this; ((IHttpController)controller).Execute(actionContext); } }
/// <summary> /// 异常时 /// </summary> /// <param name="session">产生异常的会话</param> /// <param name="exception">异常</param> protected sealed override void OnException(ISession session, Exception exception) { var response = new HttpResponse(session); var requestContext = new RequestContext(null, response); var exceptionConext = new ExceptionContext(requestContext, exception); this.ExecGlobalExceptionFilters(exceptionConext); if (exceptionConext.Result != null) { exceptionConext.Result.ExecuteResult(requestContext); } else { base.OnException(session, exception); } }
/// <summary> /// 处理Http异常 /// </summary> /// <param name="ex">Http异常</param> /// <param name="context">请求上下文</param> private void ProcessHttpException(HttpException ex, RequestContext context) { var exceptionContent = new ExceptionContext(context, ex); this.ExecGlobalExceptionFilters(exceptionContent); var result = exceptionContent.Result ?? new ErrorResult(ex); result.ExecuteResult(context); }
/// <summary> /// 执行异常过滤器 /// </summary> /// <param name="filters">Api行为过滤器</param> /// <param name="exceptionContext">上下文</param> private void ExecExceptionFilters(IEnumerable<IFilter> filters, ExceptionContext exceptionContext) { var totalFilters = this.GetTotalFilters(filters); foreach (var filter in totalFilters) { filter.OnException(exceptionContext); if (exceptionContext.ExceptionHandled == true) break; } }
public void OnException(ExceptionContext filterContext) { filterContext.ExceptionHandled = true; filterContext.Result = new ContentResult("异常处理完成 .."); }
/// <summary> /// 处理http请求 /// </summary> /// <param name="requestContext">请求上下文</param> private void ProcessRequest(RequestContext requestContext) { var route = requestContext.Request.Url.AbsolutePath; var action = this.httpActionList.TryGet(requestContext.Request); if (action == null) { var exception = new ApiNotExistException(route); var exceptionContext = new ExceptionContext(requestContext, exception); foreach (var filter in this.GlobalFilter.ExceptionFilters) { filter.OnException(exceptionContext); if (exceptionContext.ExceptionHandled == true) break; } var result = exceptionContext.Result != null ? exceptionContext.Result : new ErrorResult { Status = 404, Errors = exception.Message }; result.ExecuteResult(requestContext); } else { var controller = this.DependencyResolver.GetService(action.DeclaringService) as HttpController; var actionContext = new ActionContext(requestContext, action); controller.Server = this; ((IHttpController)controller).Execute(actionContext); // 释放资源 this.DependencyResolver.TerminateService(controller); } }
/// <summary> /// 处理找不到Action /// </summary> /// <param name="route">路由</param> /// <param name="requestContext">上下文</param> private void ProcessActionNotFound(string route, RequestContext requestContext) { var exception = new ApiNotExistException(route); var exceptionContext = new ExceptionContext(requestContext, exception); foreach (var filter in this.GlobalFilter.ExceptionFilters) { filter.OnException(exceptionContext); if (exceptionContext.ExceptionHandled == true) break; } var result = exceptionContext.Result != null ? exceptionContext.Result : new ErrorResult { Status = 404, Errors = exception.Message }; result.ExecuteResult(requestContext); }
/// <summary> /// 处理Api行为执行过程中产生的异常 /// </summary> /// <param name="actionContext">上下文</param> /// <param name="actionfilters">过滤器</param> /// <param name="exception">异常项</param> private void ProcessExecutingException(ActionContext actionContext, IEnumerable<IFilter> actionfilters, Exception exception) { var exceptionContext = new ExceptionContext(actionContext, new ApiExecuteException(exception)); this.ExecAllExceptionFilters(actionfilters, exceptionContext); var result = exceptionContext.Result; if (result == null) { result = new ErrorResult { Status = 500, Errors = exception.Message }; } result.ExecuteResult(actionContext); }
protected override void OnException(ExceptionContext filterContext) { filterContext.ExceptionHandled = true; }
protected override void OnException(ExceptionContext filterContext) { filterContext.ExceptionHandled = true; filterContext.Result = new ContentResult("异常处理完成 .."); }