Skip to content

Commit 061bd1b

Browse files
committed
more error handling
1 parent 29e5c77 commit 061bd1b

File tree

1 file changed

+57
-18
lines changed

1 file changed

+57
-18
lines changed

generate/modes/rap/generate.ts

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import getAudioDuration from '../../audioDuration';
2-
import { generateCleanSrt } from '../../cleanSrt';
31
import { query } from '../../dbClient';
4-
import { secondsToSrtTime, srtTimeToSeconds } from '../../transcribeAudio';
5-
import { transcribeAudio } from '../../transcribeAudio';
6-
import { generateFillerContext } from '../../fillerContext';
7-
import { writeFile } from 'fs/promises';
82
import path from 'path';
93
import ffmpeg from 'fluent-ffmpeg';
104
import fs from 'fs';
115

126
const RVC_SERVICE_URL = process.env.RVC_SERVICE_URL || 'http://127.0.0.1:5555';
137

148
function adjustPath(filePath: string): string {
9+
if (!filePath) {
10+
console.error(
11+
'Error: filePath is undefined or null in adjustPath function'
12+
);
13+
return '';
14+
}
15+
1516
if (filePath.startsWith('/app/shared_data/')) {
1617
return filePath.replace('/app/shared_data/', '/app/brainrot/shared_data/');
1718
} else if (filePath.startsWith('shared_data/')) {
@@ -43,16 +44,35 @@ export default async function generateRap({
4344
);
4445
}
4546

46-
const { instrumentalPath, vocalPath } = await fetch(
47-
`${RVC_SERVICE_URL}/audio-separator`,
48-
{
49-
method: 'POST',
50-
headers: {
51-
'Content-Type': 'application/json',
52-
},
53-
body: JSON.stringify({ url: audioUrl }),
54-
}
55-
).then((res) => res.json());
47+
const response = await fetch(`${RVC_SERVICE_URL}/audio-separator`, {
48+
method: 'POST',
49+
headers: {
50+
'Content-Type': 'application/json',
51+
},
52+
body: JSON.stringify({ url: audioUrl }),
53+
});
54+
55+
if (!response.ok) {
56+
const errorData = await response.json().catch(() => ({}));
57+
console.error('Audio separator API error:', errorData);
58+
throw new Error(
59+
`Audio separator API returned status ${response.status}: ${
60+
errorData.error || 'Unknown error'
61+
}`
62+
);
63+
}
64+
65+
const separatorData = await response.json();
66+
67+
if (!separatorData.instrumentalPath || !separatorData.vocalPath) {
68+
console.error(
69+
'Audio separator API returned incomplete data:',
70+
separatorData
71+
);
72+
throw new Error('Audio separator API did not return required paths');
73+
}
74+
75+
const { instrumentalPath, vocalPath } = separatorData;
5676

5777
const adjustedInstrumentalPath = adjustPath(instrumentalPath);
5878
const adjustedVocalPath = adjustPath(vocalPath);
@@ -70,7 +90,7 @@ export default async function generateRap({
7090
);
7191
}
7292

73-
const { finalAudioPath } = await fetch(`${RVC_SERVICE_URL}/rvc`, {
93+
const rvcResponse = await fetch(`${RVC_SERVICE_URL}/rvc`, {
7494
method: 'POST',
7595
body: JSON.stringify({
7696
instrumentalPath,
@@ -80,7 +100,26 @@ export default async function generateRap({
80100
headers: {
81101
'Content-Type': 'application/json',
82102
},
83-
}).then((res) => res.json());
103+
});
104+
105+
if (!rvcResponse.ok) {
106+
const errorData = await rvcResponse.json().catch(() => ({}));
107+
console.error('RVC API error:', errorData);
108+
throw new Error(
109+
`RVC API returned status ${rvcResponse.status}: ${
110+
errorData.error || 'Unknown error'
111+
}`
112+
);
113+
}
114+
115+
const rvcData = await rvcResponse.json();
116+
117+
if (!rvcData.finalAudioPath) {
118+
console.error('RVC API returned incomplete data:', rvcData);
119+
throw new Error('RVC API did not return required finalAudioPath');
120+
}
121+
122+
const { finalAudioPath } = rvcData;
84123

85124
// Adjust the final audio path for the brainrot container
86125
const adjustedFinalAudioPath = adjustPath(finalAudioPath);

0 commit comments

Comments
 (0)