summaryrefslogtreecommitdiff
path: root/ExceptionMiddleware.cs
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2026-05-22 12:46:00 +1000
committerJake Mannens <jake@asger.xyz>2026-05-23 22:13:00 +1000
commit4ea3ddb38d010c2f85c22b7f1c3f2d7e0c1355e3 (patch)
tree90af9203059d645eb77216f1a091722ee9702438 /ExceptionMiddleware.cs
parent6de5d7f5364fe1d54703da6d6b7cb08ea26e939f (diff)
Initial commitwasm-oldserver
Diffstat (limited to 'ExceptionMiddleware.cs')
-rw-r--r--ExceptionMiddleware.cs64
1 files changed, 0 insertions, 64 deletions
diff --git a/ExceptionMiddleware.cs b/ExceptionMiddleware.cs
deleted file mode 100644
index 29d0e10..0000000
--- a/ExceptionMiddleware.cs
+++ /dev/null
@@ -1,64 +0,0 @@
-using HyperBooru.ApiModels;
-using System.Reflection;
-using System.Text.Json;
-using System.Text.Json.Serialization;
-using System.Text.Json.Serialization.Metadata;
-
-namespace HyperBooru;
-
-// Middleware class to intercept API controller exceptions and
-// return said exceptions to API clients as serialized JSON objects
-public sealed class ExceptionMiddleware {
- private RequestDelegate next;
-
- public ExceptionMiddleware(RequestDelegate next) =>
- this.next = next;
-
- public async Task Invoke(HttpContext context) {
- try {
- await next(context);
- } catch(HBException e) {
- context.Response.ContentType = "application/json";
- context.Response.StatusCode =
- e.GetType().GetCustomAttribute<ExceptionStatusCodeAttribute>()?.StatusCode ??
- StatusCodes.Status500InternalServerError;
-
- await context.Response.WriteAsJsonAsync(e);
-
- var x = 1;
- } catch(Exception) {
- context.Response.StatusCode = StatusCodes.Status500InternalServerError;
- context.Response.ContentType = "application/json";
-
- context.Response.Clear();
-
- await context.Response.WriteAsync(string.Empty);
- }
- }
-}
-
-// This class is needed as the JSON serializer often fails to serialize
-// members of the native 'Exception' class
-public sealed class ExceptionJsonResolver : DefaultJsonTypeInfoResolver {
- public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options) {
- var info = base.GetTypeInfo(type, options);
-
- if(!typeof(Exception).IsAssignableFrom(type))
- return info;
-
- string[] excludedProps = [
- "data",
- "hResult",
- "helpLink",
- "innerException",
- "source",
- "stackTrace",
- "targetSite"
- ];
-
- foreach(var p in info.Properties.Where(p => excludedProps.Contains(p.Name)))
- p.ShouldSerialize = (_, _) => false;
-
- return info;
- }
-}