Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Or 1949 #543

Merged
merged 8 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@

<ItemGroup>
<EmbeddedResource Include="Contexten\*.json" />
<Compile Update="ValidationMessages.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>ValidationMessages.resx</DependentUpon>
</Compile>
<EmbeddedResource Update="ValidationMessages.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>ValidationMessages.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<Import Project="..\..\.paket\Paket.Restore.targets" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AssociationRegistry.Admin.Api.Infrastructure;
namespace AssociationRegistry.Admin.Api.Infrastructure.ExceptionHandlers;

using Be.Vlaanderen.Basisregisters.Api.Exceptions;
using Be.Vlaanderen.Basisregisters.BasicApiProblem;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AssociationRegistry.Admin.Api.Infrastructure;
namespace AssociationRegistry.Admin.Api.Infrastructure.ExceptionHandlers;

using System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AssociationRegistry.Admin.Api.Infrastructure;
namespace AssociationRegistry.Admin.Api.Infrastructure.ExceptionHandlers;

using Be.Vlaanderen.Basisregisters.Api.Exceptions;
using Be.Vlaanderen.Basisregisters.BasicApiProblem;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace AssociationRegistry.Admin.Api.Infrastructure.ExceptionHandlers;

using Be.Vlaanderen.Basisregisters.Api.Exceptions;
using Be.Vlaanderen.Basisregisters.BasicApiProblem;
using EventStore;
using Microsoft.AspNetCore.Http;

public class UnexpectedAggregateVersionExceptionHandler : DefaultExceptionHandler<UnexpectedAggregateVersionException>
{
private readonly ProblemDetailsHelper _problemDetailsHelper;

public UnexpectedAggregateVersionExceptionHandler(ProblemDetailsHelper problemDetailsHelper)
{
_problemDetailsHelper = problemDetailsHelper;
}

protected override ProblemDetails GetApiProblemFor(UnexpectedAggregateVersionException exception)
=> new()
{
HttpStatus = StatusCodes.Status412PreconditionFailed,
Title = ProblemDetails.DefaultTitle,
Detail = exception.Message,
ProblemTypeUri = _problemDetailsHelper.GetExceptionTypeUriFor(exception),
ProblemInstanceUri = $"{_problemDetailsHelper.GetInstanceBaseUri()}/{ProblemDetails.GetProblemNumber()}",
};
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace AssociationRegistry.Admin.Api.Infrastructure.Extensions;

using ExceptionHandlers;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,32 @@
using System.Threading.Tasks;
using Be.Vlaanderen.Basisregisters.Api.Exceptions;
using Be.Vlaanderen.Basisregisters.AspNetCore.Mvc.Formatters.Json;
using Be.Vlaanderen.Basisregisters.BasicApiProblem;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.NewtonsoftJson;
using Newtonsoft.Json;
using ProblemDetails = Be.Vlaanderen.Basisregisters.BasicApiProblem.ProblemDetails;

public static class HttpResponseExtensions
{
public static async Task WriteProblemDetailsAsync(this HttpResponse response, ProblemDetailsHelper problemDetailsHelper, string problemDetailsMessage)
public static async Task<IActionResult> WriteNotFoundProblemDetailsAsync(
this HttpResponse response,
ProblemDetailsHelper problemDetailsHelper,
string? message = null)
{
response.StatusCode = StatusCodes.Status400BadRequest;
message ??= ValidationMessages.Status404NotFound;
await response.WriteProblemDetailsAsync(problemDetailsHelper, message, StatusCodes.Status404NotFound);
return new EmptyResult();
}

public static async Task WriteProblemDetailsAsync(this HttpResponse response, ProblemDetailsHelper problemDetailsHelper, string problemDetailsMessage, int statusCode = StatusCodes.Status400BadRequest)
{
response.StatusCode = statusCode;
await response.WriteAsync(
JsonConvert.SerializeObject(
new ProblemDetails
{
HttpStatus = StatusCodes.Status400BadRequest,
HttpStatus = statusCode,
Title = ProblemDetails.DefaultTitle,
Detail = problemDetailsMessage,
ProblemTypeUri = "urn:associationregistry.admin.api:validation",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace AssociationRegistry.Admin.Api.Infrastructure.Middleware;

using Be.Vlaanderen.Basisregisters.AggregateSource;
using Be.Vlaanderen.Basisregisters.Api.Exceptions;
using System.Threading.Tasks;
using EventStore;
using Extensions;
using Microsoft.AspNetCore.Http;

public class UnexpectedAggregateVersionMiddleware
Expand All @@ -13,16 +16,15 @@ public UnexpectedAggregateVersionMiddleware(RequestDelegate next)
_next = next;
}

public async Task InvokeAsync(HttpContext context)
public async Task InvokeAsync(HttpContext context, ProblemDetailsHelper problemDetailsHelper)
{
try
{
await _next(context);
}
catch (UnexpectedAggregateVersionException)
catch (UnexpectedAggregateVersionException ex)
{
context.Response.Clear();
context.Response.StatusCode = StatusCodes.Status412PreconditionFailed;
await context.Response.WriteProblemDetailsAsync(problemDetailsHelper, ex.Message, StatusCodes.Status412PreconditionFailed);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace AssociationRegistry.Admin.Api.Infrastructure.Swagger.Examples;

using Be.Vlaanderen.Basisregisters.Api.Exceptions;
using Be.Vlaanderen.Basisregisters.BasicApiProblem;
using Microsoft.AspNetCore.Http;
using Swashbuckle.AspNetCore.Filters;

public class NotFoundProblemDetailsExamples : IExamplesProvider<ProblemDetails>
{
private readonly ProblemDetailsHelper _helper;

public NotFoundProblemDetailsExamples(ProblemDetailsHelper helper)
{
_helper = helper;
}
public ProblemDetails GetExamples()
=> new()
{
HttpStatus = StatusCodes.Status404NotFound,
Title = ProblemDetails.DefaultTitle,
Detail = "De gevraagde vereniging werd niet gevonden.",
ProblemTypeUri = "urn:associationregistry.admin.api:validation",
ProblemInstanceUri = $"{_helper.GetInstanceBaseUri()}/{ProblemDetails.GetProblemNumber()}",
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace AssociationRegistry.Admin.Api.Infrastructure.Swagger.Examples;

using Be.Vlaanderen.Basisregisters.Api.Exceptions;
using Be.Vlaanderen.Basisregisters.BasicApiProblem;
using Microsoft.AspNetCore.Http;
using Swashbuckle.AspNetCore.Filters;

public abstract class PreconditionFailedProblemDetailsExamplesBase : IExamplesProvider<ProblemDetails>
{
private readonly ProblemDetailsHelper _helper;
private readonly string _message;

protected PreconditionFailedProblemDetailsExamplesBase(ProblemDetailsHelper helper, string message)
{
_helper = helper;
_message = message;
}

public ProblemDetails GetExamples()
=> new()
{
HttpStatus = StatusCodes.Status412PreconditionFailed,
Title = ProblemDetails.DefaultTitle,
Detail = _message,
ProblemTypeUri = "urn:associationregistry.admin.api:validation",
ProblemInstanceUri = $"{_helper.GetInstanceBaseUri()}/{ProblemDetails.GetProblemNumber()}",
};
}

public class PreconditionFailedProblemDetailsExamples : PreconditionFailedProblemDetailsExamplesBase
{
public PreconditionFailedProblemDetailsExamples(ProblemDetailsHelper helper) :
base(helper, ValidationMessages.Status412PreconditionFailed)
{
}
}

public class HistoriekPreconditionFailedProblemDetailsExamples : PreconditionFailedProblemDetailsExamplesBase
{
public HistoriekPreconditionFailedProblemDetailsExamples(ProblemDetailsHelper helper) :
base(helper, ValidationMessages.Status412Historiek)
{
}
}

public class DetailPreconditionFailedProblemDetailsExamples : PreconditionFailedProblemDetailsExamplesBase
{
public DetailPreconditionFailedProblemDetailsExamples(ProblemDetailsHelper helper) :
base(helper, ValidationMessages.Status412Detail)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ public ValidationProblemDetails GetExamples()
},
};
}
public class ProblemDetailsExamples : IExamplesProvider<ProblemDetails>

public class BadRequestProblemDetailsExamples : IExamplesProvider<ProblemDetails>
{
private readonly ProblemDetailsHelper _helper;

public ProblemDetailsExamples(ProblemDetailsHelper helper)
public BadRequestProblemDetailsExamples(ProblemDetailsHelper helper)
{
_helper = helper;
}
Expand Down
2 changes: 2 additions & 0 deletions src/AssociationRegistry.Admin.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace AssociationRegistry.Admin.Api;
using Infrastructure;
using Infrastructure.Configuration;
using Infrastructure.ConfigurationBindings;
using Infrastructure.ExceptionHandlers;
using Infrastructure.Extensions;
using Infrastructure.Json;
using Infrastructure.Middleware;
Expand Down Expand Up @@ -204,6 +205,7 @@ private static void ConfigureExceptionHandler(WebApplication app)
{
new BadHttpRequestExceptionHandler(problemDetailsHelper),
new CouldNotParseRequestExceptionHandler(problemDetailsHelper),
new UnexpectedAggregateVersionExceptionHandler(problemDetailsHelper),
new JsonReaderExceptionHandler(problemDetailsHelper),
},
problemDetailsHelper);
Expand Down
Loading