public void Delete_DbErrorHandled() { // Arrange var taskRepoMock = new Mock <IRepository <Task> >(); var controller = new HomeController(taskRepoMock.Object); var testTask = new Task { Id = 1, ParentId = 2, Name = "a1", Children = new List <Task>() }; Task callbackTask = null; taskRepoMock.Setup(r => r.GetByID(1)).Returns(testTask); taskRepoMock.Setup(r => r.Delete(It.IsAny <Task>())).Callback((Task task) => { callbackTask = task; }); var ex = new Exception("The DELETE statement conflicted with the SAME TABLE REFERENCE constraint some other text"); var dbEx = new System.Data.Entity.Infrastructure.DbUpdateException("", new Exception("", ex)); taskRepoMock.Setup(r => r.Save()).Throws(dbEx); // Act var rez = controller.Delete(1); // Assert taskRepoMock.VerifyAll(); callbackTask.ShouldBe(testTask); rez.ShouldBeOfType <ViewResult>(); var modelTemp = ((ViewResult)rez).Model; modelTemp.ShouldNotBeNull(); modelTemp.ShouldBeOfType <TaskDeleteModel>(); var model = (TaskDeleteModel)modelTemp; model.Id.ShouldBe(1); model.ParentId.ShouldBe(2); model.Message.ShouldBe("Can not delete this task, because it contains other tasks."); }
public void Edit_Post_ErrorNotHandledText() { // Arrange var taskRepoMock = new Mock <IRepository <Task> >(); var controller = new HomeController(taskRepoMock.Object); var inModel = new TaskEditModel { Id = 1, Name = "a1" }; var callbackTask = new Task { Id = 1, ParentId = 2, Name = "test" }; taskRepoMock.Setup(r => r.GetByID(1)).Returns(callbackTask); var ex = new Exception("Wrong exception message"); var dbEx = new System.Data.Entity.Infrastructure.DbUpdateException("", new Exception("", ex)); taskRepoMock.Setup(r => r.Save()).Throws(dbEx); // Act Should.Throw <System.Data.Entity.Infrastructure.DbUpdateException>(() => { controller.Edit(inModel); }); // Assert taskRepoMock.VerifyAll(); callbackTask.ShouldNotBeNull(); callbackTask.Name.ShouldBe("a1"); controller.ModelState.Count.ShouldBe(0); }
public void Create_Post_ErrorNotHandledText() { // Arrange var taskRepoMock = new Mock <IRepository <Task> >(); var controller = new HomeController(taskRepoMock.Object); var inModel = new TaskCreateModel { ParentId = 1, Name = "a1" }; Task callbackTask = null; taskRepoMock.Setup(r => r.Insert(It.IsAny <Task>())).Callback((Task t) => { callbackTask = t; }); var ex = new Exception("Wrong exception message"); var dbEx = new System.Data.Entity.Infrastructure.DbUpdateException("", new Exception("", ex)); taskRepoMock.Setup(r => r.Save()).Throws(dbEx); // Act Should.Throw <System.Data.Entity.Infrastructure.DbUpdateException>(() => { controller.Create(inModel); }); // Assert taskRepoMock.VerifyAll(); callbackTask.ShouldNotBeNull(); callbackTask.ParentId.ShouldBe(1); callbackTask.Name.ShouldBe("a1"); controller.ModelState.Count.ShouldBe(0); }
public static void DbUpdateException(System.Data.Entity.Infrastructure.DbUpdateException DbUpdate) { string InnerEx = DbUpdate.InnerException.InnerException.ToString(); if (InnerEx.Contains("Cannot insert duplicate key row in object") || InnerEx.Contains("Violation of UNIQUE KEY constraint")) { MessageBox.Show(ExcepcionesMensajes.NombresRepetidos, "Repetido", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { manejaExcepcion(new Exception()); } }
public static DataOperationException ToDataOperationException(this System.Data.Entity.Infrastructure.DbUpdateException dbUpdateException) { DataOperationException result = null; Exception innerMostException = dbUpdateException.InnerMostException(); var sqlException = innerMostException as SqlException; //if (innerMostException is SqlException) if (innerMostException.GetType().IsAssignableFrom(typeof(SqlException))) if (sqlException != null) { result = sqlException.ToDataOperationException(); } else { result = new DataOperationException("Unknown Data Error", dbUpdateException); } return(result); }
static public Errors TryDecodeDbUpdateException(System.Data.Entity.Infrastructure.DbUpdateException ex) { if (ex.InnerException is System.Data.Entity.Core.UpdateException) { if (ex.InnerException.InnerException != null && ex.InnerException.InnerException is System.Data.SqlClient.SqlException exception) { foreach (System.Data.SqlClient.SqlError sqlError in exception.Errors) { return((Errors)sqlError.Number); } } return(Errors.Unknown); } else { return(Errors.Unknown); } }
// event handler for delegating protected void Button2_Click(object sender, EventArgs e) { try { string name = TextBox.Text; string dept = getmethoddepartment(); int id = BusinessLogic.getemployeeid(name); //to get the employee id based on the name BusinessLogic.updatetemporaryhead(id, dept); //to update the temporary head Label3.Visible = true; Label4.Visible = true; TextBox2.Visible = true; var k = BusinessLogic.gettemporaryheadname(dept); // to get the temporary head name TextBox2.Text = k.ToString(); Button3.Visible = true; } catch (System.Configuration.Provider.ProviderException ex) { System.Configuration.Provider.ProviderException ee = ex; ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('The selected employee has already been delegated')", true); //Response.Write("<br/><br/>Exception:<br/>" + ee); } catch (System.Data.SqlClient.SqlException ex) { System.Data.SqlClient.SqlException ee = ex; ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please ensure you have selected the correct data.')", true); //Response.Write("<br/><br/>Exception:<br/>" + ee); } catch (System.Data.Entity.Infrastructure.DbUpdateException ex) { System.Data.Entity.Infrastructure.DbUpdateException ee = ex; ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please ensure you have selected an employee to delegate')", true); } catch (Exception ex) { Exception ee = ex; ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Oops ! Something went wrong. Please try again.')", true); } }
public void Edit_Post_ErrorHandled() { // Arrange var taskRepoMock = new Mock <IRepository <Task> >(); var controller = new HomeController(taskRepoMock.Object); var inModel = new TaskEditModel { Id = 1, Name = "a1" }; var callbackTask = new Task { Id = 1, ParentId = 2, Name = "test" }; taskRepoMock.Setup(r => r.GetByID(1)).Returns(callbackTask); var ex = new Exception("Cannot insert duplicate key row in object 'dbo.Task' with unique index 'IX_ParentId_Name' some other text"); var dbEx = new System.Data.Entity.Infrastructure.DbUpdateException("", new Exception("", ex)); taskRepoMock.Setup(r => r.Save()).Throws(dbEx); // Act var rez = controller.Edit(inModel); // Assert taskRepoMock.VerifyAll(); callbackTask.ShouldNotBeNull(); callbackTask.Name.ShouldBe("a1"); controller.ModelState.Count.ShouldBe(1); controller.ModelState[""].Errors.Single().ErrorMessage.ShouldBe("Task name should be unique on this nesting level"); rez.ShouldBeOfType <ViewResult>(); var modelTemp = ((ViewResult)rez).Model; modelTemp.ShouldNotBeNull(); modelTemp.ShouldBeOfType <TaskEditModel>(); var model = (TaskEditModel)modelTemp; model.Id.ShouldBe(1); model.ParentId.ShouldBe(2); model.Name.ShouldBe("a1"); }
public void Create_Post_ErrorHandled() { // Arrange var taskRepoMock = new Mock <IRepository <Task> >(); var controller = new HomeController(taskRepoMock.Object); var inModel = new TaskCreateModel { ParentId = 1, Name = "a1" }; Task callbackTask = null; taskRepoMock.Setup(r => r.Insert(It.IsAny <Task>())).Callback((Task t) => { callbackTask = t; }); var ex = new Exception("Cannot insert duplicate key row in object 'dbo.Task' with unique index 'IX_ParentId_Name'"); var dbEx = new System.Data.Entity.Infrastructure.DbUpdateException("", new Exception("", ex)); taskRepoMock.Setup(r => r.Save()).Throws(dbEx); // Act var rez = controller.Create(inModel); // Assert taskRepoMock.VerifyAll(); callbackTask.ShouldNotBeNull(); callbackTask.ParentId.ShouldBe(1); callbackTask.Name.ShouldBe("a1"); controller.ModelState.Count.ShouldBe(1); controller.ModelState[""].Errors.Single().ErrorMessage.ShouldBe("Task name should be unique on this nesting level"); rez.ShouldBeOfType <ViewResult>(); var modelTemp = ((ViewResult)rez).Model; modelTemp.ShouldNotBeNull(); modelTemp.ShouldBeOfType <TaskCreateModel>(); var model = (TaskCreateModel)modelTemp; model.ParentId.ShouldBe(1); model.Name.ShouldBe("a1"); }
public void Delete_DbErrorNotHandledText() { // Arrange var taskRepoMock = new Mock <IRepository <Task> >(); var controller = new HomeController(taskRepoMock.Object); var testTask = new Task { Id = 1, ParentId = 2, Name = "a1", Children = new List <Task>() }; Task callbackTask = null; taskRepoMock.Setup(r => r.GetByID(1)).Returns(testTask); taskRepoMock.Setup(r => r.Delete(It.IsAny <Task>())).Callback((Task task) => { callbackTask = task; }); var ex = new Exception("Wrong exception message"); var dbEx = new System.Data.Entity.Infrastructure.DbUpdateException("", new Exception("", ex)); taskRepoMock.Setup(r => r.Save()).Throws(dbEx); // Act Should.Throw <System.Data.Entity.Infrastructure.DbUpdateException>(() => { controller.Delete(1); }); // Assert taskRepoMock.VerifyAll(); callbackTask.ShouldBe(testTask); }
public TUser UserSave(string loginKey, ref ErrorInfo err, TUser inEnt, IList <string> allPar) { if (!GlobalFun.UserCheckFunctioAuthority(loginKey, ref err, MethodBase.GetCurrentMethod())) { return(null); } using (DBEntities db = new DBEntities()) { try { var idPar = allPar.SingleOrDefault(x => x == "ID"); if (idPar != null) { allPar.Remove(idPar); } var userEnt = db.fa_user.SingleOrDefault(a => a.ID == inEnt.ID); if (userEnt == null) { userEnt = db.fa_user.SingleOrDefault(x => x.LOGIN_NAME == inEnt.LOGIN_NAME); } var loginEnt = db.fa_login.SingleOrDefault(x => x.LOGIN_NAME == inEnt.LOGIN_NAME); bool isAdd = false; if (userEnt == null) { isAdd = true; userEnt = Fun.ClassToCopy <ProInterface.Models.TUser, fa_user>(inEnt); if (userEnt.ID == 0) { userEnt.ID = Fun.GetSeqID <fa_user>(); } userEnt.CREATE_TIME = DateTime.Now; userEnt.LOGIN_COUNT = 0; userEnt.REGION = db.fa_district.SingleOrDefault(p => p.ID == inEnt.DISTRICT_ID).REGION; } else { if (db.fa_user.Where(x => x.LOGIN_NAME == inEnt.LOGIN_NAME && x.ID != userEnt.ID).Count() > 0) { err.IsError = true; err.Message = "登录工号已经存在"; return(null); } userEnt = Fun.ClassToCopy <ProInterface.Models.TUser, fa_user>(inEnt, userEnt, allPar); } if (loginEnt == null) { loginEnt = Fun.ClassToCopy <TUser, fa_login>(inEnt); loginEnt.ID = Fun.GetSeqID <fa_login>(); loginEnt.PASSWORD = AppSet.DefaultPwd.Md5(); loginEnt.REGION = userEnt.REGION; db.fa_login.Add(loginEnt); } else { if (db.fa_login.Where(x => x.LOGIN_NAME == loginEnt.LOGIN_NAME && x.ID != loginEnt.ID).Count() > 0) { err.IsError = true; err.Message = "登录工号已经存在"; return(null); } loginEnt = Fun.ClassToCopy <TUser, fa_login>(inEnt, loginEnt, allPar); loginEnt.PHONE_NO = inEnt.PHONE_NO; } if (allPar.Contains("RoleAllID")) { IList <int> allRoleId = new List <int>(); allRoleId = inEnt.RoleAllID.Split(',').Where(x => !string.IsNullOrEmpty(x)).Select(x => Convert.ToInt32(x)).ToList(); userEnt.fa_role.Clear(); userEnt.fa_role = db.fa_role.Where(x => allRoleId.Contains(x.ID)).ToList(); } if (isAdd) { db.fa_user.Add(userEnt); } userEnt.fa_district1.Clear(); if (!string.IsNullOrEmpty(inEnt.UserDistrict)) { var disArrList = inEnt.UserDistrict.Split(',').Select(x => Convert.ToInt32(x)).ToList(); userEnt.fa_district1 = db.fa_district.Where(x => disArrList.Contains(x.ID)).ToList(); } db.SaveChanges(); inEnt = Fun.ClassToCopy <fa_user, TUser>(userEnt, inEnt); inEnt.ID = userEnt.ID; GlobalFun.UserWriteLog(loginKey, MethodBase.GetCurrentMethod(), StatusType.UserLogType.Edit); return(inEnt); } catch (Exception e) { err.IsError = true; if (e.Message.IndexOf("EntityValidationErrors") > -1) { err.Message = Fun.GetDbEntityErrMess(e as DbEntityValidationException); } else { System.Data.Entity.Infrastructure.DbUpdateException t = e as System.Data.Entity.Infrastructure.DbUpdateException; err.Message = t.Message; } return(null); } } }