Skip to content

Commit

Permalink
gwt: Explicit exception catching and pretty printing.
Browse files Browse the repository at this point in the history
  • Loading branch information
calin-iorgulescu committed Feb 6, 2015
1 parent 9ccb08d commit a4ee3fc
Show file tree
Hide file tree
Showing 21 changed files with 184 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ro.pub.cs.vmchecker.client;

import ro.pub.cs.vmchecker.client.event.ErrorDisplayEvent;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import ro.pub.cs.vmchecker.client.i18n.VmcheckerConstants;

public class ExceptionHandler {

private static ExceptionHandler exceptionHandlerSingleton;

private HandlerManager eventBus;
private static VmcheckerConstants constants = GWT
.create(VmcheckerConstants.class);

private ExceptionHandler() { }

public static ExceptionHandler getInstance() {
if (exceptionHandlerSingleton == null) {
exceptionHandlerSingleton = new ExceptionHandler();
}
return exceptionHandlerSingleton;
}

public void setEventBus(HandlerManager eventBus) {
this.eventBus = eventBus;
}

public void exceptionError(Exception e) {
GWT.log("[exceptionError()]", e);
/* unhandled exception. get stack trace. */

if (eventBus == null) return;

StringBuilder sb = new StringBuilder();
for (StackTraceElement element : e.getStackTrace()) {
sb.append(element + "<br/>");
}

eventBus.fireEvent(new ErrorDisplayEvent(constants.exceptionError() + " " + constants.exceptionErrorText(),
"<b>" + constants.exceptionErrorContent() + "</b>:<br/>" + e.toString() +
"<br/>" + sb.toString() + "<br/>"));
}
}
13 changes: 9 additions & 4 deletions gwt/vmchecker-gui/src/ro/pub/cs/vmchecker/client/Vmchecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@

public class Vmchecker implements EntryPoint {

public void onModuleLoad() {
public void onModuleLoad() {
HandlerManager eventBus = new HandlerManager(null);
HTTPService rpcService = new HTTPService(eventBus);
AppController appCtrl = new AppController(eventBus, rpcService);
appCtrl.go(RootPanel.get());
try {
HTTPService rpcService = new HTTPService(eventBus);
AppController appCtrl = new AppController(eventBus, rpcService);
appCtrl.go(RootPanel.get());
} catch (Exception e) {
ExceptionHandler.getInstance().setEventBus(eventBus);
ExceptionHandler.getInstance().exceptionError(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public interface VmcheckerConstants extends Constants,
String unknownFormat();
String badServerStatusCode();
String popupCloseButton();
String exceptionError();
String exceptionErrorText();
String exceptionErrorContent();

String statisticsTitle();
String statisticsNoSubmissionAvailable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ serviceErrorContent = Content
serviceErrorStatusCode = Status Code
unknownFormat = Unknown format in response
badServerStatusCode = Bad server response
exceptionError = [Exception error]
exceptionErrorText = Unhandled exception
exceptionErrorContent = Exception Stack Trace

popupCloseButton = close

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ serviceErrorContent = Contenu
serviceErrorStatusCode = Status Code
unknownFormat = Format de réponse inconnu
badServerStatusCode = Mauvaise réponse du serveur
exceptionError = [Exception error]
exceptionErrorText = Unhandled exception
exceptionErrorContent = Exception Stack Trace

popupCloseButton = fermer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ serviceErrorContent = Conținut
serviceErrorStatusCode = Codul întors de server
unknownFormat = Format invalid în răspuns
badServerStatusCode = Răspuns invalid de la server
exceptionError = [Exception error]
exceptionErrorText = Unhandled exception
exceptionErrorContent = Exception Stack Trace

popupCloseButton = închide

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ public void onSubmitComplete(SubmitCompleteEvent event) {
* changed.
*/

if(event.getSource() == uploadNormalWidget.getUploadForm()) {
if (event.getSource() == uploadNormalWidget.getUploadForm()) {

/* Normal assignment file submission response */

UploadResponseDecoder responseDecoder = new UploadResponseDecoder();
UploadStatus response;
try {
response = responseDecoder.decode(event.getResults());
responseDecoder.parse(event.getResults());
if (!responseDecoder.errorsEncountered()) {
UploadStatus response = responseDecoder.getResult();
StatusChangedEvent statusChangeEvent = null;
if (response.status) {
statusChangeEvent = new StatusChangedEvent(StatusChangedEvent.StatusType.SUCCESS,
Expand All @@ -126,20 +126,20 @@ public void onSubmitComplete(SubmitCompleteEvent event) {
constants.uploadFileFail());
}
eventBus.fireEvent(statusChangeEvent);
} catch (Exception e) {
} else {
ServiceError se = new ServiceError(eventBus, HTTPService.UPLOAD_URL);
se.parseError(event.getResults());
}
}

if(event.getSource() == uploadLargeWidget.getMd5UploadForm()) {
if (event.getSource() == uploadLargeWidget.getMd5UploadForm()) {

/* Large assignment md5 submission response */

UploadResponseDecoder responseDecoder = new UploadResponseDecoder();
UploadStatus response;
try {
response = responseDecoder.decode(event.getResults());
responseDecoder.parse(event.getResults());
if (!responseDecoder.errorsEncountered()) {
UploadStatus response = responseDecoder.getResult();
StatusChangedEvent statusChangeEvent = null;
if (response.status) {
statusChangeEvent = new StatusChangedEvent(StatusChangedEvent.StatusType.SUCCESS,
Expand All @@ -150,37 +150,37 @@ public void onSubmitComplete(SubmitCompleteEvent event) {
}
loadAndDisplayUpload();
eventBus.fireEvent(statusChangeEvent);
} catch (Exception e) {
} else {
ServiceError se = new ServiceError(eventBus, HTTPService.UPLOAD_MD5_URL);
se.parseError(event.getResults());
}

}

if(event.getSource() == uploadLargeWidget.getEvaluationForm()) {
if (event.getSource() == uploadLargeWidget.getEvaluationForm()) {

/* Large assignment evaluation request response */

LargeEvaluationResponseDecoder responseDecoder = new LargeEvaluationResponseDecoder();
LargeEvaluationResponse response;
try {
response = responseDecoder.decode(event.getResults());
responseDecoder.parse(event.getResults());
if (!responseDecoder.errorsEncountered()) {
LargeEvaluationResponse response = responseDecoder.getResult();
StatusChangedEvent statusChangeEvent = null;
if(response.status) {
if (response.status) {
statusChangeEvent = new StatusChangedEvent(StatusChangedEvent.StatusType.SUCCESS,
constants.evaluateSuccess());
constants.evaluateSuccess());
} else {
statusChangeEvent = new StatusChangedEvent(StatusChangedEvent.StatusType.ERROR,
response.error);
if(response.error.equals("md5"))
if (response.error.equals("md5"))
statusChangeEvent = new StatusChangedEvent(StatusChangedEvent.StatusType.ERROR,
constants.evaluateFailMd5());
if(response.error.equals("zip"))
if (response.error.equals("zip"))
statusChangeEvent = new StatusChangedEvent(StatusChangedEvent.StatusType.ERROR,
constants.evaluateFailZip());
}
eventBus.fireEvent(statusChangeEvent);
} catch (Exception e) {
} else {
ServiceError se = new ServiceError(eventBus, HTTPService.BEGIN_EVALUATION_URL);
se.parseError(event.getResults());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,13 @@ public void onResponseReceived(Request request, Response response) {
return;
}

try {
T result = decoder.decode(response.getText());
if (result != null) {
callback.onSuccess(result);
} else {
GWT.log("Null result from Decoder: ", new NullPointerException());
se.parseError(response.getText());
}
} catch (Exception e) {
GWT.log("Decoder could not parse response.", e);
decoder.parse(response.getText());
if (decoder.errorsEncountered()) {
GWT.log("Decoder could not parse response.", null);
se.parseError(response.getText());
} else {
T result = decoder.getResult();
callback.onSuccess(result);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,23 @@ public void serverError(int statusCode, String statusMessage, String text) {
}

public void parseError(String text) {
try {
ErrorResponseDecoder decoder = new ErrorResponseDecoder();
ErrorResponse serviceError = decoder.decode(text);
ErrorResponseDecoder decoder = new ErrorResponseDecoder();
decoder.parse(text);
if (!decoder.errorsEncountered()) {
ErrorResponse serviceError = decoder.getResult();
if (serviceError.isAuthError()) {
eventBus.fireEvent(new AuthenticationEvent(AuthenticationEvent.EventType.ERROR));
} else {
eventBus.fireEvent(new ErrorDisplayEvent(constants.serviceError() + " " + serviceError.message, serviceError.trace));
}
} catch (Exception e) {
GWT.log("[parseError()]", e);
} else {
GWT.log("[parseError()]", null);
/* unexpected format */
eventBus.fireEvent(new ErrorDisplayEvent(constants.serviceError() + " " + constants.unknownFormat(),
"<b>" + constants.serviceErrorUrl() + "</b>: " + URL + "<br/><b>" + constants.serviceErrorContent() + "</b>:<br/>" + text));
"<b>" + constants.serviceErrorUrl() + "</b>: " + URL + "<br/>" +
"<b>" + constants.serviceErrorContent() + "</b>:<br/>"));
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,30 @@

import ro.pub.cs.vmchecker.client.model.Assignment;

public class AssignmentsListDecoder implements JSONDecoder<Assignment[]> {
public final class AssignmentsListDecoder extends JSONDecoder<Assignment[]> {

public static final String idKey = "assignmentId";
public static final String titleKey = "assignmentTitle";
public static final String storageTypeKey = "assignmentStorage";
public static final String storageHostKey = "assignmentStorageHost";
public static final String storageBasepathKey = "assignmentStorageBasepath";
public static final String deadlineKey = "deadline";
public static final String statementLinkKey = "statementLink";
private static final String idKey = "assignmentId";
private static final String titleKey = "assignmentTitle";
private static final String storageTypeKey = "assignmentStorage";
private static final String storageHostKey = "assignmentStorageHost";
private static final String storageBasepathKey = "assignmentStorageBasepath";
private static final String deadlineKey = "deadline";
private static final String statementLinkKey = "statementLink";

@Override
public Assignment[] decode(String text) throws Exception {
protected Assignment[] decode(String text) {
JSONValue jsonValue = JSONParser.parse(text);
JSONArray jsonArray;
JSONArray jsonArray = jsonValue.isArray();
Assignment[] assignments = null;

if ((jsonArray = jsonValue.isArray()) != null) {
assignments = new Assignment[jsonArray.size()];
for (int i = 0; i < jsonArray.size(); i++) {
assignments[i] = parseAssignment(jsonArray.get(i).isObject());
}
assignments = new Assignment[jsonArray.size()];
for (int i = 0; i < jsonArray.size(); i++) {
assignments[i] = parseAssignment(jsonArray.get(i).isObject());
}
return assignments;
}

private Assignment parseAssignment(JSONObject jsonObj) {
if (jsonObj == null)
return null;
String id = jsonObj.get(idKey).isString().stringValue();
String title = jsonObj.get(titleKey).isString().stringValue();
String storageType = jsonObj.get(storageTypeKey).isString().stringValue().toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;

public class AuthenticationResponseDecoder implements JSONDecoder<AuthenticationResponse> {
public final class AuthenticationResponseDecoder extends JSONDecoder<AuthenticationResponse> {

public static final String statusKey = "status";
public static final String fullnameKey = "fullname";
public static final String usernameKey = "username";
public static final String infoKey = "info";
private static final String statusKey = "status";
private static final String fullnameKey = "fullname";
private static final String usernameKey = "username";
private static final String infoKey = "info";

@Override
public AuthenticationResponse decode(String text) throws Exception {
protected AuthenticationResponse decode(String text) {
JSONValue jsonValue = JSONParser.parse(text);
JSONObject jsonObj = jsonValue.isObject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,24 @@

import ro.pub.cs.vmchecker.client.model.Course;

public class CoursesListDecoder implements JSONDecoder<Course[]> {
public final class CoursesListDecoder extends JSONDecoder<Course[]> {

public static final String idKey = "id";
public static final String titleKey = "title";
private static final String idKey = "id";
private static final String titleKey = "title";

@Override
public Course[] decode(String text) throws Exception {
protected Course[] decode(String text) {
JSONValue jsonValue = JSONParser.parse(text);
JSONArray jsonArray;
JSONArray jsonArray = jsonValue.isArray();

if ((jsonArray = jsonValue.isArray()) != null) {
Course[] courses = new Course[jsonArray.size()];
for (int i = 0; i < jsonArray.size(); i++) {
courses[i] = parseCourse(jsonArray.get(i).isObject());
}
return courses;
Course[] courses = new Course[jsonArray.size()];
for (int i = 0; i < jsonArray.size(); i++) {
courses[i] = parseCourse(jsonArray.get(i).isObject());
}

return null;
return courses;
}

private Course parseCourse(JSONObject jsonObj) {
if (jsonObj == null)
return null;
String id = jsonObj.get(idKey).isString().stringValue();
String title = jsonObj.get(titleKey).isString().stringValue();
return new Course(id, title);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

import ro.pub.cs.vmchecker.client.model.ErrorResponse;

public class ErrorResponseDecoder implements JSONDecoder<ErrorResponse> {
public final class ErrorResponseDecoder extends JSONDecoder<ErrorResponse> {

public static final String errorTypeKey = "errorType";
public static final String errorMessageKey = "errorMessage";
public static final String errorTraceKey = "errorTrace";
private static final String errorTypeKey = "errorType";
private static final String errorMessageKey = "errorMessage";
private static final String errorTraceKey = "errorTrace";

@Override
public ErrorResponse decode(String text) throws Exception {
protected ErrorResponse decode(String text) {
JSONValue jsonValue = JSONParser.parse(text);
JSONObject jsonObj = jsonValue.isObject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import ro.pub.cs.vmchecker.client.model.FileList;

public class FileListDecoder implements JSONDecoder<FileList> {
public final class FileListDecoder extends JSONDecoder<FileList> {

public static final String filenameKey = "fileName";
private static final String filenameKey = "fileName";

@Override
public FileList decode(String text) throws Exception {
protected FileList decode(String text) {

JSONValue jsonValue = JSONParser.parse(text);
JSONArray jsonArray = jsonValue.isArray();
Expand Down
Loading

0 comments on commit a4ee3fc

Please sign in to comment.