Base solution for your next web application
Open Closed

Use CheckErrors to log and throw user friendly message #8267


User avatar
0
sumitshah created

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&lt;Owner&gt;(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:

  1. Exception handling is already implemented in the framework
  2. log4net is setup and we have a log4net configuration file
  3. if any error occurs in the backend, the same is automatically logged using log4net if the correct level is set in the config file
  4. There is not explicit logging code required

How can CheckErrors be implemented in all our AppServices


4 Answer(s)
  • User Avatar
    0
    ryancyq created
    Support Team

    Hi, CheckErrors implemented in AbpZeroTemplateAppServiceBase was only designed to check for error in IdentityResult.

    Are you throw custom exception in ValidateOwners()? If so, you can directly throw UserFriendlyException instead.

  • User Avatar
    0
    sumitshah created

    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.

  • User Avatar
    0
    ismcagdas created
    Support Team

    @sumitshah correct. You can directly throw UserFriendlyException in ValidateOwners method.

  • User Avatar
    0
    ismcagdas created
    Support Team

    This issue is closed because of no recent activity. Please open a new issue if you are still having this problem.