From 721bfecb77713fbaa5776fce06a124b889e36221 Mon Sep 17 00:00:00 2001 From: blueeon Date: Sun, 31 Mar 2024 15:02:39 +0800 Subject: [PATCH] fix: Optimize the logic for session ID validation and improve error messages. --- src/app/api/get_limit/route.ts | 2 +- src/lib/SunoApi.ts | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/app/api/get_limit/route.ts b/src/app/api/get_limit/route.ts index 8094694..9568a44 100644 --- a/src/app/api/get_limit/route.ts +++ b/src/app/api/get_limit/route.ts @@ -15,7 +15,7 @@ export async function GET(req: NextRequest) { } catch (error) { console.error('Error fetching limit:', error); - return new NextResponse(JSON.stringify({ error: 'Internal server error' }), { + return new NextResponse(JSON.stringify({ error: 'Internal server error. ' + error }), { status: 500, headers: { 'Content-Type': 'application/json' } }); diff --git a/src/lib/SunoApi.ts b/src/lib/SunoApi.ts index 0934336..04ac4a8 100644 --- a/src/lib/SunoApi.ts +++ b/src/lib/SunoApi.ts @@ -20,7 +20,7 @@ export interface AudioInfo { gpt_description_prompt?: string; // Prompt for GPT description prompt?: string; // Prompt for audio generation status: string; // Status - type?: string; + type?: string; tags?: string; // Genre of music. duration?: string; // Duration of the audio } @@ -66,12 +66,11 @@ class SunoApi { const getSessionUrl = `${SunoApi.CLERK_BASE_URL}/v1/client?_clerk_js_version=4.70.5`; // Get session ID const sessionResponse = await this.client.get(getSessionUrl); - const sid = sessionResponse.data.response['last_active_session_id']; - if (!sid) { - throw new Error("Failed to get session id"); + if (!sessionResponse?.data?.response?.['last_active_session_id']) { + throw new Error("Failed to get session id, you may need to update the SUNO_COOKIE"); } // Save session ID for later use - this.sid = sid; + this.sid = sessionResponse.data.response['last_active_session_id']; } /** @@ -234,6 +233,7 @@ class SunoApi { } } + /** * Processes the lyrics (prompt) from the audio metadata into a more readable format. * @param prompt The original lyrics text.