revert fb8e77bb68
Local Build and Deploy / deploy (push) Successful in 1m11s
Local Build and Deploy / deploy (push) Successful in 1m11s
revert style: clean up code with prettier and eslint
This commit is contained in:
1 parent
27de078f54
commit
e85e4d74ea
378 files changed
+20710
-22428
No files matched your search
@@ -1,26 +1,27 @@
|
||||
import { ZodError } from "zod";
|
||||
import { ZodError } from 'zod'
|
||||
|
||||
// ── Action Result Type ───────────────────────────────────────────────
|
||||
|
||||
export type ActionResult<T = Record<string, unknown>> =
|
||||
{ ok: true; data?: T } | { ok: false; error: string; fieldErrors?: Record<string, string[]> };
|
||||
| { ok: true; data?: T }
|
||||
| { ok: false; error: string; fieldErrors?: Record<string, string[]> }
|
||||
|
||||
// ── Helper to build results ──────────────────────────────────────────
|
||||
|
||||
export function actionOk<T = Record<string, unknown>>(data?: T): ActionResult<T> {
|
||||
return { ok: true, data: data ?? ({} as T) };
|
||||
return { ok: true, data: data ?? ({} as T) }
|
||||
}
|
||||
|
||||
export function actionError(message: string): ActionResult<never> {
|
||||
return { ok: false, error: message };
|
||||
return { ok: false, error: message }
|
||||
}
|
||||
|
||||
// ── Throwable error ──────────────────────────────────────────────────
|
||||
|
||||
export class ActionError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = "ActionError";
|
||||
super(message)
|
||||
this.name = 'ActionError'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,21 +31,21 @@ export function handleActionError(error: unknown): ActionResult<never> {
|
||||
if (error instanceof ZodError) {
|
||||
return {
|
||||
ok: false,
|
||||
error: "Validation failed",
|
||||
error: 'Validation failed',
|
||||
fieldErrors: error.flatten().fieldErrors as Record<string, string[]>,
|
||||
};
|
||||
}
|
||||
}
|
||||
if (error instanceof Error && error.name === "ActionError") {
|
||||
return { ok: false, error: error.message };
|
||||
if (error instanceof Error && error.name === 'ActionError') {
|
||||
return { ok: false, error: error.message }
|
||||
}
|
||||
// Prisma P2025
|
||||
if (
|
||||
error instanceof Error &&
|
||||
error.constructor.name === "PrismaClientKnownRequestError" &&
|
||||
(error as Error & { code?: string }).code === "P2025"
|
||||
error.constructor.name === 'PrismaClientKnownRequestError' &&
|
||||
(error as Error & { code?: string }).code === 'P2025'
|
||||
) {
|
||||
return { ok: false, error: "Not found" };
|
||||
return { ok: false, error: 'Not found' }
|
||||
}
|
||||
console.error("[Action error]", error);
|
||||
return { ok: false, error: "Internal server error" };
|
||||
console.error('[Action error]', error)
|
||||
return { ok: false, error: 'Internal server error' }
|
||||
}
|
||||
Reference in new issue
Block a user