add custom exceptions and start using them in every service

This commit is contained in:
Вячеслав 2025-04-18 14:50:01 +05:00
parent 7bff14a66e
commit eb271793ad
4 changed files with 33 additions and 2 deletions

View file

@ -1,5 +1,6 @@
using SurveyBackend.Core.Models;
using SurveyBackend.Core.Services;
using SurveyBackend.Services.Exceptions;
using SurveyBackend.Services.Helpers;
namespace SurveyBackend.Services.Services;
@ -20,7 +21,7 @@ public class AuthorizationService : IAuthorizationService
var user = await _userService.GetUserByEmail(email);
if (user is null || !_passwordHasher.Verify(password, user.Password))
{
return null;
throw new UnauthorizedException("Email or password is incorrect.");
}
var token = TokenHelper.GetAuthToken(user);
@ -32,7 +33,7 @@ public class AuthorizationService : IAuthorizationService
var existingUser = await _userService.GetUserByEmail(user.Email);
if (existingUser is not null)
{
throw new Exception("Email already exists");
throw new ConflictException("Email already exists");
}
user.Password = _passwordHasher.HashPassword(user.Password);