Add Global Exception Handling

This commit is contained in:
lumijiez
2025-06-10 23:30:58 +03:00
parent b8ddee390a
commit db61171ada
8 changed files with 102 additions and 7 deletions

View File

@@ -0,0 +1,7 @@
namespace Imprink.Application.Exceptions;
public abstract class BaseApplicationException : Exception
{
protected BaseApplicationException(string message) : base(message) { }
protected BaseApplicationException(string message, Exception innerException) : base(message, innerException) { }
}

View File

@@ -0,0 +1,7 @@
namespace Imprink.Application.Exceptions;
public class NotFoundException : BaseApplicationException
{
public NotFoundException(string message) : base(message) { }
public NotFoundException(string message, Exception innerException) : base(message, innerException) { }
}

View File

@@ -1,3 +1,4 @@
using Imprink.Application.Exceptions;
using Imprink.Application.Users.Dtos;
using Imprink.Domain.Entities.Users;
using MediatR;
@@ -10,7 +11,8 @@ public class DeleteUserRoleHandler(IUnitOfWork uw) : IRequestHandler<DeleteUserR
{
public async Task<UserRoleDto?> Handle(DeleteUserRoleCommand request, CancellationToken cancellationToken)
{
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken)) return null;
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken))
throw new NotFoundException("User with ID: " + request.Sub + " does not exist.");
var userRole = new UserRole
{

View File

@@ -1,3 +1,4 @@
using Imprink.Application.Exceptions;
using Imprink.Application.Users.Dtos;
using MediatR;
@@ -9,7 +10,8 @@ public class GetUserRolesHandler(IUnitOfWork uw): IRequestHandler<GetUserRolesCo
{
public async Task<IEnumerable<RoleDto>> Handle(GetUserRolesCommand request, CancellationToken cancellationToken)
{
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken)) return [];
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken))
throw new NotFoundException("User with ID: " + request.Sub + " does not exist.");
var roles = await uw.UserRoleRepository.GetUserRolesAsync(request.Sub, cancellationToken);

View File

@@ -1,3 +1,4 @@
using Imprink.Application.Exceptions;
using Imprink.Application.Users.Dtos;
using Imprink.Domain.Entities.Users;
using MediatR;
@@ -10,7 +11,8 @@ public class SetUserRoleHandler(IUnitOfWork uw) : IRequestHandler<SetUserRoleCom
{
public async Task<UserRoleDto?> Handle(SetUserRoleCommand request, CancellationToken cancellationToken)
{
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken)) return null;
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken))
throw new NotFoundException("User with ID: " + request.Sub + " does not exist.");
var userRole = new UserRole
{