Permalink
Browse files

Error handling for login

  • Loading branch information...
1 parent 1712cd7 commit c082187b14e32e0d589366dad0347791cc5a7fd5 @mknittig committed Nov 30, 2009
@@ -140,8 +140,18 @@ public void login() /* throws ReviewboardException */{
loginRequest.setParameter("password", credentials.getPassword());
try {
- httpClient.executeMethod(loginRequest);
- cookie = loginRequest.getResponseHeader("Set-Cookie").getValue();
+ if (httpClient.executeMethod(loginRequest) == 200) {
+ if (reviewboardReader.isStatOK(loginRequest.getResponseBodyAsString())) {
+ cookie = loginRequest.getResponseHeader("Set-Cookie").getValue();
+ } else {
+ //TODO Use a custom exception for error handling
+ throw new RuntimeException(reviewboardReader.getErrorMessage(
+ loginRequest.getResponseBodyAsString()));
+ }
+ } else {
+ //TODO Use a custom exception for error handling
+ throw new RuntimeException("Review Board site is not up!");
+ }
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
@@ -267,11 +277,11 @@ public ReviewRequest newReviewRequest(ReviewRequest reviewRequest) throws Review
reviewRequest.setLastUpdated(newReviewRequest.getLastUpdated());
reviewRequest.setSubmitter(newReviewRequest.getSubmitter());
- //TODO
- //reviewRequest.getTargetPeople().add(newReviewRequest.getSubmitter());
- //reviewRequest.setSummary("Test");
- //reviewRequest.setDescription("Test");
- //updateReviewRequest(reviewRequest);
+ // TODO
+ // reviewRequest.getTargetPeople().add(newReviewRequest.getSubmitter());
+ // reviewRequest.setSummary("Test");
+ // reviewRequest.setDescription("Test");
+ // updateReviewRequest(reviewRequest);
return reviewRequest;
}
@@ -282,7 +292,7 @@ public ReviewRequest getReviewRequest(int reviewRequestId) throws ReviewboardExc
}
public List<Review> getReviews(int reviewRequestId) throws ReviewboardException {
- List<Review> result = reviewboardReader.readReviews(
+ List<Review> result = reviewboardReader.readReviews(
executeGet("/api/json/reviewrequests/" + reviewRequestId + "/reviews/"));
for (Review review : result) {
@@ -304,7 +314,7 @@ public void updateReviewRequest(ReviewRequest reviewRequest) throws ReviewboardE
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("status", reviewRequest.getStatus().toString());
- //parameters.put("public", reviewRequest.getPublicReviewRequest());
+ // parameters.put("public", reviewRequest.getPublicReviewRequest());
parameters.put("summary", reviewRequest.getSummary());
parameters.put("description", reviewRequest.getDescription());
parameters.put("testing_done", reviewRequest.getTestingDone());
@@ -397,7 +407,7 @@ private TaskData getTaskDataForReviewRequest(TaskRepository taskRepository,
List<String> diffs = new ArrayList<String>();
int iter = 1;
- //XXX Ugly hack, there should ba an API call for this function
+ // XXX Ugly hack, there should ba an API call for this function
while (true) {
try {
diffs.add(executeGet(String.format("/r/%d/diff/%d/raw/", reviewRequestId, iter)));
@@ -55,6 +55,30 @@
*/
public class RestfulReviewboardReader {
+ public boolean isStatOK(String source) throws ReviewboardException {
+ try {
+ JSONObject jsonStat = new JSONObject(source);
+ return jsonStat.getString("stat").equals("ok");
+ } catch (Exception e) {
+ throw new ReviewboardException(e.getMessage(), e);
+ }
+ }
+
+ public String getErrorMessage(String source) throws ReviewboardException {
+ try {
+ JSONObject jsonStat = new JSONObject(source);
+ if (jsonStat.getString("err").equals("fail")) {
+ JSONObject jsonError = jsonStat.getJSONObject("err");
+ return jsonError.getString("msg") + " (Errorcode: " +
+ jsonError.getString("code") + ")!";
+ } else {
+ throw new IllegalStateException("Request didn't fail!");
+ }
+ } catch (Exception e) {
+ throw new ReviewboardException(e.getMessage(), e);
+ }
+ }
+
public List<User> readUsers(String source) throws ReviewboardException {
try {
JSONObject jsonUsers = new JSONObject(source);

0 comments on commit c082187

Please sign in to comment.