Skip to content
This repository was archived by the owner on Oct 4, 2024. It is now read-only.
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
6 changes: 6 additions & 0 deletions CustomLibrary.ProblemDetails.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomLibrary.ProblemDetails", "src\CustomLibrary.ProblemDetails\CustomLibrary.ProblemDetails.csproj", "{7C1C6714-C223-4AAB-8907-880DDB971725}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomLibrary.ProblemDetails.Sample", "src\CustomLibrary.ProblemDetails.Sample\CustomLibrary.ProblemDetails.Sample.csproj", "{D2C69704-5854-47E9-86A2-496AE61809CA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{7C1C6714-C223-4AAB-8907-880DDB971725}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C1C6714-C223-4AAB-8907-880DDB971725}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C1C6714-C223-4AAB-8907-880DDB971725}.Release|Any CPU.Build.0 = Release|Any CPU
{D2C69704-5854-47E9-86A2-496AE61809CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D2C69704-5854-47E9-86A2-496AE61809CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2C69704-5854-47E9-86A2-496AE61809CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2C69704-5854-47E9-86A2-496AE61809CA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
168 changes: 168 additions & 0 deletions src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
using System.Net.Mime;
using Microsoft.AspNetCore.Mvc;

namespace CustomLibrary.ProblemDetails.Sample.Controllers;

[ApiController]
[Route("api/[controller]")]
[Produces(MediaTypeNames.Application.Json)]
public class TestController : ControllerBase
{
public TestController()
{
}

[HttpGet("NotModified-Exception")]
public async Task<IActionResult> GetExceptionNotModifiedAsync()
{
try
{
await Task.Delay(500);
throw new Exception.NotModifiedException("Not Modified");
}
catch (Exception.NotModifiedException exc)
{
return ResponseException.NotModified(HttpContext, exc);
}
}

[HttpGet("BadRequest-Exception")]
public async Task<IActionResult> GetExceptionBadRequestAsync()
{
try
{
await Task.Delay(500);
throw new Exception.BadRequestException("Bad Request");
}
catch (Exception.BadRequestException exc)
{
return ResponseException.BadRequest(HttpContext, exc);
}
}

[HttpGet("Unauthorized-Exception")]
public async Task<IActionResult> GetExceptionUnauthorizedAsync()
{
try
{
await Task.Delay(500);
throw new Exception.UnauthorizedException("Unauthorized");
}
catch (Exception.UnauthorizedException exc)
{
return ResponseException.Unauthorized(HttpContext, exc);
}
}

[HttpGet("Forbidden-Exception")]
public async Task<IActionResult> GetExceptionForbiddenAsync()
{
try
{
await Task.Delay(500);
throw new Exception.ForbiddenException("Forbidden");
}
catch (Exception.ForbiddenException exc)
{
return ResponseException.Forbidden(HttpContext, exc);
}
}

[HttpGet("NotFound-Exception")]
public async Task<IActionResult> GetExceptionNotFoundAsync()
{
try
{
await Task.Delay(500);
throw new Exception.NotFoundException("Not Found");
}
catch (Exception.NotFoundException exc)
{
return ResponseException.NotFound(HttpContext, exc);
}
}

[HttpGet("MethodNotAllowed-Exception")]
public async Task<IActionResult> GetExceptionMethodNotAllowedAsync()
{
try
{
await Task.Delay(500);
throw new Exception.NotAllowedException("Method Not Allowed");
}
catch (Exception.NotAllowedException exc)
{
return ResponseException.MethodNotAllowed(HttpContext, exc);
}
}

[HttpGet("NotAcceptable-Exception")]
public async Task<IActionResult> GetExceptionNotAcceptableAsync()
{
try
{
await Task.Delay(500);
throw new Exception.NotAcceptableException("Not Acceptable");
}
catch (Exception.NotAcceptableException exc)
{
return ResponseException.NotAcceptable(HttpContext, exc);
}
}

[HttpGet("RequestTimeout-Exception")]
public async Task<IActionResult> GetExceptionRequestTimeoutAsync()
{
try
{
await Task.Delay(500);
throw new Exception.RequestTimeoutException("Request Timeout");
}
catch (Exception.RequestTimeoutException exc)
{
return ResponseException.RequestTimeout(HttpContext, exc);
}
}

[HttpGet("Conflict-Exception")]
public async Task<IActionResult> GetExceptionConflictAsync()
{
try
{
await Task.Delay(500);
throw new Exception.ConflictException("Conflict");
}
catch (Exception.ConflictException exc)
{
return ResponseException.Conflict(HttpContext, exc);
}
}

[HttpGet("UnprocessableEntity-Exception")]
public async Task<IActionResult> GetExceptionUnprocessableEntityAsync()
{
try
{
await Task.Delay(500);
throw new Exception.UnprocessableEntityException("Unprocessable Entity");
}
catch (Exception.UnprocessableEntityException exc)
{
return ResponseException.UnprocessableEntity(HttpContext, exc);
}
}

[HttpGet("InternalServerError-Exception")]
public async Task<IActionResult> GetExceptionInternalServerErrorAsync()
{
try
{
await Task.Delay(500);
throw new Exception.InternalServerErrorException("Internal Server Error");
}
catch (Exception.InternalServerErrorException exc)
{
return ResponseException.InternalServerError(HttpContext, exc);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CustomLibrary.ProblemDetails\CustomLibrary.ProblemDetails.csproj" />
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions src/CustomLibrary.ProblemDetails.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace CustomLibrary.ProblemDetails.Sample;

public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

Startup startup = new(builder.Configuration);

startup.ConfigureServices(builder.Services);

var app = builder.Build();

startup.Configure(app);

app.Run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:36483",
"sslPort": 0
}
},
"profiles": {
"CustomLibrary.ProblemDetails.Sample": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5082",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
62 changes: 62 additions & 0 deletions src/CustomLibrary.ProblemDetails.Sample/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Hellang.Middleware.ProblemDetails;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.OpenApi.Models;

namespace CustomLibrary.ProblemDetails.Sample;

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services
.AddEndpointsApiExplorer()
.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Sample",
Version = "v1",
});
});

services.AddProblemDetails();
services.AddCors(options =>
{
options.AddPolicy("Sample", policy =>
{
policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
});
});

services.Configure<KestrelServerOptions>(Configuration.GetSection("Kestrel"));
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
}

public void Configure(WebApplication app)
{
IWebHostEnvironment env = app.Environment;

app.UseProblemDetails();
app.UseCors("Sample");

app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Sample v1");
});

app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions src/CustomLibrary.ProblemDetails.Sample/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}