Skip to content

Commit 05e478f

Browse files
authored
Merge pull request #15 from ColyTeam/feature/better-error-handling
Better Error Handling
2 parents 36d3156 + e1a728c commit 05e478f

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

main/src/main/java/com/shirkanesi/magmaplayer/ytdlp/YTDLPAudioTrack.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ private void pullAudioStreamAsync(String streamUrl, File tempFile) {
112112

113113
while (pullProcess.getInputStream().available() < MIN_AVAILABLE_BYTES) {
114114
// Yes, this is busy waiting. The author does not know a better solution.
115-
// TODO: find better solution
116115
Thread.sleep(100);
117116
}
118117

@@ -149,6 +148,8 @@ private String findStreamUrl(String commandTemplate, String url) throws IOExcept
149148
process.destroy();
150149
}
151150

151+
handleErrorInProcess(process);
152+
152153
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
153154
return bufferedReader.readLine();
154155
}
@@ -273,6 +274,8 @@ public AudioTrackInformation getInformation() throws AudioPlayerException {
273274
process.destroy();
274275
}
275276

277+
handleErrorInProcess(process);
278+
276279
String json;
277280
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
278281
// yt-dlp will not put any line-breaks into the response ==> one line is enough
@@ -285,4 +288,21 @@ public AudioTrackInformation getInformation() throws AudioPlayerException {
285288
throw new AudioPlayerException("Could not load track information", e);
286289
}
287290
}
291+
292+
private void handleErrorInProcess(Process process) {
293+
if (process.exitValue() != 0) {
294+
StringBuilder content = new StringBuilder();
295+
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
296+
String line;
297+
while ((line = bufferedReader.readLine()) != null) {
298+
content.append(line).append("\n");
299+
}
300+
301+
} catch (IOException e) {
302+
throw new AudioTrackPullException("Error while reading from source-stream", e);
303+
}
304+
throw new AudioTrackPullException("Error while reading from source-stream: " + content);
305+
}
306+
}
307+
288308
}

0 commit comments

Comments
 (0)