From 272a5cecab32d5bfa59edb67fd1cd0c5051ca9dd Mon Sep 17 00:00:00 2001 From: Ryan Freeman Date: Mon, 21 Apr 2025 17:03:29 +0100 Subject: [PATCH] Improve comment error handling --- app/actions/comments.ts | 11 ++++++----- components/ui/Comments.tsx | 2 +- lib/ntfy.ts | 20 ++++++++++++-------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/app/actions/comments.ts b/app/actions/comments.ts index b868a1d..0c6bf9b 100644 --- a/app/actions/comments.ts +++ b/app/actions/comments.ts @@ -37,9 +37,10 @@ const notificationBody = (comment: { id: number, content: string }, user: { name } export async function addComment(prevState: { message: string }, formData: FormData) { - const general_error = 'There was a problem with your comment, please try again later.' + const validation_error = 'Validation error, your comment was invalid.' const authorisation_error = 'Error, you must be logged in to post a comment.' - const success_message = 'Thanks, your comment was submitted and is awaiting approval.' + const server_error = 'Server error, please try again later.' + const success_message = 'Your comment was submitted and is awaiting approval.' const schema = z.object({ comment: z.string().min(1).max(300), @@ -55,7 +56,7 @@ export async function addComment(prevState: { message: string }, formData: FormD const parse = schema.safeParse({comment, slug, parent_id}); if (!parse.success) { - return {message: general_error} + return {message: validation_error} } if (parent_id === '') parent_id = null @@ -82,7 +83,7 @@ export async function addComment(prevState: { message: string }, formData: FormD .single() if (error || newComment?.id === null) { - return {message: general_error} + return {message: server_error} } if (process.env.NODE_ENV === 'production') { @@ -92,6 +93,6 @@ export async function addComment(prevState: { message: string }, formData: FormD return {message: success_message} } catch (error) { console.error('Error posting comment:', error) - return {message: general_error} + return {message: server_error} } } \ No newline at end of file diff --git a/components/ui/Comments.tsx b/components/ui/Comments.tsx index 475d141..d74b76b 100644 --- a/components/ui/Comments.tsx +++ b/components/ui/Comments.tsx @@ -172,7 +172,7 @@ Comments.Form = function Form({slug}: { slug: string }) { -
+

{`${commentLength} / ${commentMaxLength}`}

{replyContext?.replyTo && diff --git a/lib/ntfy.ts b/lib/ntfy.ts index 80f3920..5c8aeb7 100644 --- a/lib/ntfy.ts +++ b/lib/ntfy.ts @@ -1,15 +1,19 @@ import fetcher from '@/lib/fetcher' -const NTFY_URL = process.env.NTFY_URL || '' +const NTFY_URL = process.env.NTFY_URL ?? '' export async function sendNotification(notificationBody: any) { if (NTFY_URL !== '') { - await fetcher(NTFY_URL, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify(notificationBody) - }) + try { + await fetcher(NTFY_URL, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(notificationBody) + }) + } catch (error) { + console.error('Failed to send notification:', error) + } } } \ No newline at end of file