| | | 1 | | using Microsoft.AspNetCore.Http.HttpResults; |
| | | 2 | | using System.Reflection; |
| | | 3 | | |
| | | 4 | | namespace Anichron.API.Infrastructure; |
| | | 5 | | |
| | | 6 | | /// <summary>Failures as Problem Details: a stable <c>code</c> extension, and the English text in <c>detail</c>.</summar |
| | | 7 | | internal static class ApiProblems |
| | | 8 | | { |
| | | 9 | | internal const string CodeExtension = "code"; |
| | | 10 | | |
| | | 11 | | internal static IResult Problem(int statusCode, string code, string detail) |
| | | 12 | | => Results.Problem(detail: detail, statusCode: statusCode, extensions: Extensions(code)); |
| | | 13 | | |
| | | 14 | | /// <summary>A failure of one request field, with <paramref name="code"/> also listed under that field in <c>errors< |
| | | 15 | | /// <param name="statusCode">The response status.</param> |
| | | 16 | | /// <param name="code">The failure's code.</param> |
| | | 17 | | /// <param name="field">The request field, as the client names it in JSON.</param> |
| | | 18 | | /// <param name="detail">The English text.</param> |
| | | 19 | | internal static IResult FieldProblem(int statusCode, string code, string field, string detail) |
| | | 20 | | => Results.ValidationProblem(FieldErrors(field, code), detail: detail, statusCode: statusCode, extensions: Exten |
| | | 21 | | |
| | | 22 | | /// <summary>A 400 for one request field, with <paramref name="code"/> also listed under that field in <c>errors</c> |
| | | 23 | | /// <param name="code">The failure's code.</param> |
| | | 24 | | /// <param name="field">The request field, as the client names it.</param> |
| | | 25 | | /// <param name="detail">The English text.</param> |
| | | 26 | | internal static ValidationProblem InvalidField(string code, string field, string detail) |
| | | 27 | | => TypedResults.ValidationProblem(FieldErrors(field, code), detail: detail, extensions: Extensions(code)); |
| | | 28 | | |
| | | 29 | | private static Dictionary<string, string[]> FieldErrors(string field, string code) => new() { [field] = [code] }; |
| | | 30 | | |
| | | 31 | | private static Dictionary<string, object?> Extensions(string code) => new() { [CodeExtension] = code }; |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | internal static class ErrorCodes |
| | | 35 | | { |
| | | 36 | | internal static IEnumerable<string> All() |
| | 1 | 37 | | => typeof(ErrorCodes).GetFields(BindingFlags.Static | BindingFlags.Public) |
| | 1 | 38 | | .Where(constant => constant.IsLiteral) |
| | 1 | 39 | | .Select(constant => constant.GetRawConstantValue()) |
| | 1 | 40 | | .OfType<string>() |
| | 1 | 41 | | .Order(StringComparer.Ordinal); |
| | | 42 | | |
| | | 43 | | public const string InvalidCredentials = "invalid_credentials"; |
| | | 44 | | public const string AccountDisabled = "account_disabled"; |
| | | 45 | | public const string AccountLocked = "account_locked"; |
| | | 46 | | public const string RateLimited = "rate_limited"; |
| | | 47 | | public const string RefreshTokenRequired = "refresh_token_required"; |
| | | 48 | | public const string RefreshTokenInvalid = "refresh_token_invalid"; |
| | | 49 | | public const string InvalidUsername = "invalid_username"; |
| | | 50 | | public const string InvalidEmail = "invalid_email"; |
| | | 51 | | public const string UsernameTaken = "username_taken"; |
| | | 52 | | public const string EmailTaken = "email_taken"; |
| | | 53 | | public const string PasswordTooShort = "password_too_short"; |
| | | 54 | | public const string PasswordTooLong = "password_too_long"; |
| | | 55 | | public const string PasswordPwned = "password_pwned"; |
| | | 56 | | public const string InviteTokenInvalid = "invite_token_invalid"; |
| | | 57 | | public const string PasswordChangeRequired = "password_change_required"; |
| | | 58 | | public const string CannotModifySelf = "cannot_modify_self"; |
| | | 59 | | public const string PathInvalid = "path_invalid"; |
| | | 60 | | public const string PathAlreadyAssigned = "path_already_assigned"; |
| | | 61 | | public const string PathOverlaps = "path_overlaps"; |
| | | 62 | | public const string InvalidDate = "invalid_date"; |
| | | 63 | | public const string InvalidPage = "invalid_page"; |
| | | 64 | | public const string InvalidPageSize = "invalid_page_size"; |
| | | 65 | | public const string InvalidCursor = "invalid_cursor"; |
| | | 66 | | public const string PendingOrOutcome = "pending_or_outcome"; |
| | | 67 | | public const string OrphanIdsOrAll = "orphan_ids_or_all"; |
| | | 68 | | public const string InvalidOrphanCount = "invalid_orphan_count"; |
| | | 69 | | public const string UnknownOrphans = "unknown_orphans"; |
| | | 70 | | public const string ServiceUnavailable = "service_unavailable"; |
| | | 71 | | } |