Skip to content

Commit

Permalink
use ctx.error
Browse files Browse the repository at this point in the history
  • Loading branch information
techmannih committed Jan 15, 2025
1 parent 22f32cc commit 1f87daf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
14 changes: 6 additions & 8 deletions fake-snippets-api/routes/api/snippets/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export default withRouteSpec({
jsonResponse: z.object({
ok: z.boolean(),
snippet: snippetSchema.optional(),
error: z.string().optional(),
}),
})(async (req, ctx) => {
let {
Expand All @@ -34,17 +33,16 @@ export default withRouteSpec({
unscoped_name = `untitled-${snippet_type}-${ctx.db.idCounter + 1}`
}

// Check if snippet already exists for the user
const existingSnippet = ctx.db.snippets.find(
(snippet) =>
snippet.unscoped_name === unscoped_name &&
snippet.owner_name === ctx.auth.github_username,
)

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

Expand All @@ -66,9 +64,9 @@ export default withRouteSpec({
try {
ctx.db.addSnippet(newSnippet)
} catch (error) {
return ctx.json({
ok: false,
error: `Failed to create snippet: ${error}`,
return ctx.error(500, {
error_code: "snippet_creation_failed",
message: `Failed to create snippet: ${error}`,
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/ViewSnippetHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function ViewSnippetHeader() {
},
onError: (error: any) => {
// Check if the error message contains 'already exists'
if (error.message?.includes("already been forked")) {
if (error.message?.includes("already forked")) {
toast({
title: "Snippet already exists",
description: error.message,
Expand Down

0 comments on commit 1f87daf

Please sign in to comment.