Skip to content

Commit

Permalink
Added new RobotServerException to capture and preserve the details
Browse files Browse the repository at this point in the history
of an error reponse from the server.
  • Loading branch information
briandealwis committed Mar 18, 2010
1 parent 176fd89 commit 369a075
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
import org.eclipsecon.e4rover.core.IArenaCamImage;
import org.eclipsecon.e4rover.core.IGame;
import org.eclipsecon.e4rover.core.IGameObject;
import org.eclipsecon.e4rover.core.IPlayer;
import org.eclipsecon.e4rover.core.IPlayerQueue;
import org.eclipsecon.e4rover.core.IPlayers;
import org.eclipsecon.e4rover.core.IRobot;
import org.eclipsecon.e4rover.core.IServerConstants;
import org.eclipsecon.e4rover.core.NotYourTurnException;
import org.eclipsecon.e4rover.core.RobotServerException;
import org.eclipsecon.e4rover.core.XmlSerializer;
import org.eclipsecon.e4rover.internal.core.ArenaCamImage;
import org.eclipsecon.e4rover.internal.core.ServerObject;
Expand Down Expand Up @@ -97,9 +97,8 @@ public void setWheelVelocity(int leftWheel, int rightWheel, String playerKey) th
int resp = httpClient.executeMethod(post);
if (resp == HttpStatus.SC_CONFLICT) {
throw new NotYourTurnException(post.getResponseBodyAsString());
}
if (resp != HttpStatus.SC_OK) {
throw new IOException("Server reported error " + resp + ". Message: " + post.getResponseBodyAsString());
} else if (resp != HttpStatus.SC_OK) {
throw new RobotServerException(resp, post.getURI(), post.getResponseBodyAsString());
}
} finally {
post.releaseConnection();
Expand All @@ -117,9 +116,9 @@ public int enterPlayerQueue(String playerKey) throws IOException {
int resp = httpClient.executeMethod(post);
if (resp == HttpStatus.SC_OK) {
return Integer.parseInt(post.getResponseHeader(IServerConstants.QUEUE_POSITION).getValue());
} else {
throw new RobotServerException(resp, post.getURI(), post.getResponseBodyAsString());
}
else
throw new IOException("Server reported error " + resp + ". URL: " + post.getURI() + ". Message: " + post.getResponseBodyAsString());
} finally {
post.releaseConnection();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.inject.Named;
import javax.inject.Provider;

import org.apache.commons.httpclient.HttpStatus;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.e4.core.services.StatusReporter;
import org.eclipse.e4.core.services.annotations.PostConstruct;
Expand All @@ -33,6 +34,7 @@
import org.eclipse.swt.widgets.Text;
import org.eclipsecon.e4rover.core.ContestPlatform;
import org.eclipsecon.e4rover.core.IPlayerQueue;
import org.eclipsecon.e4rover.core.RobotServerException;

public class PlayersQueueView extends Object {
/* SWT parent composite for this view */
Expand Down Expand Up @@ -82,6 +84,14 @@ public void modifyText(ModifyEvent e) {
public void widgetSelected(SelectionEvent e) {
try {
platform.enterPlayerQueue(keyText.getText());
} catch (RobotServerException rse) {
if (rse.getResponseCode() == HttpStatus.SC_CONFLICT) {
statusReporter.get().show(StatusReporter.ERROR, "Player queue full! Please try again later.",
null);
} else {
statusReporter.get().show(StatusReporter.ERROR, "An error occurred when requesting control",
rse);
}
} catch (IOException e1) {
statusReporter.get().show(StatusReporter.ERROR, "An error occurred when requesting control", e1);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.eclipsecon.e4rover.core;

import java.io.IOException;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.URI;

@SuppressWarnings("serial")
public class RobotServerException extends IOException {
protected int responseCode;
protected URI uri;
protected String message;

public RobotServerException(int responseCode, URI uri, String message) {
this.responseCode = responseCode;
this.uri = uri;
this.message = message;
}

public int getResponseCode() {
return responseCode;
}

public URI getUri() {
return uri;
}

public String getMessage() {
return message;
}

public String toString() {
return HttpStatus.getStatusText(responseCode) + " - " + uri
+ " - " + message;
}
}

0 comments on commit 369a075

Please sign in to comment.