chg: frontend: routes: errorhandling

This commit is contained in:
Alex
2024-10-09 18:07:28 +02:00
parent c008bcad0b
commit 3b3dc9d251
4 changed files with 19 additions and 28 deletions

View File

@@ -90,7 +90,9 @@ export const actions = {
console.dir(formData);
console.dir(cleanUpdateData);
const apiURL = `${BASE_API_URI}/backend/users/update/`;
const res = await fetch(apiURL, {
/** @type {RequestInit} */
const requestUpdateOptions = {
method: "PATCH",
credentials: "include",
headers: {
@@ -98,11 +100,12 @@ export const actions = {
Cookie: `jwt=${cookies.get("jwt")}`,
},
body: JSON.stringify(cleanUpdateData),
});
};
const res = await fetch(apiURL, requestUpdateOptions);
if (!res.ok) {
const response = await res.json();
const errors = formatError(response.error);
const errors = formatError(response.errors);
return fail(400, { errors: errors });
}
@@ -159,7 +162,7 @@ export const actions = {
if (!res.ok) {
const response = await res.json();
const errors = formatError(response.error);
const errors = formatError(response.errors);
return fail(400, { errors: errors });
}
@@ -195,7 +198,7 @@ export const actions = {
if (!res.ok) {
const response = await res.json();
const errors = formatError(response.error);
const errors = formatError(response.errors);
return fail(400, { errors: errors });
}

View File

@@ -99,7 +99,12 @@
password2 = "";
const open = () => (showModal = true);
const close = () => (showModal = false);
const close = () => {
showModal = false;
if (form) {
form.errors = undefined;
}
};
// const toggleAvatars = () => (showAvatars = !showAvatars);
$: selectedSubscriptionModel =
@@ -222,7 +227,7 @@
in:receive={{ key: error.id }}
out:send={{ key: error.id }}
>
{error.error}
{$t(error.field) + ": " + $t(error.key)}
</h4>
{/each}
{/if}

View File

@@ -44,18 +44,9 @@ export const actions = {
console.log("Login response headers:", Object.fromEntries(res.headers));
if (!res.ok) {
let errorMessage = `HTTP error! status: ${res.status}`;
try {
const errorData = await res.json();
errorMessage = errorData.error || errorMessage;
} catch (parseError) {
console.error("Failed to parse error response:", parseError);
errorMessage = await res.text(); // Get the raw response text if JSON parsing fails
}
console.error("Login failed:", errorMessage);
return fail(res.status, {
errors: [{ error: errorMessage, id: Date.now() }],
});
const errorData = await res.json();
const errors = formatError(errorData.errors);
return fail(res.status, { errors });
}
const responseBody = await res.json();
@@ -81,12 +72,4 @@ export const actions = {
console.log("Redirecting to:", next || "/");
throw redirect(303, next || "/");
},
// if (!res.ok) {
// const response = await res.json();
// const errors = formatError(response.error);
// return fail(400, { errors: errors });
// }
// throw redirect(303, next || "/");
// },
};

View File

@@ -35,7 +35,7 @@
in:receive={{ key: error.id }}
out:send={{ key: error.id }}
>
{error.error}
{$t(error.key)}
</h4>
{/each}
{/if}