Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ All notable changes to this project will be documented in this file.
* Updated Jackson dependency to 3.1.3 (#93)
* Use Java _record_ classes for `Message`, `Stop` and `Trip` models (#94)

### Fixed
* Throw exception on HTTP status != 200 instead of silently returning no results (#96)


## 2.0.11 - 2026-05-14

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/de/stklcode/pubtrans/ura/UraClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,11 @@ private InputStream request(String url) throws IOException {
reqBuilder.timeout(config.timeout());
}

return clientBuilder.build().send(reqBuilder.build(), HttpResponse.BodyHandlers.ofInputStream()).body();
var response = clientBuilder.build().send(reqBuilder.build(), HttpResponse.BodyHandlers.ofInputStream());
if (response.statusCode() != 200) {
throw new IOException("API request failed with status " + response.statusCode());
}
return response.body();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("API request interrupted", e);
Expand Down
27 changes: 14 additions & 13 deletions src/test/java/de/stklcode/pubtrans/ura/UraClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,13 @@ void getStopsTest() throws UraClientException {
assertEquals(6.0708663, stops.get(4).longitude());

// Test Exception handling.
/*
mockHttpToError(500);

var uraClient = new UraClient(wireMock.baseUrl());
var e = assertThrows(IllegalStateException.class, uraClient::getStops);
assertInstanceOf(IOException.class, e.getCause());
assertTrue(e.getCause().getMessage().startsWith("Server returned HTTP response code: 500 for URL"));
*/
Exception e = assertThrows(UraClientException.class, uraClient::getStops);
assertEquals("Failed to read stops from API", e.getMessage());
e = assertInstanceOf(IOException.class, e.getCause());
assertEquals("API request failed with status 500", e.getMessage());
}

@Test
Expand Down Expand Up @@ -203,21 +202,23 @@ void getTripsTest() throws UraClientException {
assertEquals(5, trips.size());

// Test Exception handling.
/*
mockHttpToError(502);
var uraClient = new UraClient(wireMock.baseUrl());
var e = assertThrows(IllegalStateException.class, uraClient::getTrips);
assertInstanceOf(IOException.class, e.getCause());
assertTrue(e.getCause().getMessage().startsWith("Server returned HTTP response code: 502 for URL"));
*/
Exception e = assertThrows(UraClientException.class, uraClient::getStops);
assertEquals("Failed to read stops from API", e.getMessage());
e = assertInstanceOf(IOException.class, e.getCause());
assertEquals("API request failed with status 502", e.getMessage());

mockHttpToException();
UraClientException exception = assertThrows(
e = assertThrows(
UraClientException.class,
() -> new UraClient(wireMock.baseUrl()).getTrips(),
"Expected reader to raise an exception"
);
assertEquals("Failed to read trips from API", exception.getMessage(), "Unexpected error message");
assertInstanceOf(IOException.class, exception.getCause(), "Unexpected error cause");
assertEquals("Failed to read trips from API", e.getMessage(), "Unexpected error message");
assertInstanceOf(IOException.class, e.getCause(), "Unexpected error cause");
assertEquals("Failed to read trips from API", e.getMessage(), "Unexpected error message");
assertInstanceOf(IOException.class, e.getCause(), "Unexpected error cause");

}

Expand Down