/// <summary> /// 处理进行Ajax请求时产生的异常 /// </summary> /// <param name="filterContext">异常上下文对象</param> protected override void HandleAjaxException(ExceptionContext filterContext) { if (filterContext.Exception is MessageException) { LogUtil.Warn(filterContext.Exception.ToString()); ResponseUtil.ResponseTextHtml("{\"status\":0,\"data\":\"" + filterContext.Exception.Message + "\"}"); } else if (filterContext.Exception is AuthorizationException) { ResponseUtil.ResponseTextHtml("{\"status\":3,\"data\":\"/User/Login\"}"); } else if (filterContext.Exception is BinderException) { LogUtil.Info(filterContext.Exception.ToString()); BinderException be = (BinderException)filterContext.Exception; if (be.Code == "0") { ResponseUtil.ResponseTextHtml("<script>parent.P.Set.TempCallBack('{\"status\":0,\"data\":\"" + filterContext.Exception.Message + "\"}');</script>"); } } else { LogUtil.Error(filterContext.Exception.ToString()); ResponseUtil.ResponseTextHtml("{\"status\":4,\"data\":\"" + filterContext.Exception.Message + "\"}"); } filterContext.Result = null; filterContext.ExceptionHandled = true; filterContext.HttpContext.Response.StatusCode = 200; filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; }
public void TestRuntimeExceptionUnqualifiedValueException() { string jsonString = "[{\"Bind\":\"strange.unittests.ISimpleInterface\",\"To\":\"SimpleInterfaceImplementer\"}]"; TestDelegate testDelegate = delegate { binder.ConsumeBindings(jsonString); }; BinderException ex = Assert.Throws <BinderException>(testDelegate); //Because we haven't fully qualified the value Assert.AreEqual(BinderExceptionType.RUNTIME_NULL_VALUE, ex.type); }
public void TestRuntimeUnknownTypeException() { string jsonString = "[{\"Bind\":true,\"To\":\"Solo\"}]"; TestDelegate testDelegate = delegate { binder.ConsumeBindings(jsonString); }; BinderException ex = Assert.Throws <BinderException>(testDelegate); //Because we can't bind a boolean as a key Assert.AreEqual(BinderExceptionType.RUNTIME_TYPE_UNKNOWN, ex.type); }
public void TestRuntimeTooManyKeysException() { string jsonString = "[{\"Bind\":[\"Han\",\"Leia\"],\"To\":\"Solo\"}]"; TestDelegate testDelegate = delegate { binder.ConsumeBindings(jsonString); }; BinderException ex = Assert.Throws <BinderException>(testDelegate); //Because we have two keys in a Binder that only supports one Assert.AreEqual(BinderExceptionType.RUNTIME_TOO_MANY_KEYS, ex.type); }
public void TestRuntimeNoBindException() { string jsonString = "[{\"oops\":\"Han\",\"To\":\"Solo\"}]"; TestDelegate testDelegate = delegate { binder.ConsumeBindings(jsonString); }; BinderException ex = Assert.Throws <BinderException>(testDelegate); //Because we have no bind key Assert.AreEqual(BinderExceptionType.RUNTIME_NO_BIND, ex.type); }
public void TestRuntimeExceptionTooManyValues() { string jsonString = "[{\"Bind\":\"strange.unittests.ISimpleInterface\",\"To\":[\"strange.unittests.SimpleInterfaceImplementer\",\"strange.unittests.PolymorphicClass\"]}]"; TestDelegate testDelegate = delegate { binder.ConsumeBindings(jsonString); }; BinderException ex = Assert.Throws <BinderException>(testDelegate); //Because we have two values in a Binder that only supports one Assert.AreEqual(BinderExceptionType.RUNTIME_TOO_MANY_VALUES, ex.type); }
public void TestThrowsErrorOnUnresolvedAbstraction() { string jsonString = "[{\"Bind\":\"strange.unittests.TestView2\",\"To\":\"strange.unittests.TestMediator\",\"ToAbstraction\":\"TestView\"}]"; TestDelegate testDelegate = delegate { mediationBinder.ConsumeBindings(jsonString); }; BinderException ex = Assert.Throws <BinderException>(testDelegate); //Because the Abstraction value isn't fully qualified Assert.AreEqual(BinderExceptionType.RUNTIME_NULL_VALUE, ex.type); }
public void TestConflictWithoutWeak() { binder.Bind <ISimpleInterface>().To <SimpleInterfaceImplementer>(); TestDelegate testDelegate = delegate { binder.Bind <ISimpleInterface>().To <SimpleInterfaceImplementerTwo>(); object instance = binder.GetBinding <ISimpleInterface>().value; Assert.IsNotNull(instance); }; BinderException ex = Assert.Throws <BinderException>(testDelegate); //Because we have a conflict between the two above bindings Assert.AreEqual(BinderExceptionType.CONFLICT_IN_BINDER, ex.type); }
public void TestRuntimeFailedWhitelistException() { string jsonString = "[{\"Bind\":\"Han\",\"To\":\"Solo\"}, {\"Bind\":\"Darth\",\"To\":\"Vader\"}]"; System.Collections.Generic.List <object> whitelist = new System.Collections.Generic.List <object> (); whitelist.Add("Solo"); binder.WhitelistBindings(whitelist); TestDelegate testDelegate = delegate { binder.ConsumeBindings(jsonString); }; BinderException ex = Assert.Throws <BinderException>(testDelegate); //Because Vader is not whitelisted Assert.AreEqual(BinderExceptionType.RUNTIME_FAILED_WHITELIST_CHECK, ex.type); }
public void TestRuntimeUnqualifiedCommandException() { string jsonInjectorString = "[{\"Bind\":\"strange.unittests.ISimpleInterface\",\"To\":\"strange.unittests.SimpleInterfaceImplementer\", \"Options\":\"ToSingleton\"}]"; injectionBinder.ConsumeBindings(jsonInjectorString); string jsonCommandString = "[{\"Bind\":\"TestEvent\",\"To\":\"CommandWithInjection\"}]"; TestDelegate testDelegate = delegate { commandBinder.ConsumeBindings(jsonCommandString); }; BinderException ex = Assert.Throws <BinderException> (testDelegate); Assert.AreEqual(ex.type, BinderExceptionType.RUNTIME_NULL_VALUE); }
/// <summary> /// 处理非Ajax请求时产生的异常 /// </summary> /// <param name="filterContext">异常上下文对象</param> protected override void HandleException(ExceptionContext filterContext) { // 下面是临时演示,以后需按异常类型完善 if (filterContext.Exception is MessageException) { LogUtil.Warn(filterContext.Exception.ToString()); //Response.Write("<div style=\"padding-top:100px; margin:0 auto; text-align:center;\">"); //Response.Write("抱歉,发生了错误,参考消息:<span style='color:red;'>" + filterContext.Exception.Message + "</span>"); //Response.Write("<br /><br /><a href=\"javascript:history.back();\">单击此处返回</a>"); //Response.Write("</div>"); } else if (filterContext.Exception is AuthorizationException) { // Response.Redirect("/User/Login", true); Response.Redirect("/Apply/ApplyPrompt", true); } else if (filterContext.Exception is BinderException) { LogUtil.Info(filterContext.Exception.ToString()); BinderException be = (BinderException)filterContext.Exception; if (be.Code == "0") { ResponseUtil.ResponseTextHtml("<script>parent.P.Set.TempCallBack('{\"status\":0,\"data\":\"" + filterContext.Exception.Message + "\"}');</script>"); } } else { //此处日志由父类记录,避免重复记录 //LogUtil.Error(filterContext.Exception.ToString()); // 注要:如果想通过url传递任何参数,一定要防止注入攻击 base.HandleException(filterContext); } ViewBag.Message = filterContext.Exception.Message; ViewBag.StackTrace = filterContext.Exception.StackTrace; filterContext.Result = View("Error"); filterContext.ExceptionHandled = true; filterContext.HttpContext.Response.StatusCode = 500; filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; }