We have a owners module in our application and need to perform CRUD operations for some field in this module. For this we have created owner appservice. We are injecting IOwnerRepository and using the same for the CRUD operations. This IOwnerRepository is inheriting from IOwnerRepository: IRepository<Owner, int>
Below is the code snippet we are using for the insert and update. In this code we are wanting to use the CheckErrors function which logs the error using log4net as well as throws a user friendly message(Similar to what is used in the UserAppService)
public async Task CreateOrEdit(CreateOrEditOwnerDto input)
{
try
{
ValidateOwners(input);
if (input.Id == null)
{
var owner = ObjectMapper.Map<Owner>(input);
await _customOwnerRepository.InsertAsync(owner);
}
else
{
var updateOwner = _customOwnerRepository.Get(input.Id.GetValueOrDefault());
ObjectMapper.Map(input, updateOwner);
CurrentUnitOfWork.SaveChanges();
}
}
catch(Exception ex)
{
throw new UserFriendlyException(ex.Message);
}
}
unfortunately we are unable to use the CheckErrors function(as used in the UserAppService) cause the return type is not Task<IdentityResult>. Could you please let us know how can this be achieved in one shot without explicitly logging and throwing error.
Also, please let us know if we are correct on the below points:
- Exception handling is already implemented in the framework
- log4net is setup and we have a log4net configuration file
- if any error occurs in the backend, the same is automatically logged using log4net if the correct level is set in the config file
- There is not explicit logging code required
How can CheckErrors be implemented in all our AppServices
4 Answer(s)
-
0
Hi,
CheckErrors
implemented in AbpZeroTemplateAppServiceBase was only designed to check for error inIdentityResult
.Are you throw custom exception in
ValidateOwners()
? If so, you can directly throwUserFriendlyException
instead. -
0
Okay so you mean to say that CheckErrors cannot be used for methods whose return type is not IdentityResult and we will have to use UserFriendlyException to throw errors.
-
0
@sumitshah correct. You can directly throw
UserFriendlyException
inValidateOwners
method. -
0
This issue is closed because of no recent activity. Please open a new issue if you are still having this problem.