Improve error handling and logging for mail and OTP endpoints #7

Merged
rajukottedi merged 1 commits from feature/portfolio-admin-auth into dev 2026-02-16 11:54:21 +05:30
3 changed files with 6 additions and 6 deletions
Showing only changes of commit 4b968099ad - Show all commits

View File

@ -59,7 +59,7 @@ namespace PortBlog.API.Controllers
catch (Exception ex)
{
logger.LogCritical(ex, "Exception while sending OTP for {ToEmail}.", messageSendDto.ToEmail);
return StatusCode(500, "A problem happened while handling your request.");
return StatusCode(500, ex.Message);
}
}

View File

@ -262,8 +262,8 @@ namespace PortBlog.API.Controllers
}
catch (Exception ex)
{
_logger.LogCritical($"Exception while sending message from {message.Name}.", ex);
return StatusCode(500, "A problem happened while handling your request.");
_logger.LogCritical(ex, "Exception while sending message from {Name}.", message.Name);
return StatusCode(500, ex.Message);
}
}
}

View File

@ -67,14 +67,14 @@ namespace PortBlog.API.Services
}
catch (Exception ex)
{
// Log full exception and persist failed message. Do not rethrow to avoid causing a 500 from OTP generation
// Log full exception and persist failed message.
logger.LogCritical(ex, "Exception while sending mail from {FromEmail} ({Name}).", messageSendDto.FromEmail, messageSendDto.Name);
messageSendDto.SentStatus = (int)MailConstants.MailStatus.Failed;
mailRepository.AddMessage(messageEntity);
await mailRepository.SaveChangesAsync();
// swallow the exception so OTP generation continues; caller can inspect delivery status via message logs
return;
// Throw a descriptive exception so callers can return an error message to the client.
throw new InvalidOperationException($"Failed to send email to '{messageSendDto.ToEmail}'. See inner exception for details.", ex);
}
}
}