diff --git a/server.js b/server.js index 6bb4ce3..ba78d1e 100644 --- a/server.js +++ b/server.js @@ -1316,6 +1316,10 @@ ${idadeInstrucao} headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ contents: [{ parts: [{ text: systemInstruction }] }] }) }); + if (!response.ok) { + const txt = await response.text(); + throw new Error(`Gemini Error: ${txt}`); + } const data = await response.json(); musicaText = data.candidates?.[0]?.content?.parts?.[0]?.text || ''; } @@ -1323,7 +1327,7 @@ ${idadeInstrucao} // Limpeza musicaText = musicaText.replace(/[\s\S]*?<\/think>/gi, '').trim(); - if (!musicaText) throw new Error('Falha ao gerar música'); + if (!musicaText) throw new Error('Falha ao gerar música (texto vazio)'); const dbRes = await dbPool.query( `INSERT INTO escola.musicas (usuario_id, faixa_etaria, estrofes, ritmo, tema, musica) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id`, @@ -1333,7 +1337,7 @@ ${idadeInstrucao} res.json({ id: dbRes.rows[0].id, musica: musicaText }); } catch (err) { console.error('Erro Música:', err); - res.status(500).json({ error: 'Erro ao gerar música.' }); + res.status(500).json({ error: 'Erro ao gerar música: ' + err.message }); } }); @@ -1399,13 +1403,17 @@ ${idadeInstrucao} headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ contents: [{ parts: [{ text: systemInstruction }] }] }) }); + if (!response.ok) { + const txt = await response.text(); + throw new Error(`Gemini Error: ${txt}`); + } const data = await response.json(); historiaText = data.candidates?.[0]?.content?.parts?.[0]?.text || ''; } historiaText = historiaText.replace(/[\s\S]*?<\/think>/gi, '').trim(); - if (!historiaText) throw new Error('Falha ao inventar história'); + if (!historiaText) throw new Error('Falha ao inventar história (texto vazio)'); const dbRes = await dbPool.query( `INSERT INTO escola.historias (usuario_id, faixa_etaria, paragrafos, personagens, estilo, tema, historia) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id`, @@ -1415,7 +1423,7 @@ ${idadeInstrucao} res.json({ id: dbRes.rows[0].id, historia: historiaText }); } catch (err) { console.error('Erro História:', err); - res.status(500).json({ error: 'Erro ao inventar história.' }); + res.status(500).json({ error: 'Erro ao inventar história: ' + err.message }); } });