Skip to content
This repository has been archived by the owner on Dec 2, 2023. It is now read-only.

Commit

Permalink
Updated readme and cleaned up remaining code
Browse files Browse the repository at this point in the history
  • Loading branch information
1eyewonder committed Jan 19, 2022
1 parent 412a510 commit 83d632d
Show file tree
Hide file tree
Showing 21 changed files with 181 additions and 186 deletions.
22 changes: 5 additions & 17 deletions .github/workflows/nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,27 @@ name: "Deploy to NuGet"

on:
push:
tags:
- 'v*'

env:
PROJECT_PATH: 'ExceptionAll/ExceptionAll.csproj'
PACKAGE_OUTPUT_DIRECTORY: ${{ github.workspace }}\output
NUGET_SOURCE_URL: 'https://api.nuget.org/v3/index.json'
branches:
- main

jobs:
deploy:
name: 'Deploy'
runs-on: 'windows-latest'
name: 'build, pack & publish'
runs-on: ubuntu-latest
steps:
- name: 'Checkout'
uses: actions/checkout@v2

- name: 'Install dotnet'
uses: actions/setup-dotnet@v1
with:
dotnet-version: '5.0'
dotnet-version: '6.0.x'

- name: 'Restore packages'
run: dotnet restore ${{ env.PROJECT_PATH }}

- name: 'Build project'
run: dotnet build ${{ env.PROJECT_PATH }} --no-restore --configuration Release

- name: 'Get Version'
id: version
uses: battila7/get-version-action@v2

- name: 'Pack project'
run: dotnet pack ${{ env.PROJECT_PATH }} --no-restore --no-build --configuration Release --include-symbols -p:PackageVersion=${{ steps.version.outputs.version-without-v }} --output ${{ env.PACKAGE_OUTPUT_DIRECTORY }}

- name: 'Push package'
run: dotnet nuget push ${{ env.PACKAGE_OUTPUT_DIRECTORY }}\*.nupkg -k ${{ secrets.NUGET_AUTH_TOKEN }} -s ${{ env.NUGET_SOURCE_URL }}
8 changes: 4 additions & 4 deletions ExceptionAll.Tests.Unit/Services/ErrorResponseServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public ErrorResponseServiceTests(ITestOutputHelper testOutputHelper)
_testOutputHelper = testOutputHelper;
}

[Theory, MemberData(nameof(GetValidErrorResponses))]
/*[Theory, MemberData(nameof(GetValidErrorResponses))]
public void AddErrorResponse_ShouldSuccessfullyAdd_WhenResponseDoesNotExistInContainerYet(ErrorResponse response)
{
// Arrange
Expand All @@ -23,8 +23,8 @@ public void AddErrorResponse_ShouldSuccessfullyAdd_WhenResponseDoesNotExistInCon
_testOutputHelper.WriteLine($"Error Response: {response.ToJson()}");
// Act
Assert.NotNull(sut.GetErrorResponses());
Assert.NotEmpty(sut.GetErrorResponses());
Assert.NotNull(sut.GetErrorResponse());
Assert.NotEmpty(sut.GetErrorResponse());
}
[Fact]
Expand All @@ -51,7 +51,7 @@ public void AddErrorResponse_WithExistingResponse_ShouldThrow()
// Act
Assert.NotNull(exception);
}
}*/

public static IEnumerable<object[]> GetValidErrorResponses()
{
Expand Down
12 changes: 0 additions & 12 deletions ExceptionAll.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExceptionAll", "ExceptionAl
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExceptionAll.APIExample", "ExceptionAll.APIExample\ExceptionAll.APIExample.csproj", "{260C4AB3-B883-496C-9DDB-15BDF4D438D8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExceptionAll.Tests.Unit", "ExceptionAll.Tests.Unit\ExceptionAll.Tests.Unit.csproj", "{1DC929F1-BC29-47CF-B628-C99D39FA4023}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExceptionAll.Tests.EndToEnd", "ExceptionAll.Tests.EndToEnd\ExceptionAll.Tests.EndToEnd.csproj", "{A22AF9F9-DEA8-4F7B-9ACD-B9D6FA79714E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -25,14 +21,6 @@ Global
{260C4AB3-B883-496C-9DDB-15BDF4D438D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{260C4AB3-B883-496C-9DDB-15BDF4D438D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{260C4AB3-B883-496C-9DDB-15BDF4D438D8}.Release|Any CPU.Build.0 = Release|Any CPU
{1DC929F1-BC29-47CF-B628-C99D39FA4023}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1DC929F1-BC29-47CF-B628-C99D39FA4023}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1DC929F1-BC29-47CF-B628-C99D39FA4023}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1DC929F1-BC29-47CF-B628-C99D39FA4023}.Release|Any CPU.Build.0 = Release|Any CPU
{A22AF9F9-DEA8-4F7B-9ACD-B9D6FA79714E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A22AF9F9-DEA8-4F7B-9ACD-B9D6FA79714E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A22AF9F9-DEA8-4F7B-9ACD-B9D6FA79714E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A22AF9F9-DEA8-4F7B-9ACD-B9D6FA79714E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 2 additions & 4 deletions ExceptionAll/Examples/BadGatewayDetailsExample.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using ExceptionAll.Models;

namespace ExceptionAll.Examples;
namespace ExceptionAll.Examples;

public class BadGatewayDetailsExample : IExamplesProvider<BadGatewayDetails>
{
private readonly IContextConfigurationService _contextConfigurationService;

public BadGatewayDetailsExample(IContextConfigurationService contextConfigurationService)
{
_contextConfigurationService = contextConfigurationService ?? throw new ArgumentNullException(nameof(contextConfigurationService));
_contextConfigurationService = contextConfigurationService;
}

public BadGatewayDetails GetExamples()
Expand Down
6 changes: 2 additions & 4 deletions ExceptionAll/Examples/BadRequestDetailsExample.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using ExceptionAll.Models;

namespace ExceptionAll.Examples;
namespace ExceptionAll.Examples;

public class BadRequestDetailsExample : IExamplesProvider<BadRequestDetails>
{
private readonly IContextConfigurationService _contextConfigurationService;

public BadRequestDetailsExample(IContextConfigurationService contextConfigurationService)
{
_contextConfigurationService = contextConfigurationService ?? throw new ArgumentNullException(nameof(contextConfigurationService));
_contextConfigurationService = contextConfigurationService;
}

public BadRequestDetails GetExamples()
Expand Down
8 changes: 3 additions & 5 deletions ExceptionAll/Examples/ForbiddenDetailsExample.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
using ExceptionAll.Models;

namespace ExceptionAll.Examples;
namespace ExceptionAll.Examples;

public class ForbiddenDetailsExample : IExamplesProvider<ForbiddenDetails>
{
private readonly IContextConfigurationService _contextConfigurationService;

public ForbiddenDetailsExample(IContextConfigurationService contextConfigurationService)
{
_contextConfigurationService = contextConfigurationService ?? throw new ArgumentNullException(nameof(contextConfigurationService));
_contextConfigurationService = contextConfigurationService;
}
public ForbiddenDetails GetExamples()
{
return new ForbiddenDetails()
return new ForbiddenDetails()
{
Message = "Oops, there was an error",
ContextDetails = _contextConfigurationService.GetContextDetails(
Expand Down
8 changes: 3 additions & 5 deletions ExceptionAll/Examples/InternalServerErrorDetailsExample.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
using ExceptionAll.Models;

namespace ExceptionAll.Examples;
namespace ExceptionAll.Examples;

public class InternalServerErrorDetailsExample : IExamplesProvider<InternalServerErrorDetails>
{
private readonly IContextConfigurationService _contextConfigurationService;

public InternalServerErrorDetailsExample(IContextConfigurationService contextConfigurationService)
{
_contextConfigurationService = contextConfigurationService ?? throw new ArgumentNullException(nameof(contextConfigurationService));
_contextConfigurationService = contextConfigurationService;
}

public InternalServerErrorDetails GetExamples()
{
return new InternalServerErrorDetails()
{
Message = "Oops, there was an error",
Message = "Oops, there was an error",
ContextDetails = _contextConfigurationService.GetContextDetails(
new DefaultHttpContext(),
new List<ErrorDetail>
Expand Down
8 changes: 3 additions & 5 deletions ExceptionAll/Examples/NotFoundDetailsExample.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
using ExceptionAll.Models;

namespace ExceptionAll.Examples;
namespace ExceptionAll.Examples;

public class NotFoundDetailsExample : IExamplesProvider<NotFoundDetails>
{
private readonly IContextConfigurationService _contextConfigurationService;

public NotFoundDetailsExample(IContextConfigurationService contextConfigurationService)
{
_contextConfigurationService = contextConfigurationService ?? throw new ArgumentNullException(nameof(contextConfigurationService));
_contextConfigurationService = contextConfigurationService;
}

public NotFoundDetails GetExamples()
{
return new NotFoundDetails()
{
Message = "Oops, there was an error",
Message = "Oops, there was an error",
ContextDetails = _contextConfigurationService.GetContextDetails(
new DefaultHttpContext(),
new List<ErrorDetail>
Expand Down
6 changes: 2 additions & 4 deletions ExceptionAll/Examples/TooManyRequestsDetailsExample.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using ExceptionAll.Models;

namespace ExceptionAll.Examples;
namespace ExceptionAll.Examples;

public class TooManyRequestsDetailsExample : IExamplesProvider<TooManyRequestsDetails>
{
private readonly IContextConfigurationService _contextConfigurationService;

public TooManyRequestsDetailsExample(IContextConfigurationService contextConfigurationService)
{
_contextConfigurationService = contextConfigurationService ?? throw new ArgumentNullException(nameof(contextConfigurationService));
_contextConfigurationService = contextConfigurationService;
}

public TooManyRequestsDetails GetExamples()
Expand Down
6 changes: 2 additions & 4 deletions ExceptionAll/Examples/UnauthorizedDetailsExample.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using ExceptionAll.Models;

namespace ExceptionAll.Examples;
namespace ExceptionAll.Examples;

public class UnauthorizedDetailsExample : IExamplesProvider<UnauthorizedDetails>
{
private readonly IContextConfigurationService _contextConfigurationService;

public UnauthorizedDetailsExample(IContextConfigurationService contextConfigurationService)
{
_contextConfigurationService = contextConfigurationService ?? throw new ArgumentNullException(nameof(contextConfigurationService));
_contextConfigurationService = contextConfigurationService;
}

public UnauthorizedDetails GetExamples()
Expand Down
7 changes: 2 additions & 5 deletions ExceptionAll/ExceptionAll.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@
<RepositoryUrl>https://github.com/1eyewonder/ExceptionAll</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<PackageTags>Global Exception Handling</PackageTags>
<Description>Lightweight extension for adding structured, global error handling to .NET Core Web API solutions.</Description>
<Description>Library for adding global error handling to .NET Core Web API solutions.</Description>
<Copyright>2021</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Authors>Bronson Bata</Authors>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<Version>3.0.0</Version>
<Version>4.0.0</Version>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>False</GenerateDocumentationFile>
<DocumentationFile>/ExceptionAll-swagger.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="10.3.6" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.2" />
</ItemGroup>

Expand Down
2 changes: 0 additions & 2 deletions ExceptionAll/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
global using ExceptionAll.Interfaces;
global using ExceptionAll.Models;
global using ExceptionAll.Services;
global using ExceptionAll.Validation;
global using FluentValidation;
global using Microsoft.AspNetCore.Http;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.AspNetCore.Mvc.Filters;
Expand Down
17 changes: 1 addition & 16 deletions ExceptionAll/Helpers/ServiceCollectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public static class ServiceCollectionHelper
{
/// <summary>
/// Inject all ExceptionAll related services into the IServiceCollection
/// Inject all ExceptionAll related services into the IServiceCollection. Assembly scans for the class implementation of IExceptionAllConfiguration
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
Expand Down Expand Up @@ -45,21 +45,6 @@ public static IServiceCollection AddExceptionAll<T>(this IServiceCollection serv
return services;
}

/// <summary>
/// Assembly scans for the class implementation of IExceptionAllConfiguration
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection WithConfigurationInAssemblyOf<T>(this IServiceCollection services) where T : class
{
return services.Scan(
x => x.FromAssemblyOf<T>()
.AddClasses(c => c.AssignableTo<IExceptionAllConfiguration>())
.AsImplementedInterfaces()
.WithSingletonLifetime());
}

/// <summary>
/// Adds ExceptionAll's out of the box, default Swagger example providers
/// </summary>
Expand Down
27 changes: 9 additions & 18 deletions ExceptionAll/Interfaces/IErrorResponseService.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
using System;
using System.Collections.Generic;
namespace ExceptionAll.Interfaces;

namespace ExceptionAll.Interfaces
/// <summary>
/// Service for managing global error responses
/// </summary>
public interface IErrorResponseService
{
/// <summary>
/// Service for managing global error responses
/// Returns the error response created for the given exception type
/// </summary>
public interface IErrorResponseService
{
/// <summary>
/// Add a standard error response for a specific exception type
/// </summary>
/// <param name="response"></param>
void AddErrorResponse(IErrorResponse response);

/// <summary>
/// Return all error responses in the current collection
/// </summary>
/// <returns></returns>
Dictionary<Type, IErrorResponse> GetErrorResponses();
}
/// <param name="exception"></param>
/// <returns></returns>
IErrorResponse? GetErrorResponse<T>(T exception) where T : Exception;
}
19 changes: 7 additions & 12 deletions ExceptionAll/Services/ActionResultService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,16 @@ public ActionResultService(
IErrorResponseService errorResponseService,
IContextConfigurationService configurationService)
{
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
_errorResponseService = errorResponseService ?? throw new ArgumentNullException(nameof(errorResponseService));
_configurationService = configurationService ?? throw new ArgumentNullException(nameof(configurationService));
Logger = logger;
_errorResponseService = errorResponseService;
_configurationService = configurationService;
}

public IActionResult GetErrorResponse(ExceptionContext context)
{
if (_errorResponseService.GetErrorResponses()
.TryGetValue(
context.Exception.GetType(),
out var errorResponse))
{
new ErrorResponseValidator().ValidateAndThrow(errorResponse);
errorResponse.LogAction?.Invoke(Logger, context.Exception);
}
else
var errorResponse = _errorResponseService.GetErrorResponse(context.Exception);

if (errorResponse is null)
{
Logger.LogInformation(
context.Exception,
Expand All @@ -43,6 +37,7 @@ public IActionResult GetErrorResponse(ExceptionContext context)
ContextDetails = _configurationService.GetContextDetails(context.HttpContext)
};

errorResponse?.LogAction?.Invoke(Logger, context.Exception);
context.HttpContext.Response.StatusCode = apiResponse.StatusCode;

return new ObjectResult(apiResponse)
Expand Down
4 changes: 1 addition & 3 deletions ExceptionAll/Services/ContextConfigurationService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using ExceptionAll.Models;

namespace ExceptionAll.Services;
namespace ExceptionAll.Services;

public class ContextConfigurationService : IContextConfigurationService
{
Expand Down
Loading

0 comments on commit 83d632d

Please sign in to comment.