Skip to content

Commit

Permalink
add exception constructors for better exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
emrecoskun705 committed Sep 21, 2023
1 parent 13d04c9 commit c7c0f3e
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 14 deletions.
9 changes: 9 additions & 0 deletions Unitagram.Application/Exceptions/AccountLockoutException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ namespace Unitagram.Application.Exceptions;

public class AccountLockoutException : Exception
{
public AccountLockoutException(string message) : base(message)
{

}

public AccountLockoutException()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ namespace Unitagram.Application.Exceptions;

public class EmailAlreadyConfirmedException : Exception
{
public EmailAlreadyConfirmedException(string message) : base(message)
{

}

public EmailAlreadyConfirmedException()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ namespace Unitagram.Application.Exceptions.EmailVerification;

public class EmailOtpNotFoundException : Exception
{
public EmailOtpNotFoundException(string message) : base(message)
{

}

public EmailOtpNotFoundException()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ namespace Unitagram.Application.Exceptions.EmailVerification;

public class InvalidCodeException : Exception
{
public InvalidCodeException(string message) : base(message)
{

}

public InvalidCodeException()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ namespace Unitagram.Application.Exceptions.EmailVerification;

public class OtpCodeTryAgainLaterException : Exception
{
public OtpCodeTryAgainLaterException(int minutesDifference) : base(minutesDifference.ToString(CultureInfo.InvariantCulture))
public OtpCodeTryAgainLaterException(string message) : base(message)
{

}

public OtpCodeTryAgainLaterException()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ namespace Unitagram.Application.Exceptions.EmailVerification;

public class ReachedMaximumCodeUsageException : Exception
{
public ReachedMaximumCodeUsageException(string message) : base(message)
{

}

public ReachedMaximumCodeUsageException()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ namespace Unitagram.Application.Exceptions;

public class InvalidAccountCredentialsException : Exception
{
public InvalidAccountCredentialsException(string message) : base(message)
{

}

public InvalidAccountCredentialsException()
{

}
}
9 changes: 9 additions & 0 deletions Unitagram.Application/Exceptions/JwtTokenException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ namespace Unitagram.Application.Exceptions;

public class JwtTokenException : Exception
{
public JwtTokenException(string message) : base(message)
{

}

public JwtTokenException()
{

}
}
9 changes: 7 additions & 2 deletions Unitagram.Application/Exceptions/NotFoundException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ namespace Unitagram.Application.Exceptions;

public class NotFoundException : Exception
{
public NotFoundException(string name) : base(name)
public NotFoundException(string message) : base(message)
{


}

public NotFoundException()
{

}
}
9 changes: 9 additions & 0 deletions Unitagram.Application/Exceptions/UserNotFoundException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ namespace Unitagram.Application.Exceptions;

public class UserNotFoundException : Exception
{
public UserNotFoundException(string message) : base(message)
{

}

public UserNotFoundException()
{

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MediatR;
using Unitagram.Application.Contracts.Localization;
using Unitagram.Application.Contracts.Persistence;
using Unitagram.Application.Exceptions;

Expand All @@ -7,15 +8,17 @@ namespace Unitagram.Application.Features.University.Commands.CreateUniversity;
public class CreateUniversityCommandHandler : IRequestHandler<CreateUniversityCommand, Unit>
{
private readonly IUniversityRepository _universityRepository;
private readonly ILocalizationService _localizationService;

public CreateUniversityCommandHandler(IUniversityRepository universityRepository)
public CreateUniversityCommandHandler(IUniversityRepository universityRepository, ILocalizationService localizationService)
{
_universityRepository = universityRepository;
_localizationService = localizationService;
}

public async Task<Unit> Handle(CreateUniversityCommand request, CancellationToken cancellationToken)
{
var validator = new CreateUniversityCommandValidator();
var validator = new CreateUniversityCommandValidator(_localizationService);
var validationResult = await validator.ValidateAsync(request);

if (validationResult.Errors.Any())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
using FluentValidation;
using Unitagram.Application.Contracts.Localization;

namespace Unitagram.Application.Features.University.Commands.CreateUniversity;

public class CreateUniversityCommandValidator : AbstractValidator<CreateUniversityCommand>
{
public CreateUniversityCommandValidator()
private readonly ILocalizationService _localizer;

public CreateUniversityCommandValidator(ILocalizationService localizer)
{
_localizer = localizer;



RuleFor(p => p.Domain)
.NotEmpty().WithMessage("{PropertyName} is required")
.NotEmpty().WithMessage(_localizer.GetLocalizedString("PropertyNameRequired"))
.NotNull()
.MaximumLength(80).WithMessage("{PropertyName} must be less than 800 characters");

RuleFor(p =>p.Province)
.NotEmpty().WithMessage("{PropertyName} is required")
.NotEmpty().WithMessage(_localizer.GetLocalizedString("PropertyNameRequired"))
.NotNull()
.MaximumLength(80).WithMessage("{PropertyName} must be less than 800 characters");

RuleFor(p =>p.Name)
.NotEmpty().WithMessage("{PropertyName} is required")
.NotEmpty().WithMessage(_localizer.GetLocalizedString("PropertyNameRequired"))
.NotNull()
.MaximumLength(80).WithMessage("{PropertyName} must be less than 80 characters");
}
Expand Down
2 changes: 1 addition & 1 deletion Unitagram.Identity/Services/EmailVerificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task<Result<Unit>> GenerateAsync(Guid userId)
}

var minutesDifference = CalculateMinutesDifference(otpConfirmation.RetryDateTimeUtc!.Value);
var exception = new OtpCodeTryAgainLaterException(minutesDifference);
var exception = new OtpCodeTryAgainLaterException();
return new Result<Unit>(exception);
}

Expand Down
4 changes: 2 additions & 2 deletions Unitagram.Infrastructure/Resources/SharedResources.en-us.resx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="test" xml:space="preserve">
<value>test enggg</value>
<data name="PropertyNameRequired" xml:space="preserve">
<value>{PropertyName} is required.</value>
</data>
</root>
4 changes: 2 additions & 2 deletions Unitagram.Infrastructure/Resources/SharedResources.tr-TR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="test" xml:space="preserve">
<value>testtrtrtr</value>
<data name="PropertyNameRequired" xml:space="preserve">
<value>{PropertyName} gereklidir.</value>
</data>
</root>

0 comments on commit c7c0f3e

Please sign in to comment.