Skip to content

Commit 9500c37

Browse files
committed
Create exceptions for http response and protocol.
1 parent 3234e09 commit 9500c37

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package javajs.http;
2+
3+
import java.io.IOException;
4+
5+
/**
6+
* A generic class for exceptions resulting from the HTTP exchange.
7+
*/
8+
public class ClientProtocolException extends IOException {
9+
10+
private static final long serialVersionUID = 2500725227517025772L;
11+
12+
public ClientProtocolException() {
13+
super();
14+
}
15+
16+
public ClientProtocolException(String message) {
17+
super(message);
18+
}
19+
20+
public ClientProtocolException(Throwable cause) {
21+
super(cause);
22+
}
23+
24+
public ClientProtocolException(String message, Throwable cause) {
25+
super(message, cause);
26+
}
27+
28+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package javajs.http;
2+
3+
/**
4+
* Exception that can be raised by the application to indicate
5+
* a non 2xx server response. The http client will not raise
6+
* this exception implicitly. A typical use case is to throw
7+
* this exception if the response status code indicate failure.
8+
* The status code and the reason phrase should be populated from
9+
* the {@link HttpClient.HttpResponse#getStatusCode()} and
10+
* {@link HttpClient.HttpResponse#getReasonPhrase()} respectively.
11+
*/
12+
public class HttpResponseException extends ClientProtocolException {
13+
14+
private static final long serialVersionUID = -8481868921105838666L;
15+
16+
private final int statusCode;
17+
private final String reasonPhrase;
18+
19+
public HttpResponseException(int statusCode, String reasonPhrase, String message) {
20+
super(message);
21+
this.statusCode = statusCode;
22+
this.reasonPhrase = reasonPhrase;
23+
}
24+
25+
public HttpResponseException(int statusCode, String reasonPhrase) {
26+
this(statusCode, reasonPhrase, String.format("%d %s", statusCode, reasonPhrase));
27+
}
28+
29+
/**
30+
* Get status code of the response which caused this exception.
31+
*
32+
* @return status code
33+
*/
34+
public int getStatusCode() {
35+
return statusCode;
36+
}
37+
38+
/**
39+
* Get reason phrase complementing the error's status code.
40+
*/
41+
public String getReasonPhrase() {
42+
return reasonPhrase;
43+
}
44+
45+
}

0 commit comments

Comments
 (0)