Add a helper method to identify validation errors
authorAlexander Ebert <ebert@woltlab.com>
Sun, 17 Mar 2024 15:24:07 +0000 (16:24 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 17 Mar 2024 15:24:07 +0000 (16:24 +0100)
ts/WoltLabSuite/Core/Api/Error.ts
ts/WoltLabSuite/Core/Api/Result.ts

index b09ba2697eeb853ff50800a0f1aabe290c5e8d99..36078cbddfa1340931b2d162c2317dddb727ddb5 100644 (file)
@@ -6,5 +6,22 @@ export class ApiError {
     public readonly code: string,
     public readonly message: string,
     public readonly param: string,
+    public readonly statusCode: number,
+  ) {}
+
+  getValidationError(): ValidationError | undefined {
+    if (this.type !== "invalid_request_error" || this.statusCode !== 400) {
+      return undefined;
+    }
+
+    return new ValidationError(this.code, this.message, this.param);
+  }
+}
+
+class ValidationError {
+  constructor(
+    public readonly code: string,
+    public readonly message: string,
+    public readonly param: string,
   ) {}
 }
index 138e1990f1adad4c2ad2f801a2bd65ff69ef3b46..92fb7b3b6748da511e12a3bcb255202fd52d88f0 100644 (file)
@@ -62,7 +62,7 @@ export async function apiResultFromStatusNotOk(e: StatusNotOk): Promise<ApiResul
     typeof json.message === "string" &&
     typeof json.param === "string"
   ) {
-    return apiResultFromError(new ApiError(json.type, json.code, json.message, json.param));
+    return apiResultFromError(new ApiError(json.type, json.code, json.message, json.param, response.status));
   }
 
   throw e;