From 4d5a7dee32b70585503394e8cbc75f13e3d03f56 Mon Sep 17 00:00:00 2001 From: sirpiglr <49359077-sirpiglr@users.noreply.replit.com> Date: Wed, 3 Dec 2025 02:16:30 +0000 Subject: [PATCH] Improve error handling and logging for Discord account linking Enhance the Discord verification API with granular step-by-step error tracking and more specific error messages for improved debugging. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 9203795e-937a-4306-b81d-b4d5c78c240e Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: 65035574-4852-4251-bbc4-c9c37cc1ce43 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7c94b7a0-29c7-4f2e-94ef-44b2153872b7/9203795e-937a-4306-b81d-b4d5c78c240e/saoW2ee Replit-Helium-Checkpoint-Created: true --- .replit | 4 --- api/discord/verify-code.ts | 70 +++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/.replit b/.replit index e58779c6..b28a4fdd 100644 --- a/.replit +++ b/.replit @@ -52,10 +52,6 @@ externalPort = 80 localPort = 8044 externalPort = 3003 -[[ports]] -localPort = 36225 -externalPort = 3002 - [[ports]] localPort = 38557 externalPort = 3000 diff --git a/api/discord/verify-code.ts b/api/discord/verify-code.ts index 9b3b21b7..6f539139 100644 --- a/api/discord/verify-code.ts +++ b/api/discord/verify-code.ts @@ -29,13 +29,15 @@ export default async function handler(req: any, res: any) { return res.status(500).json({ message: "Server configuration error" }); } + let step = "init"; try { + step = "create_client"; console.log("[Discord Verify] Creating Supabase client with URL:", supabaseUrl?.substring(0, 30) + "..."); const supabase = createClient(supabaseUrl, supabaseServiceRole); + step = "lookup_code"; console.log("[Discord Verify] Looking up code:", verification_code.trim()); - // Find valid verification code const { data: verification, error: verifyError } = await supabase .from("discord_verifications") .select("*") @@ -55,63 +57,74 @@ export default async function handler(req: any, res: any) { error: verifyError, }); return res.status(400).json({ - message: - "Invalid or expired verification code. Please try /verify again.", + message: "Invalid or expired verification code. Please try /verify again.", error: verifyError.message, + step, }); } if (!verification) { return res.status(400).json({ - message: - "Invalid or expired verification code. Please try /verify again.", + message: "Invalid or expired verification code. Please try /verify again.", + step, }); } + step = "check_existing_link"; const discordId = verification.discord_id; - // Check if already linked - const { data: existingLink } = await supabase + const { data: existingLink, error: existingLinkError } = await supabase .from("discord_links") .select("*") .eq("discord_id", discordId) .single(); - if (existingLink && existingLink.user_id !== user_id) { - return res.status(400).json({ - message: - "This Discord account is already linked to another AeThex account.", + if (existingLinkError && existingLinkError.code !== "PGRST116") { + console.error("[Discord Verify] Existing link check failed:", existingLinkError); + return res.status(500).json({ + message: "Failed to check existing link", + step, + error: existingLinkError.message, }); } - // Create or update link - const { error: linkError } = await supabase.from("discord_links").upsert({ - discord_id: discordId, - user_id: user_id, - linked_at: new Date().toISOString(), - }); + if (existingLink && existingLink.user_id !== user_id) { + return res.status(400).json({ + message: "This Discord account is already linked to another AeThex account.", + step, + }); + } + + step = "create_link"; + const { error: linkError } = await supabase.from("discord_links").upsert( + { + discord_id: discordId, + user_id: user_id, + linked_at: new Date().toISOString(), + }, + { onConflict: "discord_id" } + ); if (linkError) { console.error("[Discord Verify] Link creation failed:", linkError); - return res - .status(500) - .json({ message: "Failed to link Discord account" }); + return res.status(500).json({ + message: "Failed to link Discord account", + step, + error: linkError.message, + }); } - // Delete used verification code + step = "delete_code"; const { error: deleteError } = await supabase .from("discord_verifications") .delete() .eq("verification_code", verification_code.trim()); if (deleteError) { - console.error( - "[Discord Verify] Failed to delete verification code:", - deleteError, - ); - // Don't return error - code is already used and link is created + console.error("[Discord Verify] Failed to delete verification code:", deleteError); } + step = "success"; res.status(200).json({ success: true, message: "Discord account linked successfully!", @@ -122,14 +135,15 @@ export default async function handler(req: any, res: any) { }, }); } catch (error: any) { - console.error("[Discord Verify] Error:", { + console.error("[Discord Verify] Error at step:", step, { message: error?.message, code: error?.code, stack: error?.stack?.substring(0, 500), }); res.status(500).json({ message: "An error occurred. Please try again.", - debug: process.env.NODE_ENV === 'development' ? error?.message : undefined, + step, + error: error?.message, }); } }