Skip to content

Commit 7f0aa3e

Browse files
julius-ddargmuesli
andauthored
fix: throw Exception on all 4xx and 5xx status codes (#418)
* fix: throw Exception on all 4xx and 5xx status codes I had a temporary bug in my usage of this nice lib that is likely caused by Spotify replying with a 4xx or 5xx status code that was handled by the lib as 2xx status code. * chore: format --------- Co-authored-by: Jonas Thelemann <e-mail@jonas-thelemann.de>
1 parent d4d4927 commit 7f0aa3e

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ private String getResponseBody(CloseableHttpResponse httpResponse) throws
350350
case HttpStatus.SC_SERVICE_UNAVAILABLE:
351351
throw new ServiceUnavailableException(errorMessage);
352352
default:
353+
if (httpResponse.getCode() >= 400 && httpResponse.getCode() < 500) {
354+
throw new BadRequestException(errorMessage);
355+
} else if (httpResponse.getCode() >= 500) {
356+
throw new InternalServerErrorException(errorMessage);
357+
}
353358
return responseBody;
354359
}
355360
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package se.michaelthelin.spotify;
2+
3+
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
4+
import org.apache.hc.core5.http.Header;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.ValueSource;
8+
import se.michaelthelin.spotify.SpotifyHttpManager.Builder;
9+
import se.michaelthelin.spotify.exceptions.SpotifyWebApiException;
10+
11+
import java.net.URI;
12+
13+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
14+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
15+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
16+
17+
@WireMockTest(httpPort = 9090)
18+
class SpotifyHttpManagerTest {
19+
20+
private final SpotifyHttpManager spotifyHttpManager = new SpotifyHttpManager(new Builder());
21+
22+
@ParameterizedTest
23+
@ValueSource(ints = {405, 409, 410, 414, 422, 431, 499})
24+
public void throwsSpotifyWebApiExceptionForAll4xxStatusCodes(int statusCode) {
25+
26+
stubFor(get("/test/foo/")
27+
.willReturn(aResponse()
28+
.withStatus(statusCode)));
29+
30+
Assertions.assertThrows(SpotifyWebApiException.class, () ->
31+
spotifyHttpManager.get(URI.create("http://localhost:9090/test/foo/"), new Header[0]));
32+
}
33+
34+
@ParameterizedTest
35+
@ValueSource(ints = {501, 504, 599})
36+
public void throwsSpotifyWebApiExceptionForAll5xxStatusCodes(int statusCode) {
37+
38+
stubFor(get("/test/foo/")
39+
.willReturn(aResponse()
40+
.withStatus(statusCode)));
41+
42+
Assertions.assertThrows(SpotifyWebApiException.class, () ->
43+
spotifyHttpManager.get(URI.create("http://localhost:9090/test/foo/"), new Header[0]));
44+
}
45+
}

0 commit comments

Comments
 (0)