From 49a6131b23f1e822bef9624430d8466d6298e52f Mon Sep 17 00:00:00 2001 From: b3ni15 Date: Thu, 16 Oct 2025 13:33:35 +0200 Subject: [PATCH] refactor: Improve error handling for Discord API response parsing --- src/app/api/upload-part/route.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/app/api/upload-part/route.ts b/src/app/api/upload-part/route.ts index 2be63b7..2d013b6 100644 --- a/src/app/api/upload-part/route.ts +++ b/src/app/api/upload-part/route.ts @@ -33,13 +33,18 @@ export async function POST(request: Request) { } ); - if (!discordRes.ok) { - const errorBody = await discordRes.json(); - console.error('Discord API Error:', errorBody); - return NextResponse.json({ error: 'Failed to upload to Discord' }, { status: 500 }); + let discordData; + try { + discordData = await discordRes.json(); + } catch (jsonError) { + console.error('Failed to parse Discord response as JSON:', jsonError); + return NextResponse.json({ error: 'Failed to parse Discord response' }, { status: 500 }); } - const discordData = await discordRes.json(); + if (!discordRes.ok) { + console.error('Discord API Error:', discordData); + return NextResponse.json({ error: 'Failed to upload to Discord' }, { status: 500 }); + } const attachment = discordData.attachments[0]; if (!attachment) {