From eb271793ad910d426aa2052ea310b68ae2c95da1 Mon Sep 17 00:00:00 2001 From: shept Date: Fri, 18 Apr 2025 14:50:01 +0500 Subject: [PATCH] add custom exceptions and start using them in every service --- .../Exceptions/ConflictException.cs | 10 ++++++++++ .../Exceptions/ServiceException.cs | 10 ++++++++++ .../Exceptions/UnauthorizedException.cs | 10 ++++++++++ .../Services/AuthorizationService.cs | 5 +++-- 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 SurveyBackend/SurveyBackend.Services/Exceptions/ConflictException.cs create mode 100644 SurveyBackend/SurveyBackend.Services/Exceptions/ServiceException.cs create mode 100644 SurveyBackend/SurveyBackend.Services/Exceptions/UnauthorizedException.cs diff --git a/SurveyBackend/SurveyBackend.Services/Exceptions/ConflictException.cs b/SurveyBackend/SurveyBackend.Services/Exceptions/ConflictException.cs new file mode 100644 index 0000000..12d14ce --- /dev/null +++ b/SurveyBackend/SurveyBackend.Services/Exceptions/ConflictException.cs @@ -0,0 +1,10 @@ +namespace SurveyBackend.Services.Exceptions; + +public class ConflictException : ServiceException +{ + public override int StatusCode => 409; + + public ConflictException(string message) : base(message) + { + } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.Services/Exceptions/ServiceException.cs b/SurveyBackend/SurveyBackend.Services/Exceptions/ServiceException.cs new file mode 100644 index 0000000..30fb016 --- /dev/null +++ b/SurveyBackend/SurveyBackend.Services/Exceptions/ServiceException.cs @@ -0,0 +1,10 @@ +namespace SurveyBackend.Services.Exceptions; + +public abstract class ServiceException : Exception +{ + public abstract int StatusCode { get; } + + protected ServiceException(string message) : base(message) + { + } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.Services/Exceptions/UnauthorizedException.cs b/SurveyBackend/SurveyBackend.Services/Exceptions/UnauthorizedException.cs new file mode 100644 index 0000000..58ea510 --- /dev/null +++ b/SurveyBackend/SurveyBackend.Services/Exceptions/UnauthorizedException.cs @@ -0,0 +1,10 @@ +namespace SurveyBackend.Services.Exceptions; + +public class UnauthorizedException : ServiceException +{ + public override int StatusCode => 401; + + public UnauthorizedException(string message) : base(message) + { + } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.Services/Services/AuthorizationService.cs b/SurveyBackend/SurveyBackend.Services/Services/AuthorizationService.cs index 0e9907f..05acef2 100644 --- a/SurveyBackend/SurveyBackend.Services/Services/AuthorizationService.cs +++ b/SurveyBackend/SurveyBackend.Services/Services/AuthorizationService.cs @@ -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);