say NO to try-catch in controllers

- added ExceptionsMiddleware.cs
- added more Exception types
- removed all exceptions logic in controllers
This commit is contained in:
Вячеслав 2025-04-18 15:15:32 +05:00
parent eb271793ad
commit 55e82425a9
8 changed files with 75 additions and 20 deletions

View file

@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using SurveyBackend.Core.Services;
using SurveyBackend.DTOs; using SurveyBackend.DTOs;
using SurveyBackend.Mappers.UserDTOs; using SurveyBackend.Mappers.UserDTOs;
using SurveyBackend.Services.Services;
namespace SurveyBackend.Controllers; namespace SurveyBackend.Controllers;
@ -9,9 +9,9 @@ namespace SurveyBackend.Controllers;
[Route("auth")] [Route("auth")]
public class AuthController : ControllerBase public class AuthController : ControllerBase
{ {
private readonly AuthorizationService _authorizationService; private readonly IAuthorizationService _authorizationService;
public AuthController(AuthorizationService authorizationService) public AuthController(IAuthorizationService authorizationService)
{ {
_authorizationService = authorizationService; _authorizationService = authorizationService;
} }
@ -20,21 +20,13 @@ public class AuthController : ControllerBase
public async Task<IActionResult> LogIn([FromBody] UserLoginDto loginData) public async Task<IActionResult> LogIn([FromBody] UserLoginDto loginData)
{ {
var token = await _authorizationService.LogInUser(loginData.Email, loginData.Password); var token = await _authorizationService.LogInUser(loginData.Email, loginData.Password);
return token is null ? Unauthorized() : Ok(new { token = token }); return Ok(new { token = token });
} }
[HttpPost("register")] [HttpPost("register")]
public async Task<IActionResult> Register([FromBody] UserRegistrationDto registerData) public async Task<IActionResult> Register([FromBody] UserRegistrationDto registerData)
{ {
try await _authorizationService.RegisterUser(UserRegistrationMapper.UserRegistrationToModel(registerData));
{
await _authorizationService.RegisterUser(UserRegistrationMapper.UserRegistrationToModel(registerData));
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
return Ok(); return Ok();
} }
} }

View file

@ -0,0 +1,49 @@
using SurveyBackend.Services.Exceptions;
namespace SurveyBackend.Middlewares;
public class ExceptionsMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<ExceptionsMiddleware> _logger;
public ExceptionsMiddleware(RequestDelegate next, ILogger<ExceptionsMiddleware> logger)
{
_next = next;
_logger = logger;
}
public async Task InvokeAsync(HttpContext context)
{
try
{
await _next(context);
}
catch (ServiceException ex)
{
context.Response.StatusCode = ex.StatusCode;
context.Response.ContentType = "application/json";
var response = new
{
error = ex.Message
};
await context.Response.WriteAsJsonAsync(response);
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
context.Response.StatusCode = 500;
context.Response.ContentType = "application/json";
var response = new
{
error = "Internal Server Error. GG WP, request bub fix"
};
await context.Response.WriteAsJsonAsync(response);
}
}
}

View file

@ -7,6 +7,7 @@ using SurveyBackend.Core.Services;
using SurveyBackend.Infrastructure; using SurveyBackend.Infrastructure;
using SurveyBackend.Infrastructure.Data; using SurveyBackend.Infrastructure.Data;
using SurveyBackend.Infrastructure.Repositories; using SurveyBackend.Infrastructure.Repositories;
using SurveyBackend.Middlewares;
using SurveyBackend.Services; using SurveyBackend.Services;
using SurveyBackend.Services.Services; using SurveyBackend.Services.Services;
using SurveyLib.Core.Repositories; using SurveyLib.Core.Repositories;
@ -71,6 +72,8 @@ public class Program
app.UseSwaggerUI(); app.UseSwaggerUI();
} }
app.UseMiddleware<ExceptionsMiddleware>();
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();

View file

@ -4,6 +4,6 @@ namespace SurveyBackend.Core.Services;
public interface IAuthorizationService public interface IAuthorizationService
{ {
public Task<string?> LogInUser(string email, string password); public Task<string> LogInUser(string email, string password);
public Task RegisterUser(User user); public Task RegisterUser(User user);
} }

View file

@ -4,6 +4,6 @@ namespace SurveyBackend.Core.Services;
public interface IUserService public interface IUserService
{ {
public Task<User?> GetUserByEmail(string email); public Task<User> GetUserByEmail(string email);
public Task CreateUserAsync(User user); public Task CreateUserAsync(User user);
} }

View file

@ -0,0 +1,10 @@
namespace SurveyBackend.Services.Exceptions;
public class NotFoundException : ServiceException
{
public override int StatusCode => 404;
public NotFoundException(string message) : base(message)
{
}
}

View file

@ -16,12 +16,12 @@ public class AuthorizationService : IAuthorizationService
_passwordHasher = passwordHasher; _passwordHasher = passwordHasher;
} }
public async Task<string?> LogInUser(string email, string password) public async Task<string> LogInUser(string email, string password)
{ {
var user = await _userService.GetUserByEmail(email); var user = await _userService.GetUserByEmail(email);
if (user is null || !_passwordHasher.Verify(password, user.Password)) if (!_passwordHasher.Verify(password, user.Password))
{ {
throw new UnauthorizedException("Email or password is incorrect."); throw new UnauthorizedException("Password is incorrect.");
} }
var token = TokenHelper.GetAuthToken(user); var token = TokenHelper.GetAuthToken(user);

View file

@ -1,6 +1,7 @@
using SurveyBackend.Core.Models; using SurveyBackend.Core.Models;
using SurveyBackend.Core.Repositories; using SurveyBackend.Core.Repositories;
using SurveyBackend.Core.Services; using SurveyBackend.Core.Services;
using SurveyBackend.Services.Exceptions;
namespace SurveyBackend.Services.Services; namespace SurveyBackend.Services.Services;
@ -13,9 +14,9 @@ public class UserService : IUserService
_userRepository = userRepository; _userRepository = userRepository;
} }
public async Task<User?> GetUserByEmail(string email) public async Task<User> GetUserByEmail(string email)
{ {
return await _userRepository.GetUserByEmail(email); return await _userRepository.GetUserByEmail(email) ?? throw new NotFoundException("Email not found");
} }
public async Task CreateUserAsync(User user) public async Task CreateUserAsync(User user)