Improve error handling and logging for mail and OTP endpoints

Updated AuthController and CvController to return exception messages in 500 responses and enhanced logging. MailService now throws descriptive exceptions on email send failures, allowing upstream error reporting and preserving failed messages.
This commit is contained in:
Bangara Raju Kottedi 2026-02-16 11:44:31 +05:30
parent 2d603e4fc6
commit 4b968099ad
3 changed files with 6 additions and 6 deletions

View File

@ -59,7 +59,7 @@ namespace PortBlog.API.Controllers
catch (Exception ex) catch (Exception ex)
{ {
logger.LogCritical(ex, "Exception while sending OTP for {ToEmail}.", messageSendDto.ToEmail); 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) catch (Exception ex)
{ {
_logger.LogCritical($"Exception while sending message from {message.Name}.", ex); _logger.LogCritical(ex, "Exception while sending message from {Name}.", message.Name);
return StatusCode(500, "A problem happened while handling your request."); return StatusCode(500, ex.Message);
} }
} }
} }

View File

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