Base solution for your next web application
Open Closed

Using IdentityResult.Failed Error Handling #1642


User avatar
0
northgale created

Hi all, I have this line of validation registered on my localization xml

<text name="Identity.DuplicateName" value="Name {0} is already taken." />

and this is my code for creating a new user

public async Task CreateAsync(CreateGuestInput input)
        {
            var guest = input.MapTo<Guest>();
            CheckErrors(await _GuestManager.CreateAsync(guest));
            Logger.Info("Create a Guest with id: " + guest.Id);
        }

When creating, it will await The GuestManager

public async Task<IdentityResult> CreateAsync(Guest input)
        {
            IdentityResult _return= new IdentityResult();
            try
            {
                
                var temp = _guestRepository.GetAll().Where(i => i.IdentityNumber == input.IdentityNumber).FirstOrDefault();
                if (temp == null)
                {
                    
                    await _guestRepository.InsertAsync(input);
                    _return = IdentityResult.Success;
                }
                else
                {
                     _return = IdentityResult.Failed("Identity.DuplicateName");
                }
            }
            catch(Exception ex)
            {
                _return = new IdentityResult(ex.Message);
            }
            return _return;
        }

But I didn't get the Error Message "Name {0} is already taken" but instead i got "An unknown failure has occured.". Can anyone help me with this error handling?


5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Your code seems correct to me.

    Can you check the value of customErrors on web.config ? Can you also check if this error message is logged to Logs.txt under App_Data or Logs folder (changes according to ABP version) in your Web project ?

  • User Avatar
    0
    northgale created

    Hi ismcagdas,

    Thankyou, i've checked the webconfig and found out that my custom error is off, i turned it on but the error message still not behave as I want it to.

    Now it shows "Identity.DuplicateName" as an error message to user and not "Name {0} is already taken.", still struggling to make this works.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Can you share the CheckErrors method of yours ?

  • User Avatar
    0
    northgale created

    Sure, Here it is

    protected virtual void CheckErrors(IdentityResult identityResult)
    {
            identityResult.CheckErrors(LocalizationManager);
    }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Sorry I couldn't came up with an idea about your problem. If you can share your project, I can take a look on it.