Skip to content

Commit 1bcb832

Browse files
committed
Avoid NPE with metrics + catch 400 when loading autoplay (#284)
1 parent f3bbcb9 commit 1bcb832

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

lib/src/main/java/xyz/gianlu/librespot/mercury/MercuryClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,11 @@ void dispatch(@NotNull Response resp) {
347347
}
348348

349349
public static class MercuryException extends Exception {
350+
public final int code;
351+
350352
private MercuryException(@NotNull Response response) {
351353
super(String.format("status: %d", response.statusCode));
354+
this.code = response.statusCode;
352355
}
353356
}
354357

player/src/main/java/xyz/gianlu/librespot/player/Player.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ public class Player implements Closeable, DeviceStateHandler.Listener, PlayerSes
5454
private final PlayerConfiguration conf;
5555
private final EventsDispatcher events;
5656
private final AudioSink sink;
57+
private final Map<String, PlaybackMetrics> metrics = new HashMap<>(5);
5758
private StateWrapper state;
5859
private PlayerSession playerSession;
5960
private ScheduledFuture<?> releaseLineFuture = null;
60-
private Map<String, PlaybackMetrics> metrics = new HashMap<>(5);
6161

6262
public Player(@NotNull PlayerConfiguration conf, @NotNull Session session) {
6363
this.conf = conf;
@@ -207,7 +207,7 @@ private void panicState(@Nullable PlaybackMetrics.Reason reason) {
207207
state.updated();
208208

209209
if (reason == null) {
210-
metrics = null;
210+
metrics.clear();
211211
} else if (playerSession != null) {
212212
endMetrics(playerSession.currentPlaybackId(), reason, playerSession.currentMetrics(), state.getPosition());
213213
}
@@ -501,14 +501,6 @@ private void loadAutoplay() {
501501
return;
502502
}
503503

504-
if (context.startsWith("spotify:search:")) {
505-
LOGGER.info("Cannot load autoplay for search context: " + context);
506-
507-
state.setPosition(0);
508-
state.setState(true, false, false);
509-
state.updated();
510-
return;
511-
}
512504

513505
String contextDesc = state.getContextMetadata("context_description");
514506

@@ -540,8 +532,16 @@ private void loadAutoplay() {
540532
state.updated();
541533
}
542534
} catch (IOException | MercuryClient.MercuryException ex) {
543-
LOGGER.fatal("Failed loading autoplay station!", ex);
544-
panicState(null);
535+
if (ex instanceof MercuryClient.MercuryException && ((MercuryClient.MercuryException) ex).code == 400) {
536+
LOGGER.info("Cannot load autoplay for search context: " + context);
537+
538+
state.setPosition(0);
539+
state.setState(true, true, false);
540+
state.updated();
541+
} else {
542+
LOGGER.fatal("Failed loading autoplay station!", ex);
543+
panicState(null);
544+
}
545545
} catch (AbsSpotifyContext.UnsupportedContextException ex) {
546546
LOGGER.fatal("Cannot play local tracks!", ex);
547547
panicState(null);

0 commit comments

Comments
 (0)