Compare commits

...

4 Commits

Author SHA1 Message Date
62fc6c1a49 Merge pull request 'Improve error handling and logging for mail and OTP endpoints' (#7) from feature/portfolio-admin-auth into dev
Reviewed-on: #7
2026-02-16 11:54:21 +05:30
4b968099ad 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.
2026-02-16 11:44:31 +05:30
90d04d7a42 Merge pull request 'Improve URL config parsing and mail exception handling' (#5) from feature/portfolio-admin-auth into dev
Reviewed-on: #5
2026-02-16 11:19:29 +05:30
2d603e4fc6 Improve URL config parsing and mail exception handling
Refactor Program.cs to robustly parse and validate URLs from config, logging a warning if none are valid. Enhance MailService exception handling to log full exceptions and avoid interrupting OTP generation by swallowing mail send errors.
2026-02-16 11:18:46 +05:30
4 changed files with 24 additions and 14 deletions

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

@ -33,13 +33,25 @@ Log.Logger = loggerConfiguration
.WriteTo.File("logs/portblog.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
var urls = builder.Configuration.GetSection("Urls");
var urlsSection = builder.Configuration.GetSection("Urls");
var urlsValue = urlsSection.Value;
if (!string.IsNullOrEmpty(urls.Value))
if (!string.IsNullOrWhiteSpace(urlsValue))
{
var allowedUrlsToUse = urls.Value.Split(',');
var allowedUrlsToUse = urlsValue
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Where(u => Uri.IsWellFormedUriString(u, UriKind.Absolute))
.ToArray();
builder.WebHost.UseUrls(allowedUrlsToUse);
if (allowedUrlsToUse.Length > 0)
{
builder.WebHost.UseUrls(allowedUrlsToUse);
}
else
{
// optionally log or throw depending on desired behavior
Log.Warning("Configured 'Urls' section was present but no valid URLs were found.");
}
}
var allowedCorsOrigins = builder.Configuration.GetSection("AllowedCorsOrigins");

View File

@ -67,16 +67,14 @@ namespace PortBlog.API.Services
}
catch (Exception ex)
{
logger.LogCritical(
"Exception while sending mail from {FromEmail} ({Name}): {Exception}",
messageSendDto.FromEmail,
messageSendDto.Name,
ex.Message
);
// 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();
throw new Exception();
// 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);
}
}
}