Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix fork errors not showing #552

Merged
merged 9 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions fake-snippets-api/routes/api/snippets/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default withRouteSpec({
}),
jsonResponse: z.object({
ok: z.boolean(),
snippet: snippetSchema,
snippet: snippetSchema.optional(),
}),
})(async (req, ctx) => {
let {
Expand All @@ -28,9 +28,24 @@ export default withRouteSpec({
circuit_json,
dts,
} = req.jsonBody

if (!unscoped_name) {
unscoped_name = `untitled-${snippet_type}-${ctx.db.idCounter + 1}`
}

const existingSnippet = ctx.db.snippets.find(
(snippet) =>
snippet.unscoped_name === unscoped_name &&
snippet.owner_name === ctx.auth.github_username,
)

if (existingSnippet) {
return ctx.error(400, {
error_code: "snippet_already_exists",
message: "You have already forked this snippet in your account.",
})
}

const newSnippet: z.input<typeof snippetSchema> = {
snippet_id: `snippet_${ctx.db.idCounter + 1}`,
name: `${ctx.auth.github_username}/${unscoped_name}`,
Expand All @@ -46,7 +61,14 @@ export default withRouteSpec({
dts,
}

ctx.db.addSnippet(newSnippet)
try {
ctx.db.addSnippet(newSnippet)
} catch (error) {
return ctx.error(500, {
error_code: "snippet_creation_failed",
message: `Failed to create snippet: ${error}`,
})
}

return ctx.json({
ok: true,
Expand Down
21 changes: 21 additions & 0 deletions src/components/ViewSnippetHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export default function ViewSnippetHeader() {
owner_name: session.github_username,
code: snippet.code,
})

if (!data.ok) {
throw new Error(
data.error || "Unknown error occurred while forking snippet.",
)
}

return data.snippet
},
{
Expand All @@ -51,6 +58,20 @@ export default function ViewSnippetHeader() {
onSuccess?.(forkedSnippet)
},
onError: (error: any) => {
// Check if the error message contains 'already exists'
if (error.message?.includes("already forked")) {
toast({
title: "Snippet already exists",
description: error.message,
variant: "destructive", // You can style this variant differently
})
} else {
toast({
title: "Error",
description: "Failed to fork snippet. Please try again.",
variant: "destructive", // Use destructive variant for errors
})
}
console.error("Error forking snippet:", error)
},
},
Expand Down
Loading