Skip to content

Commit

Permalink
fix: update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Harish-osmosys committed Mar 22, 2024
1 parent 7b1f65a commit 80c7bf4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,27 +195,36 @@ public async Task<ActionResult<BaseResponse<int>>> ConfirmEmailAsync(ConfirmEmai
BaseResponse<int> response = new(ResponseStatus.Fail);
try
{
bool isValidEmail = await _userValidator.ValidEmailId(request.Email).ConfigureAwait(false);
if (!isValidEmail)
{
ModelState.AddModelError("email", "Error Finding User");
throw new UserNotFoundException(ErrorValues.GenricNotFoundMessage);
}
await _authenticationService.ConfirmEmailAsync(request).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

return Ok(response);
}
catch (NotFoundException ex)
catch (UserNotFoundException ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
response.Errors = GetErrorResponse();
return BadRequest(response);
}
catch (InvalidTokenException ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
response.Errors = GetErrorResponse();
return BadRequest(response);
}
catch (Exception ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
response.Errors = GetErrorResponse();
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,14 @@ public async Task<bool> AddUserRoleAsync(string email, Roles role)

public async Task<string> GetConfirmationToken(string Id)
{
IdentityApplicationUser? user = await _userManager.FindByIdAsync(Id).ConfigureAwait(false) ?? throw new NotFoundException("Error finding user");
return await _userManager.GenerateEmailConfirmationTokenAsync(user).ConfigureAwait(false);
IdentityApplicationUser? user = await _userManager.FindByIdAsync(Id).ConfigureAwait(false);
return await _userManager.GenerateEmailConfirmationTokenAsync(user!).ConfigureAwait(false);
}

public async Task ConfirmEmailAsync(string email, string token)
{
IdentityApplicationUser user = await _userManager.FindByEmailAsync(email).ConfigureAwait(false) ?? throw new NotFoundException("Error finding user");
IdentityResult result = await _userManager.ConfirmEmailAsync(user, token).ConfigureAwait(false);
IdentityApplicationUser? user = await _userManager.FindByEmailAsync(email).ConfigureAwait(false) ;
IdentityResult result = await _userManager.ConfirmEmailAsync(user!, token).ConfigureAwait(false);
if (!result.Succeeded)
{
throw new InvalidTokenException($"Invalid token");
Expand Down

0 comments on commit 80c7bf4

Please sign in to comment.