The following code is not caught by the try block:
var data = new MS_Officer
{
officerName = input.officerName,
email = input.email,
handphone = input.handphone,
fax = input.fax,
isActive = input.isActive
};
try
{
_msOfficerRepo.Insert(data);
} catch (DbEntityValidationException ex)
{
// Retrieve the error messages as a list of strings.
var errorMessages = ex.EntityValidationErrors
.SelectMany(x => x.ValidationErrors)
.Select(x => x.ErrorMessage);
// Join the list to a single string.
var fullErrorMessage = string.Join("; ", errorMessages);
// Combine the original exception message with the new one.
var exceptionMessage = string.Concat("Validation errors: ", fullErrorMessage);
// Throw a new DbEntityValidationException with the improved exception message.
throw new UserFriendlyException(exceptionMessage);
} catch (Exception ex)
{
throw new UserFriendlyException("Error: " + ex.Message);
}
But in my log file I see this line:
WARN 2017-11-02 15:32:01,493 [5 ] nHandling.AbpApiExceptionFilterAttribute - There are 1 validation errors:
WARN 2017-11-02 15:32:01,493 [5 ] nHandling.AbpApiExceptionFilterAttribute - There are 1 validation errors:
WARN 2017-11-02 15:32:01,493 [5 ] nHandling.AbpApiExceptionFilterAttribute - (input.positionID)
ERROR 2017-11-02 15:32:27,466 [13 ] I.Engin3.EntityFramework.Engin3DbContext - There are some validation errors while saving changes in EntityFramework:
ERROR 2017-11-02 15:32:27,467 [13 ] I.Engin3.EntityFramework.Engin3DbContext - - officerName: The field officerName must be a string with a maximum length of 25.
ERROR 2017-11-02 15:32:27,970 [13 ] nHandling.AbpApiExceptionFilterAttribute - Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at Abp.EntityFramework.AbpDbContext.SaveChanges()
at Abp.EntityFramework.Uow.EfUnitOfWork.SaveChangesInDbContext(DbContext dbContext)
at Abp.EntityFramework.Uow.EfUnitOfWork.SaveChanges()
at Abp.EntityFramework.Uow.EfUnitOfWork.CompleteUow()
at Abp.Domain.Uow.UnitOfWorkBase.Complete()
....
Why?
Abp version 2.3.0
.Net Framework
Hi,
If you are executing this code in ApplicationService it will be transactional by default.
Abp uses interceptors to start transaction before calling your method and commits transaction at the end. So actual save happens outside of your try/catch block.
You can use CurrentUnitOfWork property to SaveChanges() whenever you want, or you can inject IUnitOfWork manager to start new transaction explicitly
Please check UnitOfWork documentation - Documentation
Also in your case if you wish to show userfirendly error message you can try capture all input errors by attributing your input dto then Abp will automatically throw ValidationException which is handled on client side.
Hope that helps :)
Thanks for your response, @Krystian-D . I tried to create a validation check for unique column in a table, how could I do it without try/catch? I don't think I should use it inside a dto.
@dyhaz I think you can just use your repository to do check before trying to insert like _msOfficerRepo.GetAll().Any(x=> x.ColumnToCheck == input.CorrespondingValue) and based on that you can show appropriate message. Also I think its more explicit what you trying to capture because you can not be sure that DbEntityValidationException wont return sensitive information.
Most helpful comment
Hi,
If you are executing this code in ApplicationService it will be transactional by default.
Abp uses interceptors to start transaction before calling your method and commits transaction at the end. So actual save happens outside of your try/catch block.
You can use CurrentUnitOfWork property to SaveChanges() whenever you want, or you can inject IUnitOfWork manager to start new transaction explicitly
Please check UnitOfWork documentation - Documentation
Also in your case if you wish to show userfirendly error message you can try capture all input errors by attributing your input dto then Abp will automatically throw ValidationException which is handled on client side.
Hope that helps :)