forked from ddrilling/AsbCloudServer
Исключения
Сделал небольшую доработку по исключениям. 1. Создал новое исключение, которое позволяет указывать сразу несколько невалидных параметров. 2. Поправил middleware, сделал обобщённый класс, который преобразует исключение.
This commit is contained in:
parent
b9d60ac6fc
commit
6f05877ac9
@ -1,11 +1,13 @@
|
||||
using System;
|
||||
using AsbCloudApp.Exceptions.Interfaces;
|
||||
|
||||
namespace AsbCloudApp.Exceptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Argument validation fail Exception
|
||||
/// </summary>
|
||||
public class ArgumentInvalidException : Exception
|
||||
public class ArgumentInvalidException : Exception,
|
||||
IHasValidation
|
||||
{
|
||||
/// <summary>
|
||||
/// название аргумента
|
||||
@ -27,7 +29,7 @@ namespace AsbCloudApp.Exceptions
|
||||
/// преобразование в объект валидации
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public object ToValaidationErrorObject()
|
||||
public object ToValidationErrorObject()
|
||||
=> MakeValidationError(ParamName, Message);
|
||||
|
||||
/// <summary>
|
||||
|
47
AsbCloudApp/Exceptions/ArgumentsInvalidException.cs
Normal file
47
AsbCloudApp/Exceptions/ArgumentsInvalidException.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using AsbCloudApp.Exceptions.Interfaces;
|
||||
|
||||
namespace AsbCloudApp.Exceptions;
|
||||
|
||||
/// <summary>
|
||||
/// Èñêëþ÷åíèå äëÿ íåñêîëüêèõ íåâàëèäíûõ àãðóìåíòîâ
|
||||
/// </summary>
|
||||
public class ArgumentsInvalidException : Exception,
|
||||
IHasValidation
|
||||
{
|
||||
/// <summary>
|
||||
/// Èìåíà ïàðàìåòðîâ
|
||||
/// </summary>
|
||||
public string[] ParamNames { get; } = Array.Empty<string>();
|
||||
|
||||
/// <summary>
|
||||
/// Êîíòñðóêòîð èñêëþ÷åíèÿ
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="paramNames"></param>
|
||||
public ArgumentsInvalidException(string message,
|
||||
string[] paramNames)
|
||||
{
|
||||
ParamNames = paramNames;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ïðåîáðàçîâàíèå â îáúåêò âàëèäàöèè
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public object ToValidationErrorObject()
|
||||
=> MakeValidationError(ParamNames, Message);
|
||||
|
||||
/// <summary>
|
||||
/// ôàáðèêà îáúåêòà âàëèäàöèè
|
||||
/// </summary>
|
||||
/// <param name="paramName"></param>
|
||||
/// <param name="errors"></param>
|
||||
/// <returns></returns>
|
||||
public static object MakeValidationError(string[] paramNames, params string[] errors)
|
||||
=> new
|
||||
{
|
||||
names = paramNames,
|
||||
errors,
|
||||
};
|
||||
}
|
13
AsbCloudApp/Exceptions/Interfaces/IHasValidation.cs
Normal file
13
AsbCloudApp/Exceptions/Interfaces/IHasValidation.cs
Normal file
@ -0,0 +1,13 @@
|
||||
namespace AsbCloudApp.Exceptions.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// Èíòåðôåéñ ìàðêåð äëÿ èñêëþ÷åíèé
|
||||
/// </summary>
|
||||
public interface IHasValidation
|
||||
{
|
||||
/// <summary>
|
||||
/// ïðåîáðàçîâàíèå â îáúåêò âàëèäàöèè
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
object ToValidationErrorObject();
|
||||
}
|
@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using AsbCloudApp.Exceptions.Interfaces;
|
||||
|
||||
namespace AsbCloudWebApi.Middlewares
|
||||
{
|
||||
@ -30,6 +31,15 @@ namespace AsbCloudWebApi.Middlewares
|
||||
var body = MakeJsonBody(ex);
|
||||
await context.Response.WriteAsync(body);
|
||||
}
|
||||
catch (ArgumentsInvalidException ex)
|
||||
{
|
||||
Console.WriteLine($"ArgumentExceptions in {context.Request.Method}: {ex.Message}");
|
||||
context.Response.Clear();
|
||||
context.Response.StatusCode = 400;
|
||||
context.Response.ContentType = "application/json";
|
||||
var body = MakeJsonBody(ex);
|
||||
await context.Response.WriteAsync(body);
|
||||
}
|
||||
catch (ForbidException ex)
|
||||
{
|
||||
Console.WriteLine($"ForbidException in {context.Request.Method}: {ex.Message}");
|
||||
@ -49,9 +59,10 @@ namespace AsbCloudWebApi.Middlewares
|
||||
}
|
||||
}
|
||||
|
||||
private static string MakeJsonBody(ArgumentInvalidException ex)
|
||||
private static string MakeJsonBody<TException>(TException ex)
|
||||
where TException: Exception, IHasValidation
|
||||
{
|
||||
object error = ex.ToValaidationErrorObject();
|
||||
object error = ex.ToValidationErrorObject();
|
||||
var buffer = System.Text.Json.JsonSerializer.Serialize(error);
|
||||
return buffer;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user