Skip to content

Commit

Permalink
Fix the handling of validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
BurntimeX committed Jun 8, 2024
1 parent 235c802 commit fbefc10
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
24 changes: 15 additions & 9 deletions ts/WoltLabSuite/Core/Api/Result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ export function apiResultFromValue<T>(value: T): ApiResult<T> {
};
}

export function apiResultFromError(error: ApiError): ApiResult<never> {
return {
ok: false,
error,
unwrap() {
throw error;
},
};
export async function apiResultFromError(error: unknown): Promise<ApiResult<never>> {
if (error instanceof StatusNotOk) {
return apiResultFromStatusNotOk(error);
}

throw error;
}

export async function apiResultFromStatusNotOk(e: StatusNotOk): Promise<ApiResult<never>> {
Expand Down Expand Up @@ -73,7 +71,15 @@ 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, response.status));
const apiError = new ApiError(json.type, json.code, json.message, json.param, response.status);

return {
ok: false,
error: apiError,
unwrap() {
throw apiError;
},
};
}

throw e;
Expand Down
24 changes: 14 additions & 10 deletions wcfsetup/install/files/js/WoltLabSuite/Core/Api/Result.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fbefc10

Please sign in to comment.