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:
parent
eb271793ad
commit
55e82425a9
8 changed files with 75 additions and 20 deletions
|
|
@ -1,7 +1,7 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using SurveyBackend.Core.Services;
|
||||
using SurveyBackend.DTOs;
|
||||
using SurveyBackend.Mappers.UserDTOs;
|
||||
using SurveyBackend.Services.Services;
|
||||
|
||||
namespace SurveyBackend.Controllers;
|
||||
|
||||
|
|
@ -9,9 +9,9 @@ namespace SurveyBackend.Controllers;
|
|||
[Route("auth")]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly AuthorizationService _authorizationService;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
|
||||
public AuthController(AuthorizationService authorizationService)
|
||||
public AuthController(IAuthorizationService authorizationService)
|
||||
{
|
||||
_authorizationService = authorizationService;
|
||||
}
|
||||
|
|
@ -20,21 +20,13 @@ public class AuthController : ControllerBase
|
|||
public async Task<IActionResult> LogIn([FromBody] UserLoginDto loginData)
|
||||
{
|
||||
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")]
|
||||
public async Task<IActionResult> Register([FromBody] UserRegistrationDto registerData)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _authorizationService.RegisterUser(UserRegistrationMapper.UserRegistrationToModel(registerData));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
|
||||
await _authorizationService.RegisterUser(UserRegistrationMapper.UserRegistrationToModel(registerData));
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ using SurveyBackend.Core.Services;
|
|||
using SurveyBackend.Infrastructure;
|
||||
using SurveyBackend.Infrastructure.Data;
|
||||
using SurveyBackend.Infrastructure.Repositories;
|
||||
using SurveyBackend.Middlewares;
|
||||
using SurveyBackend.Services;
|
||||
using SurveyBackend.Services.Services;
|
||||
using SurveyLib.Core.Repositories;
|
||||
|
|
@ -71,6 +72,8 @@ public class Program
|
|||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseMiddleware<ExceptionsMiddleware>();
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue