My login function look like below:-
[Function("Loginxyz")]
public async Task<IActionResult> GenerateToken(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "user/login")] HttpRequestData req,
ILogger log)
{
try
{
if (requestData.BarCode != null)
{
var localLoginRequest = JsonConvert.DeserializeObject<xyz>(requestBody);
token = await _tokenService.GenerateToken(localLoginRequest);
}
if (token == null)
return new UnauthorizedResult();
return new OkObjectResult(new { access_token = token });
}
catch (AggregateException ae)
{
// Handle multiple exceptions
foreach (var innerException in ae.InnerExceptions)
{
log.LogError(innerException, "An error occurred");
// Handle the inner exception based on its type
}
// Return a specific error response based on the aggregate exception
return new BadRequestObjectResult("An error occurred");
}
catch (AuthenticationServiceException ex)
{
// Log the error with detailed message
log.LogError($"Error during token generation: {ex.Message}", ex);
// Create a custom error response object
var errorResponse = new
{
Error = ex.Message,
StatusCode = StatusCodes.Status400BadRequest
};
// Return the error response with the appropriate status code
return new JsonResult(errorResponse) { StatusCode = StatusCodes.Status400BadRequest };
}
catch (LoginConflictException ex)
{
// Log the error with detailed message
log.LogError($"Error during token generation: {ex.Message}", ex);
// Create a custom error response object
var errorResponse = new
{
Error = ex.Message,
StatusCode = StatusCodes.Status409Conflict
};
// Return the error response with the appropriate status code
return new JsonResult(errorResponse) { StatusCode = StatusCodes.Status409Conflict };
}
catch (Exception ex)
{
log.LogError($"Error during token generation: {ex.Message}");
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
}
}
My service class look like:-
public async Task<TokenResponseDto> GenerateToken(object tokenRequest)
{
try
{
return await tokenGenerator.GenerateToken(tokenRequest);
}
catch (Exception ex)
{
throw; // Rethrow the exception
}
}
I throw an error in the tokenGenerator.GenerateToken method and rethrow it in my service class. My expectation is that when I rethrow the error in the service class, it should be caught in my Azure Function.
However, the error I’m receiving from the rethrow is:
“An exception was thrown by the invocation.
Exception: System.ArgumentNullException: Value cannot be null. (Parameter ‘logger’)”
This is confusing because I was throwing a custom error, not related to ArgumentNullException.
can someone help me on this to understand?