fix: Optimize the logic for session ID validation and improve error messages.

This commit is contained in:
blueeon 2024-03-31 15:02:39 +08:00
parent 1cb4e921db
commit 721bfecb77
2 changed files with 6 additions and 6 deletions

View File

@ -15,7 +15,7 @@ export async function GET(req: NextRequest) {
} catch (error) { } catch (error) {
console.error('Error fetching limit:', 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, status: 500,
headers: { 'Content-Type': 'application/json' } headers: { 'Content-Type': 'application/json' }
}); });

View File

@ -20,7 +20,7 @@ export interface AudioInfo {
gpt_description_prompt?: string; // Prompt for GPT description gpt_description_prompt?: string; // Prompt for GPT description
prompt?: string; // Prompt for audio generation prompt?: string; // Prompt for audio generation
status: string; // Status status: string; // Status
type?: string; type?: string;
tags?: string; // Genre of music. tags?: string; // Genre of music.
duration?: string; // Duration of the audio 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`; const getSessionUrl = `${SunoApi.CLERK_BASE_URL}/v1/client?_clerk_js_version=4.70.5`;
// Get session ID // Get session ID
const sessionResponse = await this.client.get(getSessionUrl); const sessionResponse = await this.client.get(getSessionUrl);
const sid = sessionResponse.data.response['last_active_session_id']; if (!sessionResponse?.data?.response?.['last_active_session_id']) {
if (!sid) { throw new Error("Failed to get session id, you may need to update the SUNO_COOKIE");
throw new Error("Failed to get session id");
} }
// Save session ID for later use // 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. * Processes the lyrics (prompt) from the audio metadata into a more readable format.
* @param prompt The original lyrics text. * @param prompt The original lyrics text.