Skip to content

Commit bbc675f

Browse files
Add an HttpRequestException type to Java client (#33416)
1 parent e027239 commit bbc675f

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
package com.microsoft.signalr;
5+
6+
/**
7+
* An exception thrown when an http request fails with an unexpected status code.
8+
*/
9+
public class HttpRequestException extends RuntimeException {
10+
private static final long serialVersionUID = 1L;
11+
private final int statusCode;
12+
13+
/**
14+
* Initializes a new instance of the {@link HttpRequestException} class with a specified error message and http status code.
15+
*
16+
* @param message The error message that explains the reason for the exception.
17+
* @param statusCode The http status code.
18+
*/
19+
public HttpRequestException(String message, int statusCode) {
20+
super(message);
21+
this.statusCode = statusCode;
22+
}
23+
24+
/**
25+
* Gets the status code from an http request.
26+
*
27+
* @return The status code from an http request.
28+
*/
29+
public int getStatusCode() {
30+
return statusCode;
31+
}
32+
}

src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/HubConnection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ private Single<NegotiateResponse> handleNegotiate(String url, Map<String, String
168168

169169
return httpClient.post(Negotiate.resolveNegotiateUrl(url, this.negotiateVersion), request).map((response) -> {
170170
if (response.getStatusCode() != 200) {
171-
throw new RuntimeException(String.format("Unexpected status code returned from negotiate: %d %s.",
172-
response.getStatusCode(), response.getStatusText()));
171+
throw new HttpRequestException(String.format("Unexpected status code returned from negotiate: %d %s.",
172+
response.getStatusCode(), response.getStatusText()), response.getStatusCode());
173173
}
174174
JsonReader reader = new JsonReader(new StringReader(new String(response.getContent().array(), StandardCharsets.UTF_8)));
175175
NegotiateResponse negotiateResponse = new NegotiateResponse(reader);

src/SignalR/clients/java/signalr/test/src/main/java/com/microsoft/signalr/HubConnectionTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,8 +2704,9 @@ public void negotiateSentOnStart() {
27042704
.withHttpClient(client)
27052705
.build();
27062706

2707-
Exception exception = assertThrows(RuntimeException.class, () -> hubConnection.start().timeout(30, TimeUnit.SECONDS).blockingAwait());
2707+
HttpRequestException exception = assertThrows(HttpRequestException.class, () -> hubConnection.start().timeout(30, TimeUnit.SECONDS).blockingAwait());
27082708
assertEquals("Unexpected status code returned from negotiate: 404 .", exception.getMessage());
2709+
assertEquals(404, exception.getStatusCode());
27092710

27102711
List<HttpRequest> sentRequests = client.getSentRequests();
27112712
assertEquals(1, sentRequests.size());
@@ -3859,9 +3860,10 @@ public void non200FromNegotiateThrowsError() {
38593860
.withHttpClient(client)
38603861
.build();
38613862

3862-
RuntimeException exception = assertThrows(RuntimeException.class,
3863+
HttpRequestException exception = assertThrows(HttpRequestException.class,
38633864
() -> hubConnection.start().timeout(30, TimeUnit.SECONDS).blockingAwait());
38643865
assertEquals("Unexpected status code returned from negotiate: 500 Internal server error.", exception.getMessage());
3866+
assertEquals(500, exception.getStatusCode());
38653867
}
38663868

38673869
@Test

0 commit comments

Comments
 (0)