|
| 1 | +// <auto-generated /> |
| 2 | +#nullable enable |
| 3 | + |
| 4 | +using Microsoft.AspNetCore.Http; |
| 5 | + |
| 6 | +namespace CarpaNet.AspNetCore; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// ATProtocol XRPC error result implementing <see cref="IResult"/>. |
| 10 | +/// </summary> |
| 11 | +public sealed class ATErrorResult : IResult |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Gets the HTTP status code. |
| 15 | + /// </summary> |
| 16 | + public int StatusCode { get; private init; } |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Gets the error type identifier. |
| 20 | + /// </summary> |
| 21 | + public string? Error { get; private init; } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Gets the human-readable error message. |
| 25 | + /// </summary> |
| 26 | + public string? Message { get; private init; } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Creates a Bad Request (400) error. |
| 30 | + /// </summary> |
| 31 | + public static ATErrorResult BadRequest(string? message = null) |
| 32 | + => new() { StatusCode = 400, Error = "BadRequest", Message = message ?? "Bad Request" }; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Creates an Unauthorized (401) error. |
| 36 | + /// </summary> |
| 37 | + public static ATErrorResult Unauthorized(string? message = null) |
| 38 | + => new() { StatusCode = 401, Error = "AuthMissing", Message = message ?? "Authentication Required" }; |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Creates a Forbidden (403) error. |
| 42 | + /// </summary> |
| 43 | + public static ATErrorResult Forbidden(string? message = null) |
| 44 | + => new() { StatusCode = 403, Error = "Forbidden", Message = message ?? "Forbidden" }; |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Creates a Not Found (404) error. |
| 48 | + /// </summary> |
| 49 | + public static ATErrorResult NotFound(string? message = null) |
| 50 | + => new() { StatusCode = 404, Error = "NotFound", Message = message ?? "Not Found" }; |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Creates a Payload Too Large (413) error. |
| 54 | + /// </summary> |
| 55 | + public static ATErrorResult PayloadTooLarge(string? message = null) |
| 56 | + => new() { StatusCode = 413, Error = "PayloadTooLarge", Message = message ?? "Payload Too Large" }; |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Creates a Too Many Requests (429) error. |
| 60 | + /// </summary> |
| 61 | + public static ATErrorResult TooManyRequests(string? message = null) |
| 62 | + => new() { StatusCode = 429, Error = "TooManyRequests", Message = message ?? "Too Many Requests" }; |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Creates an Internal Server Error (500) error. |
| 66 | + /// </summary> |
| 67 | + public static ATErrorResult InternalServerError(string? message = null) |
| 68 | + => new() { StatusCode = 500, Error = "InternalServerError", Message = message ?? "Internal Server Error" }; |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Creates an Internal Server Error (500) from an exception. |
| 72 | + /// </summary> |
| 73 | + public static ATErrorResult InternalServerError(Exception exception) |
| 74 | + => new() { StatusCode = 500, Error = "InternalServerError", Message = exception.Message }; |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Creates a Not Implemented (501) error. |
| 78 | + /// </summary> |
| 79 | + public static ATErrorResult NotImplemented(string? message = null) |
| 80 | + => new() { StatusCode = 501, Error = "NotImplemented", Message = message ?? "Not Implemented" }; |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Creates a Bad Gateway (502) error. |
| 84 | + /// </summary> |
| 85 | + public static ATErrorResult BadGateway(string? message = null) |
| 86 | + => new() { StatusCode = 502, Error = "BadGateway", Message = message ?? "Bad Gateway" }; |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Creates a Service Unavailable (503) error. |
| 90 | + /// </summary> |
| 91 | + public static ATErrorResult ServiceUnavailable(string? message = null) |
| 92 | + => new() { StatusCode = 503, Error = "ServiceUnavailable", Message = message ?? "Service Unavailable" }; |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Creates a Gateway Timeout (504) error. |
| 96 | + /// </summary> |
| 97 | + public static ATErrorResult GatewayTimeout(string? message = null) |
| 98 | + => new() { StatusCode = 504, Error = "GatewayTimeout", Message = message ?? "Gateway Timeout" }; |
| 99 | + |
| 100 | + /// <inheritdoc/> |
| 101 | + public Task ExecuteAsync(HttpContext httpContext) |
| 102 | + { |
| 103 | + httpContext.Response.StatusCode = StatusCode; |
| 104 | + httpContext.Response.ContentType = "application/json"; |
| 105 | + var error = new XrpcError |
| 106 | + { |
| 107 | + Error = this.Error, |
| 108 | + Message = this.Message, |
| 109 | + }; |
| 110 | + return httpContext.Response.WriteAsync(error.ToJson()); |
| 111 | + } |
| 112 | +} |
0 commit comments