WebApi + ClientApp, GraphQL, Reflection
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
using FluentValidation;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Events.WebAPI.Util.Extensions;
|
||||
using Events.WebAPI.Contract.Command;
|
||||
|
||||
namespace Events.WebAPI.Util.Middleware;
|
||||
|
||||
public class BadRequestOnRuleValidationException : ExceptionFilterAttribute
|
||||
{
|
||||
private readonly ILogger<BadRequestOnRuleValidationException> logger;
|
||||
|
||||
public BadRequestOnRuleValidationException(ILogger<BadRequestOnRuleValidationException> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public override void OnException(ExceptionContext context)
|
||||
{
|
||||
if (context.Exception is ValidationException)
|
||||
{
|
||||
|
||||
string exceptionMessage = context.Exception.CompleteExceptionMessage();
|
||||
logger.LogDebug("Validation error: {0}", exceptionMessage);
|
||||
|
||||
ValidationException exc = (ValidationException)context.Exception;
|
||||
Dictionary<string, List<string>> validationErrors = new Dictionary<string, List<string>>();
|
||||
Dictionary<string, List<string>> validationErrorCodes = new Dictionary<string, List<string>>();
|
||||
|
||||
foreach(var failure in exc.Errors)
|
||||
{
|
||||
//remove prefix Dto. (part of Update and AddCommand)
|
||||
string propertyName = failure.PropertyName.Replace(nameof(AddCommand<object, object>.Dto) + ".", "");
|
||||
if (propertyName == nameof(AddCommand<object, object>.Dto))
|
||||
{
|
||||
propertyName = string.Empty;
|
||||
}
|
||||
|
||||
validationErrors.GetOrCreate(propertyName).Add(failure.ErrorMessage);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(failure.ErrorCode))
|
||||
{
|
||||
validationErrorCodes.GetOrCreate(propertyName).Add(failure.ErrorCode);
|
||||
}
|
||||
}
|
||||
|
||||
var problemDetails = new ValidationProblemDetails(validationErrors.ToDictionary(d => d.Key, d => d.Value.ToArray()))
|
||||
{
|
||||
Detail = context.Exception.Message,
|
||||
Title = "Validation exception",
|
||||
Instance = context.HttpContext.TraceIdentifier
|
||||
};
|
||||
if (validationErrorCodes.Count > 0)
|
||||
{
|
||||
problemDetails.Extensions["errorCodes"] = validationErrorCodes.ToDictionary(d => d.Key, d => d.Value.ToArray());
|
||||
}
|
||||
context.Result = new ObjectResult(problemDetails)
|
||||
{
|
||||
ContentTypes = { "application/problem+json" },
|
||||
StatusCode = StatusCodes.Status400BadRequest
|
||||
};
|
||||
|
||||
context.HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||
context.ExceptionHandled = true;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Events.WebAPI.Util.Extensions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
namespace Events.WebAPI.Util.Middleware;
|
||||
|
||||
public class ProblemDetailsForException : ExceptionFilterAttribute
|
||||
{
|
||||
private readonly ILogger<ProblemDetailsForException> logger;
|
||||
|
||||
public ProblemDetailsForException(ILogger<ProblemDetailsForException> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public override void OnException(ExceptionContext context)
|
||||
{
|
||||
string exceptionMessage = context.Exception.CompleteExceptionMessage();
|
||||
logger.LogError("Error 500: {0}", exceptionMessage); //TO DO: Log data from context.ActionDescriptor?
|
||||
logger.LogError(context.Exception.StackTrace);
|
||||
context.ExceptionHandled = true;
|
||||
var problemDetails = new ProblemDetails
|
||||
{
|
||||
Type = "https://httpstatuses.io/500",
|
||||
Detail = exceptionMessage,
|
||||
Title = "Internal server error",
|
||||
Instance = context.HttpContext.TraceIdentifier
|
||||
};
|
||||
context.Result = new ObjectResult(problemDetails)
|
||||
{
|
||||
ContentTypes = { "application/problem+json" },
|
||||
StatusCode = StatusCodes.Status500InternalServerError
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using Events.WebAPI.Util.Extensions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
|
||||
namespace Events.WebAPI.Util.Middleware;
|
||||
|
||||
public class ProblemDetailsForSqlException : ExceptionFilterAttribute
|
||||
{
|
||||
private readonly ILogger<ProblemDetailsForSqlException> logger;
|
||||
|
||||
public ProblemDetailsForSqlException(ILogger<ProblemDetailsForSqlException> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public override void OnException(ExceptionContext context)
|
||||
{
|
||||
Exception? exception = context.Exception;
|
||||
PostgresException? postgresException = null;
|
||||
|
||||
while (exception is not null)
|
||||
{
|
||||
if (exception is PostgresException currentPostgresException)
|
||||
{
|
||||
postgresException = currentPostgresException;
|
||||
break;
|
||||
}
|
||||
|
||||
if (exception is DbUpdateException dbUpdateException && dbUpdateException.InnerException is not null)
|
||||
{
|
||||
exception = dbUpdateException.InnerException;
|
||||
continue;
|
||||
}
|
||||
|
||||
exception = exception.InnerException;
|
||||
}
|
||||
|
||||
if (postgresException is null)
|
||||
{
|
||||
base.OnException(context);
|
||||
return;
|
||||
}
|
||||
|
||||
ProblemDetails problemDetails = postgresException.SqlState switch
|
||||
{
|
||||
PostgresErrorCodes.UniqueViolation => new ProblemDetails
|
||||
{
|
||||
Title = "Duplicate data",
|
||||
Detail = "A record with the same data already exists."
|
||||
},
|
||||
PostgresErrorCodes.ForeignKeyViolation => new ProblemDetails
|
||||
{
|
||||
Title = "Related data",
|
||||
Detail = "The operation is not allowed because related data exists."
|
||||
},
|
||||
_ => new ProblemDetails
|
||||
{
|
||||
Title = "Database error",
|
||||
Detail = $"An error occurred while saving data to the database. {postgresException.MessageText}"
|
||||
}
|
||||
};
|
||||
|
||||
logger.LogDebug("Database exception: {message}", context.Exception.CompleteExceptionMessage());
|
||||
context.ExceptionHandled = true;
|
||||
context.Result = new ObjectResult(problemDetails)
|
||||
{
|
||||
ContentTypes = { "application/problem+json" },
|
||||
StatusCode = StatusCodes.Status500InternalServerError
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user