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

View File

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

View File

@@ -44,18 +44,9 @@ export const actions = {
console.log("Login response headers:", Object.fromEntries(res.headers)); console.log("Login response headers:", Object.fromEntries(res.headers));
if (!res.ok) { if (!res.ok) {
let errorMessage = `HTTP error! status: ${res.status}`; const errorData = await res.json();
try { const errors = formatError(errorData.errors);
const errorData = await res.json(); return fail(res.status, { errors });
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 responseBody = await res.json(); const responseBody = await res.json();
@@ -81,12 +72,4 @@ export const actions = {
console.log("Redirecting to:", next || "/"); console.log("Redirecting to:", next || "/");
throw redirect(303, 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 }} in:receive={{ key: error.id }}
out:send={{ key: error.id }} out:send={{ key: error.id }}
> >
{error.error} {$t(error.key)}
</h4> </h4>
{/each} {/each}
{/if} {/if}