-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
common handler to display server side validation errors (from rails)
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
rails-gwt/src/main/java/de/mkristian/gwt/rails/DisplayErrors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |