Skip to content

Commit

Permalink
common handler to display server side validation errors (from rails)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkristian committed Jan 13, 2012
1 parent f92825b commit c0922db
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions rails-gwt/src/main/java/de/mkristian/gwt/rails/DisplayErrors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package de.mkristian.gwt.rails;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.fusesource.restygwt.client.FailedStatusCodeException;
import org.fusesource.restygwt.client.Method;

import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;


@Singleton
public class DisplayErrors {

public enum Type {
PRECONDITIONS, CONFLICT, GENERAL
}

private final Notice notice;

@Inject
public DisplayErrors(Notice notice){
this.notice = notice;
}

public Type showMessages(Method method, Throwable exp) {
if( exp instanceof FailedStatusCodeException){
switch(((FailedStatusCodeException)exp).getStatusCode()){
case 422:
JSONObject obj = JSONParser.parseStrict(method.getResponse().getText()).isObject();
if (obj != null){
StringBuffer buf = new StringBuffer();
for(String key : obj.keySet()) {
buf.append(key)
.append(": ")
.append(obj.get(key).toString().replaceAll("\\[\\]", ""))
.append("\n");
}
notice.error(buf.toString());
}
else {
// TODO
}
return Type.PRECONDITIONS;
case 409:
// TODO
return Type.CONFLICT;
default:
// TODO
}
}
return Type.GENERAL;
}
}

0 comments on commit c0922db

Please sign in to comment.