mirror of
https://litchi.icu/ngc2207/judge4c-demo.git
synced 2025-05-18 19:36:48 +00:00
feat: 添加代码片段创建功能的错误处理,新增错误页面组件
This commit is contained in:
parent
a817f718d5
commit
8e869954f3
@ -22,6 +22,7 @@ export async function createSnippet(
|
|||||||
formState: { message: string },
|
formState: { message: string },
|
||||||
formData: FormData
|
formData: FormData
|
||||||
) {
|
) {
|
||||||
|
try {
|
||||||
// Check the user's inputs and make sure they're valid
|
// Check the user's inputs and make sure they're valid
|
||||||
const title = formData.get("title");
|
const title = formData.get("title");
|
||||||
const code = formData.get("code");
|
const code = formData.get("code");
|
||||||
@ -36,13 +37,23 @@ export async function createSnippet(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
// Create a new record in the database
|
// Create a new record in the database
|
||||||
const snippet = await db.snippet.create({
|
await db.snippet.create({
|
||||||
data: {
|
data: {
|
||||||
title: title,
|
title: title,
|
||||||
code: code,
|
code: code,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
console.log(snippet);
|
} catch (err: unknown) {
|
||||||
|
if (err instanceof Error) {
|
||||||
|
return {
|
||||||
|
message: err.message,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
message: "Something went wrong...",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
// Redirect the user back to the root route
|
// Redirect the user back to the root route
|
||||||
redirect("/");
|
redirect("/");
|
||||||
}
|
}
|
||||||
|
10
src/app/snippets/new/error.tsx
Normal file
10
src/app/snippets/new/error.tsx
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
interface ErrorPageProps {
|
||||||
|
error: Error;
|
||||||
|
reset: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function ErrorPage({ error }: ErrorPageProps) {
|
||||||
|
return <div>{error.message}</div>;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user