2024-05-28 15:29:18 +02:00
|
|
|
using System.Net;
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
2024-07-14 16:44:41 +02:00
|
|
|
using Newtonsoft.Json;
|
2024-05-28 15:29:18 +02:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
|
|
namespace Foxnouns.Backend;
|
|
|
|
|
|
|
|
public class FoxnounsError(string message, Exception? inner = null) : Exception(message)
|
|
|
|
{
|
|
|
|
public Exception? Inner => inner;
|
|
|
|
|
|
|
|
public class DatabaseError(string message, Exception? inner = null) : FoxnounsError(message, inner);
|
|
|
|
|
|
|
|
public class UnknownEntityError(Type entityType, Exception? inner = null)
|
|
|
|
: DatabaseError($"Entity of type {entityType.Name} not found", inner);
|
|
|
|
}
|
|
|
|
|
2024-05-28 17:09:50 +02:00
|
|
|
public class ApiError(string message, HttpStatusCode? statusCode = null, ErrorCode? errorCode = null)
|
|
|
|
: FoxnounsError(message)
|
2024-05-28 15:29:18 +02:00
|
|
|
{
|
|
|
|
public readonly HttpStatusCode StatusCode = statusCode ?? HttpStatusCode.InternalServerError;
|
2024-05-28 17:09:50 +02:00
|
|
|
public readonly ErrorCode ErrorCode = errorCode ?? ErrorCode.InternalServerError;
|
2024-05-28 15:29:18 +02:00
|
|
|
|
2024-09-05 21:10:45 +02:00
|
|
|
public class Unauthorized(string message, ErrorCode errorCode = ErrorCode.AuthenticationError) : ApiError(message,
|
|
|
|
statusCode: HttpStatusCode.Unauthorized,
|
|
|
|
errorCode: errorCode);
|
|
|
|
|
|
|
|
public class Forbidden(
|
|
|
|
string message,
|
|
|
|
IEnumerable<string>? scopes = null,
|
|
|
|
ErrorCode errorCode = ErrorCode.Forbidden)
|
|
|
|
: ApiError(message, statusCode: HttpStatusCode.Forbidden, errorCode: errorCode)
|
2024-05-28 15:29:18 +02:00
|
|
|
{
|
|
|
|
public readonly string[] Scopes = scopes?.ToArray() ?? [];
|
|
|
|
}
|
|
|
|
|
2024-07-14 16:44:41 +02:00
|
|
|
public class BadRequest(string message, IReadOnlyDictionary<string, IEnumerable<ValidationError>>? errors = null)
|
2024-07-12 17:12:24 +02:00
|
|
|
: ApiError(message, statusCode: HttpStatusCode.BadRequest)
|
|
|
|
{
|
2024-07-14 16:44:41 +02:00
|
|
|
public BadRequest(string message, string field, object actualValue) : this("Error validating input",
|
|
|
|
new Dictionary<string, IEnumerable<ValidationError>>
|
|
|
|
{ { field, [ValidationError.GenericValidationError(message, actualValue)] } })
|
2024-07-12 17:12:24 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public JObject ToJson()
|
|
|
|
{
|
|
|
|
var o = new JObject
|
|
|
|
{
|
|
|
|
{ "status", (int)HttpStatusCode.BadRequest },
|
|
|
|
{ "message", Message },
|
2024-09-14 16:37:52 +02:00
|
|
|
{ "code", "BAD_REQUEST" }
|
2024-07-12 17:12:24 +02:00
|
|
|
};
|
|
|
|
if (errors == null) return o;
|
|
|
|
|
|
|
|
var a = new JArray();
|
|
|
|
foreach (var error in errors)
|
|
|
|
{
|
|
|
|
var errorObj = new JObject
|
|
|
|
{
|
|
|
|
{ "key", error.Key },
|
2024-07-14 16:44:41 +02:00
|
|
|
{ "errors", JArray.FromObject(error.Value) }
|
2024-07-12 17:12:24 +02:00
|
|
|
};
|
|
|
|
a.Add(errorObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
o.Add("errors", a);
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A special version of BadRequest that ASP.NET generates when it encounters an invalid request.
|
|
|
|
/// Any other methods should use <see cref="ApiError.BadRequest" /> instead.
|
|
|
|
/// </summary>
|
|
|
|
public class AspBadRequest(string message, ModelStateDictionary? modelState = null)
|
2024-05-28 15:29:18 +02:00
|
|
|
: ApiError(message, statusCode: HttpStatusCode.BadRequest)
|
|
|
|
{
|
|
|
|
public JObject ToJson()
|
|
|
|
{
|
|
|
|
var o = new JObject
|
|
|
|
{
|
|
|
|
{ "status", (int)HttpStatusCode.BadRequest },
|
2024-07-12 17:12:24 +02:00
|
|
|
{ "message", Message },
|
2024-09-14 16:37:52 +02:00
|
|
|
{ "code", "BAD_REQUEST" }
|
2024-05-28 15:29:18 +02:00
|
|
|
};
|
2024-05-28 17:09:50 +02:00
|
|
|
if (modelState == null) return o;
|
2024-05-28 15:29:18 +02:00
|
|
|
|
|
|
|
var a = new JArray();
|
2024-05-28 17:09:50 +02:00
|
|
|
foreach (var error in modelState.Where(e => e.Value is { Errors.Count: > 0 }))
|
2024-05-28 15:29:18 +02:00
|
|
|
{
|
|
|
|
var errorObj = new JObject
|
|
|
|
{
|
|
|
|
{ "key", error.Key },
|
|
|
|
{
|
|
|
|
"errors",
|
|
|
|
new JArray(error.Value!.Errors.Select(e => new JObject { { "message", e.ErrorMessage } }))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
a.Add(errorObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
o.Add("errors", a);
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-28 17:09:50 +02:00
|
|
|
public class NotFound(string message, ErrorCode? code = null)
|
|
|
|
: ApiError(message, statusCode: HttpStatusCode.NotFound, errorCode: code);
|
2024-05-28 15:29:18 +02:00
|
|
|
|
|
|
|
public class AuthenticationError(string message) : ApiError(message, statusCode: HttpStatusCode.BadRequest);
|
2024-05-28 17:09:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public enum ErrorCode
|
|
|
|
{
|
|
|
|
InternalServerError,
|
|
|
|
Forbidden,
|
|
|
|
BadRequest,
|
|
|
|
AuthenticationError,
|
2024-09-05 21:10:45 +02:00
|
|
|
AuthenticationRequired,
|
|
|
|
MissingScopes,
|
2024-05-28 17:09:50 +02:00
|
|
|
GenericApiError,
|
|
|
|
UserNotFound,
|
2024-05-30 16:59:40 +02:00
|
|
|
MemberNotFound,
|
2024-07-14 16:44:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public class ValidationError
|
|
|
|
{
|
|
|
|
public required string Message { get; init; }
|
|
|
|
|
|
|
|
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
|
|
|
public int? MinLength { get; init; }
|
|
|
|
|
|
|
|
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
|
|
|
public int? MaxLength { get; init; }
|
|
|
|
|
|
|
|
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
|
|
|
public int? ActualLength { get; init; }
|
|
|
|
|
|
|
|
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
|
|
|
public IEnumerable<object>? AllowedValues { get; init; }
|
|
|
|
|
|
|
|
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
|
|
|
public object? ActualValue { get; init; }
|
|
|
|
|
|
|
|
public static ValidationError LengthError(string message, int minLength, int maxLength, int actualLength)
|
|
|
|
{
|
|
|
|
return new ValidationError
|
|
|
|
{
|
|
|
|
Message = message,
|
|
|
|
MinLength = minLength,
|
|
|
|
MaxLength = maxLength,
|
|
|
|
ActualLength = actualLength
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ValidationError DisallowedValueError(string message, IEnumerable<object> allowedValues,
|
|
|
|
object actualValue)
|
|
|
|
{
|
|
|
|
return new ValidationError
|
|
|
|
{
|
|
|
|
Message = message,
|
|
|
|
AllowedValues = allowedValues,
|
|
|
|
ActualValue = actualValue
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ValidationError GenericValidationError(string message, object? actualValue)
|
|
|
|
{
|
|
|
|
return new ValidationError
|
|
|
|
{
|
|
|
|
Message = message,
|
|
|
|
ActualValue = actualValue
|
|
|
|
};
|
|
|
|
}
|
2024-05-28 15:29:18 +02:00
|
|
|
}
|