Files
web/actions/editProfile.ts
Michael Zetterberg 71dcf30719 fix: make sure all logged errors are preceeded with a message
Just logging an error makes it difficult to relate the error log to code in the
codebase. Error logging a message right before the error itself makes it easier
to search the codebase for that error log.
2024-07-24 11:32:15 +02:00

65 lines
1.7 KiB
TypeScript

"use server"
import { ZodError } from "zod"
import { editProfileSchema } from "@/components/Forms/Edit/Profile/schema"
import { type State, Status } from "@/types/components/myPages/myProfile/edit"
export async function editProfile(_prevState: State, values: FormData) {
try {
const data: Record<string, any> = Object.fromEntries(values.entries())
/**
* TODO: Update profile data when endpoint from
* API team is ready
*/
console.info(`Raw Data`)
console.log(data)
data.address = {
city: data["address.city"],
countryCode: data["address.countryCode"],
streetAddress: data["address.streetAddress"],
zipCode: data["address.zipCode"],
}
const parsedData = editProfileSchema.safeParse(data)
if (parsedData.success) {
console.info(`Success`)
console.log(parsedData.data)
return {
message: "All good!",
status: Status.success,
}
} else {
console.error("Error parsing edit profile")
console.error(parsedData.error)
return {
message: "Invalid data, parse failed!",
status: Status.error,
}
}
} catch (error) {
if (error instanceof ZodError) {
console.error(`ZodError handling profile edit`)
console.error(error)
return {
errors: error.issues.map((issue) => ({
message: `Server validation: ${issue.message}`,
path: issue.path.join("."),
})),
message: "Invalid form data",
status: Status.error,
}
}
console.error(`EditProfile Server Action Error`)
console.error(error)
return {
message: "Something went wrong. Please try again.",
status: Status.error,
}
}
}