How to handle custom server side exceptions

123 views
Skip to first unread message

rakesh wagh

unread,
Oct 18, 2011, 3:26:12 AM10/18/11
to Google Web Toolkit
Hi, I am using gwt's bean validation framework for error validation. I
am trying to understand the mechanism to create custom
ConstraintViolationImpl in my server's service code and send it over
to client. Following is my code in persist() method:


Set<ConstraintViolation<?>> set = new HashSet<ConstraintViolation<?
>>();
PathImpl path = new PathImpl();
ConstraintDescriptor<?> descriptor = null;
ConstraintViolation<Person> violation = new
ConstraintViolationImpl<Person>("custom bean error", "custom bean
error", Person.class, person, person, person.getAddress(),
path.append("name"), null, ElementType.FIELD);

set.add(violation);
throw new ConstraintViolationException(set);


on persist, I expect onConstraintViolation() of Receiver to be
triggered. However I get the callback in onSuccess. What am I doing
wrong here?

Any kind of help and/or reference greatly appreciated.

Thanks,
Rakesh Wagh

Thomas Broyer

unread,
Oct 18, 2011, 7:35:56 AM10/18/11
to google-we...@googlegroups.com
Validation in RequestFactory is done after the object graph has been reconstructed but before any service method is invoked. It's the only place where validation errors will lead to onConstraintViolations being called back on the client side (for all the Receivers attached to the RequestContext).
When service method invocations are processed, each one can succeed or throw, independently of the others, and will cause the onSuccess or onFailure of the Receiver _for that particular invocation_ (the one passed to the to() or fire() method of Request –but not the fire() method of RequestContext!–) to be called back.
FYI, the global Receiver's (the one passed to the fire() method of RequestContext) onFailure method is only called when the object graph cannot be reconstructed or serialized on the server-side; in that case, the onFailure of all Receivers is also called. In any other case (except onConstraintViolations), onSuccess will be called (even if all invocations failed).

rakesh wagh

unread,
Oct 18, 2011, 2:02:16 PM10/18/11
to Google Web Toolkit
with that said, what is the right way to reconstruct server side
business validations(ConstraintViolation) on client (so that they can
be directly added to editor.setErrors(voilations))?

Also, I didnt understand how to set a callback in Request (to catch
server exception). At this point as you rightly noted I am using
requestContext's fire/Receiver pair.

Thanks

rakesh wagh

unread,
Oct 18, 2011, 7:15:20 PM10/18/11
to Google Web Toolkit
How I resolved this:
public class MyServiceLayerDecorator extends ServiceLayerDecorator {

public <T extends Object>
java.util.Set<javax.validation.ConstraintViolation<T>> validate(T
domainObject) {
Set<ConstraintViolation<?>> set = new HashSet<ConstraintViolation<?
>>();
PathImpl path = PathImpl.createNewPath("name");
ConstraintViolation<T> voilation = (ConstraintViolation<T>)new
ConstraintViolationImpl<Person>("custom bean error", "custom bean
error", Person.class, (Person)domainObject, (Person)domainObject,
((Person)domainObject).getAddress(), path, null, ElementType.FIELD);
Set<ConstraintViolation<T>> voilations =
super.validate(domainObject);
voilations.add(voilation);
return voilations;
};

rakesh wagh

unread,
Oct 18, 2011, 7:17:11 PM10/18/11
to Google Web Toolkit
also changed web.xml mapping of servlet class from
<servlet-
class>com.google.web.bindery.requestfactory.server.RequestFactoryServlet</
servlet-class>
to
<servlet-class>com.test.server.MyRequestFactoryServlet</servlet-class>

rakesh wagh

unread,
Oct 19, 2011, 5:10:43 PM10/19/11
to Google Web Toolkit
forgot to mention this:
public class MyRequestFactoryServlet extends RequestFactoryServlet {
private static final long serialVersionUID = 1L;

public MyRequestFactoryServlet() {
this(new DefaultExceptionHandler(), new MyServiceLayerDecorator());
}

public MyRequestFactoryServlet(ExceptionHandler exceptionHandler,
ServiceLayerDecorator... serviceDecorators) {
super(exceptionHandler, serviceDecorators);
Reply all
Reply to author
Forward
0 new messages