Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit af01c8c

Browse files
committed
Rework exception model
1 parent 62ab2c3 commit af01c8c

19 files changed

Lines changed: 154 additions & 203 deletions

README.md

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,14 @@ To access devRant simply create a new `DevRant` object:
2424
DevRant devRant = new DevRant();
2525
```
2626

27-
Most object that are returned from `DevRant` are wrapped in a `Result` object.
28-
This will contain an optional result value, along with an error message.
29-
If the optional result is empty, then an error occurred and the error message will be set.
30-
For example:
31-
32-
```
33-
Result<CommentedRant> result = devRant.getRant(832125);
34-
35-
if (!result.getValue().isPresent()) {
36-
System.out.println("An error occurred: " + result.getError());
37-
} else {
38-
CommentedRant rant = result.getValue().get();
39-
System.out.println(rant.getUser().getUsername() + '\n' + rant.getText());
40-
}
41-
```
42-
4327
The `DevRant` class itself can be used to get specific rants and users.
4428

4529
```
4630
// Get a specific rant.
47-
Result<CommentedRant> rant = devRant.getRant(686001);
31+
CommentedRant rant = devRant.getRant(686001);
4832
4933
// Get a user by username.
50-
Result<User> me = devRant.getUser("LucaScorpion");
34+
User me = devRant.getUser("LucaScorpion");
5135
```
5236

5337
The `DevRant` class contains 2 methods for getting to specific parts of the api.
@@ -56,13 +40,13 @@ This is used to access the rant and collab feeds.
5640

5741
```
5842
// Get the 10 latest rants.
59-
Result<List<Rant>> recent = devRant.getFeed().getRants(Sort.RECENT, 10, 0);
43+
List<Rant> recent = devRant.getFeed().getRants(Sort.RECENT, 10, 0);
6044
6145
// Get the 10 best stories.
62-
Result<List<Rant>> stories = devRant.getFeed().getStories(Sort.TOP, 0);
46+
List<Rant> stories = devRant.getFeed().getStories(Sort.TOP, 0);
6347
6448
// Get 10 collabs.
65-
Result<List<Collab>> collabs = devRant.getFeed().getCollabs(10);
49+
List<Collab> collabs = devRant.getFeed().getCollabs(10);
6650
```
6751

6852
Second, `getAuth()` which returns a `DevRantAuth` object, which is used to access user functionality.

src/main/java/com/scorpiac/javarant/DevRant.java

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ public DevRantAuth getAuth() {
7070
* @param id The id of the rant.
7171
* @return The rant.
7272
*/
73-
public Result<CommentedRant> getRant(int id) {
74-
return requestHandler.get(ApiEndpoint.RANTS.toString() + '/' + id, CommentedRantResponse.class);
73+
public CommentedRant getRant(int id) {
74+
return requestHandler.get(ApiEndpoint.RANTS.toString() + '/' + id, CommentedRantResponse.class)
75+
.getValue().orElseThrow(() -> new NoSuchRantException(id));
7576
}
7677

7778
/**
@@ -80,16 +81,11 @@ public Result<CommentedRant> getRant(int id) {
8081
* @param username The username of the user.
8182
* @return The user.
8283
*/
83-
public Result<User> getUser(String username) {
84-
Result<Integer> result = requestHandler.get(ApiEndpoint.USER_ID, UserIdResponse.class, new BasicNameValuePair("username", username));
84+
public User getUser(String username) {
85+
Integer result = requestHandler.get(ApiEndpoint.USER_ID, UserIdResponse.class, new BasicNameValuePair("username", username))
86+
.getValue().orElseThrow(() -> new NoSuchUserException(username));
8587

86-
// Check the result.
87-
if (!result.getValue().isPresent()) {
88-
// When the username is invalid, no error message is returned by the API.
89-
return new Result<>("Invalid username specified.");
90-
}
91-
92-
return getUser(result.getValue().get());
88+
return getUser(result);
9389
}
9490

9591
/**
@@ -98,28 +94,23 @@ public Result<User> getUser(String username) {
9894
* @param id The id of the user.
9995
* @return The user.
10096
*/
101-
public Result<User> getUser(int id) {
102-
Result<User> result = requestHandler.get(ApiEndpoint.USERS.toString() + '/' + id, UserResponse.class);
103-
104-
// Check the result.
105-
if (!result.getValue().isPresent()) {
106-
// When the user id is invalid, no error message is returned by the API.
107-
return new Result<>("Invalid user id specified.");
108-
}
97+
public User getUser(int id) {
98+
User user = requestHandler.get(ApiEndpoint.USERS.toString() + '/' + id, UserResponse.class)
99+
.getValue().orElseThrow(() -> new NoSuchUserException(id));
109100

110101
// Set the id, as that is not part of the response.
111-
result.getValue().get().setId(id);
102+
user.setId(id);
112103

113-
return result;
104+
return user;
114105
}
115106

116107
/**
117108
* Get a random rant.
118109
*
119110
* @return A random rant.
120111
*/
121-
public Result<Rant> getSurprise() {
122-
return requestHandler.get(ApiEndpoint.SURPRISE, RantResponse.class);
112+
public Rant getSurprise() {
113+
return requestHandler.get(ApiEndpoint.SURPRISE, RantResponse.class).getValueOrError();
123114
}
124115

125116
/**
@@ -128,8 +119,9 @@ public Result<Rant> getSurprise() {
128119
* @param id The id of the collab.
129120
* @return The collab.
130121
*/
131-
public Result<Collab> getCollab(int id) {
132-
return requestHandler.get(ApiEndpoint.RANTS.toString() + '/' + id, CollabResponse.class);
122+
public Collab getCollab(int id) {
123+
return requestHandler.get(ApiEndpoint.RANTS.toString() + '/' + id, CollabResponse.class)
124+
.getValue().orElseThrow(() -> new NoSuchRantException(id));
133125
}
134126

135127
/**
@@ -143,17 +135,17 @@ public Result<Collab> getCollab(int id) {
143135
public boolean login(String username, char[] password) {
144136
logout();
145137

146-
Result<Auth> response = requestHandler.post(ApiEndpoint.AUTH_TOKEN, AuthResponse.class,
138+
Auth response = requestHandler.post(ApiEndpoint.AUTH_TOKEN, AuthResponse.class,
147139
new BasicNameValuePair("username", username),
148140
new BasicNameValuePair("password", String.valueOf(password))
149-
);
141+
).getValue().orElseThrow(() -> new DevRantException("Could not log in."));
150142

151143
// Clear the password.
152144
for (int i = 0; i < password.length; i++) {
153145
password[i] = 0;
154146
}
155147

156-
response.getValue().ifPresent(r -> auth = r);
148+
auth = response;
157149
return isLoggedIn();
158150
}
159151

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.scorpiac.javarant;
2+
3+
public class DevRantApiException extends RuntimeException {
4+
public DevRantApiException(String message) {
5+
super("A devRant API exception occurred: " + message);
6+
}
7+
}

src/main/java/com/scorpiac/javarant/DevRantAuth.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ public class DevRantAuth {
2121
* @param vote The vote to cast.
2222
* @return The rant.
2323
*/
24-
public Result<Rant> voteRant(int id, Vote vote) {
24+
public Rant voteRant(int id, Vote vote) {
2525
return devRant.getRequestHandler().post(
2626
ApiEndpoint.RANTS.toString() + '/' + id + '/' + ApiEndpoint.VOTE.toString(),
2727
RantResponse.class,
2828
getParameters(vote.getOptions())
29-
);
29+
).getValueOrError();
3030
}
3131

3232
/**
@@ -36,12 +36,12 @@ public Result<Rant> voteRant(int id, Vote vote) {
3636
* @param vote The vote to cast.
3737
* @return The comment.
3838
*/
39-
public Result<Comment> voteComment(int id, Vote vote) {
39+
public Comment voteComment(int id, Vote vote) {
4040
return devRant.getRequestHandler().post(
4141
ApiEndpoint.COMMENTS.toString() + '/' + id + '/' + ApiEndpoint.VOTE.toString(),
4242
CommentResponse.class,
4343
getParameters(vote.getOptions())
44-
);
44+
).getValueOrError();
4545
}
4646

4747
private NameValuePair[] getParameters(List<NameValuePair> params) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.scorpiac.javarant;
2+
3+
public class DevRantException extends RuntimeException {
4+
public DevRantException(String message) {
5+
super(message);
6+
}
7+
8+
public DevRantException(String message, Throwable cause) {
9+
super(message, cause);
10+
}
11+
}

src/main/java/com/scorpiac/javarant/DevRantFeed.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.scorpiac.javarant;
22

3+
import com.scorpiac.javarant.responses.CollabsFeedResponse;
34
import com.scorpiac.javarant.responses.RantsFeedResponse;
45
import com.scorpiac.javarant.responses.ResultsFeedResponse;
56
import org.apache.http.message.BasicNameValuePair;
@@ -21,12 +22,12 @@ public class DevRantFeed {
2122
* @param skip How many rants to skip.
2223
* @return Rants from the feed.
2324
*/
24-
public Result<List<Rant>> getRants(Sort sort, int limit, int skip) {
25+
public List<Rant> getRants(Sort sort, int limit, int skip) {
2526
return devRant.getRequestHandler().get(ApiEndpoint.RANTS, RantsFeedResponse.class,
2627
new BasicNameValuePair("sort", sort.toString()),
2728
new BasicNameValuePair("limit", String.valueOf(limit)),
2829
new BasicNameValuePair("skip", String.valueOf(skip))
29-
);
30+
).getValueOrError();
3031
}
3132

3233
/**
@@ -35,10 +36,10 @@ public Result<List<Rant>> getRants(Sort sort, int limit, int skip) {
3536
* @param term The term to search for.
3637
* @return The search results.
3738
*/
38-
public Result<List<Rant>> search(String term) {
39+
public List<Rant> search(String term) {
3940
return devRant.getRequestHandler().get(ApiEndpoint.SEARCH, ResultsFeedResponse.class,
4041
new BasicNameValuePair("term", term)
41-
);
42+
).getValueOrError();
4243
}
4344

4445
/**
@@ -48,11 +49,11 @@ public Result<List<Rant>> search(String term) {
4849
* @param skip How many rants to skip.
4950
* @return Weekly rants from the feed.
5051
*/
51-
public Result<List<Rant>> getWeekly(Sort sort, int skip) {
52+
public List<Rant> getWeekly(Sort sort, int skip) {
5253
return devRant.getRequestHandler().get(ApiEndpoint.WEEKLY, RantsFeedResponse.class,
5354
new BasicNameValuePair("sort", sort.toString()),
5455
new BasicNameValuePair("skip", String.valueOf(skip))
55-
);
56+
).getValueOrError();
5657
}
5758

5859
/**
@@ -62,11 +63,11 @@ public Result<List<Rant>> getWeekly(Sort sort, int skip) {
6263
* @param skip How many rants to skip.
6364
* @return Stories from the feed.
6465
*/
65-
public Result<List<Rant>> getStories(Sort sort, int skip) {
66+
public List<Rant> getStories(Sort sort, int skip) {
6667
return devRant.getRequestHandler().get(ApiEndpoint.STORIES, RantsFeedResponse.class,
6768
new BasicNameValuePair("sort", sort.toString()),
6869
new BasicNameValuePair("skip", String.valueOf(skip))
69-
);
70+
).getValueOrError();
7071
}
7172

7273
/**
@@ -75,9 +76,9 @@ public Result<List<Rant>> getStories(Sort sort, int skip) {
7576
* @param limit How many rants to get.
7677
* @return Collabs from the feed.
7778
*/
78-
public Result<List<Collab>> getCollabs(int limit) {
79-
return devRant.getRequestHandler().get(ApiEndpoint.COLLABS, RantsFeedResponse.class,
79+
public List<Collab> getCollabs(int limit) {
80+
return devRant.getRequestHandler().get(ApiEndpoint.COLLABS, CollabsFeedResponse.class,
8081
new BasicNameValuePair("limit", String.valueOf(limit))
81-
);
82+
).getValueOrError();
8283
}
8384
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.scorpiac.javarant;
2+
3+
public class NoSuchRantException extends RuntimeException {
4+
public NoSuchRantException(int id) {
5+
super("A rant with id " + id + " does not exist.");
6+
}
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.scorpiac.javarant;
2+
3+
public class NoSuchUserException extends RuntimeException {
4+
public NoSuchUserException(String username) {
5+
super("The user '" + username + "' does not exist.");
6+
}
7+
8+
public NoSuchUserException(int id) {
9+
super("A user with id " + id + " does not exist.");
10+
}
11+
}

src/main/java/com/scorpiac/javarant/Result.java

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.scorpiac.javarant.responses;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import com.scorpiac.javarant.News;
5+
import com.scorpiac.javarant.Rant;
6+
7+
import java.util.List;
8+
9+
abstract class AbstractRantsFeedResponse<T extends Rant> extends Response<List<T>> {
10+
@JsonProperty
11+
private News news; // TODO: use this.
12+
13+
@JsonProperty
14+
void setRants(List<T> rants) {
15+
value = rants;
16+
}
17+
}

0 commit comments

Comments
 (0)