(req: NextRequest)
| 6 | export const dynamic = "force-dynamic"; |
| 7 | |
| 8 | export async function POST(req: NextRequest) { |
| 9 | if (req.method === 'POST') { |
| 10 | try { |
| 11 | const body = await req.json(); |
| 12 | const { prompt } = body; |
| 13 | |
| 14 | const lyrics = await (await sunoApi((await cookies()).toString())).generateLyrics(prompt); |
| 15 | |
| 16 | return new NextResponse(JSON.stringify(lyrics), { |
| 17 | status: 200, |
| 18 | headers: { |
| 19 | 'Content-Type': 'application/json', |
| 20 | ...corsHeaders |
| 21 | } |
| 22 | }); |
| 23 | } catch (error: any) { |
| 24 | console.error('Error generating lyrics:', error); |
| 25 | |
| 26 | // Handle different types of errors |
| 27 | if (error.response) { |
| 28 | // Axios error with response |
| 29 | console.error('Response error:', JSON.stringify(error.response.data)); |
| 30 | |
| 31 | if (error.response.status === 402) { |
| 32 | return new NextResponse(JSON.stringify({ |
| 33 | error: error.response.data?.detail || 'Payment required' |
| 34 | }), { |
| 35 | status: 402, |
| 36 | headers: { |
| 37 | 'Content-Type': 'application/json', |
| 38 | ...corsHeaders |
| 39 | } |
| 40 | }); |
| 41 | } |
| 42 | |
| 43 | return new NextResponse(JSON.stringify({ |
| 44 | error: 'API Error: ' + (error.response.data?.detail || error.response.statusText || 'Unknown error') |
| 45 | }), { |
| 46 | status: error.response.status || 500, |
| 47 | headers: { |
| 48 | 'Content-Type': 'application/json', |
| 49 | ...corsHeaders |
| 50 | } |
| 51 | }); |
| 52 | } else if (error.request) { |
| 53 | // Axios error without response (network error, timeout, etc.) |
| 54 | console.error('Network error:', error.message); |
| 55 | return new NextResponse(JSON.stringify({ |
| 56 | error: 'Network error: Unable to connect to Suno API. Please check your internet connection and try again.' |
| 57 | }), { |
| 58 | status: 503, |
| 59 | headers: { |
| 60 | 'Content-Type': 'application/json', |
| 61 | ...corsHeaders |
| 62 | } |
| 63 | }); |
| 64 | } else { |
| 65 | // Other types of errors (timeout, etc.) |
nothing calls this directly
no test coverage detected