Skip to content

Commit

Permalink
Merge pull request #41 from PandaTechAM/development
Browse files Browse the repository at this point in the history
Exception extension overloads added
  • Loading branch information
HaikAsatryan committed Jun 29, 2024
2 parents cb9c275 + 7a98437 commit ce26399
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
40 changes: 40 additions & 0 deletions src/ResponseCrafter/HttpExceptions/ForbiddenException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,29 @@ public static void ThrowIfNull([NotNull] object? value, string exceptionMessage)
}
}

public static void ThrowIfNull([NotNull] object? value)
{
if (value is null)
{
throw new ForbiddenException();
}
}

public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ForbiddenException(exceptionMessage);
}
}

public static void ThrowIfNullOrWhiteSpace([NotNull] string? value)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ForbiddenException();
}
}

public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values, string exceptionMessage)
{
Expand All @@ -29,6 +45,14 @@ public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values, stri
throw new ForbiddenException(exceptionMessage);
}
}

public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values)
{
if (values is null || values.Count == 0 || values.Any(string.IsNullOrWhiteSpace))
{
throw new ForbiddenException();
}
}

public static void ThrowIf(bool condition, string exceptionMessage)
{
Expand All @@ -37,6 +61,14 @@ public static void ThrowIf(bool condition, string exceptionMessage)
throw new ForbiddenException(exceptionMessage);
}
}

public static void ThrowIf(bool condition)
{
if (condition)
{
throw new ForbiddenException();
}
}

public static void ThrowIfNullOrNegative([NotNull] decimal? value, string exceptionMessage)
{
Expand All @@ -45,4 +77,12 @@ public static void ThrowIfNullOrNegative([NotNull] decimal? value, string except
throw new ForbiddenException(exceptionMessage);
}
}

public static void ThrowIfNullOrNegative([NotNull] decimal? value)
{
if (value is < 0 or null)
{
throw new ForbiddenException();
}
}
}
40 changes: 40 additions & 0 deletions src/ResponseCrafter/HttpExceptions/UnauthorizedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,29 @@ public static void ThrowIfNull([NotNull] object? value, string exceptionMessage)
}
}

public static void ThrowIfNull([NotNull] object? value)
{
if (value is null)
{
throw new UnauthorizedException();
}
}

public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new UnauthorizedException(exceptionMessage);
}
}

public static void ThrowIfNullOrWhiteSpace([NotNull] string? value)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new UnauthorizedException();
}
}

public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values, string exceptionMessage)
{
Expand All @@ -29,6 +45,14 @@ public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values, stri
throw new UnauthorizedException(exceptionMessage);
}
}

public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values)
{
if (values is null || values.Count == 0 || values.Any(string.IsNullOrWhiteSpace))
{
throw new UnauthorizedException();
}
}

public static void ThrowIf(bool condition, string exceptionMessage)
{
Expand All @@ -37,6 +61,14 @@ public static void ThrowIf(bool condition, string exceptionMessage)
throw new UnauthorizedException(exceptionMessage);
}
}

public static void ThrowIf(bool condition)
{
if (condition)
{
throw new UnauthorizedException();
}
}

public static void ThrowIfNullOrNegative([NotNull] decimal? value, string exceptionMessage)
{
Expand All @@ -45,4 +77,12 @@ public static void ThrowIfNullOrNegative([NotNull] decimal? value, string except
throw new UnauthorizedException(exceptionMessage);
}
}

public static void ThrowIfNullOrNegative([NotNull] decimal? value)
{
if (value is < 0 or null)
{
throw new UnauthorizedException();
}
}
}
4 changes: 2 additions & 2 deletions src/ResponseCrafter/ResponseCrafter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
<Copyright>MIT</Copyright>
<PackageIcon>pandatech.png</PackageIcon>
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Version>2.2.1</Version>
<Version>2.2.2</Version>
<PackageId>Pandatech.ResponseCrafter</PackageId>
<PackageTags>Pandatech, library, exception handler, exception, middleware, Api response</PackageTags>
<Title>ResponseCrafter</Title>
<Description>Handling exceptions, custom Dtos.</Description>
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-response-crafter</RepositoryUrl>
<PackageReleaseNotes>Exception extension compiler enhancment and new features has been added</PackageReleaseNotes>
<PackageReleaseNotes>Exception extension overloads added</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit ce26399

Please sign in to comment.