Passing error messages from server to client

I’m using SmartGWT EE 1.1, Spring/Hibernate integration

User Registration form inserts record via UserDAO object into USER table. There is a unique constraint on the userName column.
If unique constraint violated, JDBC exception is thrown.
I’m catching the exception and setting errors on
com.isomorphic.datasource.DSResponse object:
dsResponse.addError(“userName”, “User with specified username already exists”);

How can I get a handle to that error message on the client side in order to display it to the user???
I registered callback, but all of the following doesn’t work:

UserRegistrationForm.this.form.saveData(new DSCallback(){
public void execute(DSResponse response, java.lang.Object rawData,
DSRequest request){
form.hasErrors();
form.hasFieldErrors(“userName”);
form.getFieldErrors(“userName”);
form.getErrors();
response.getErrors();
}

If you’ve set the error correctly and the form field is appearing with an error icon/message next to it, those APIs will get the error message.

that’s how I set an error inside my DAO object:
com.isomorphic.datasource.DSResponse.addError(“userName”, “User with specified username already exists”);

Is it a right way? Why I can’t see error message on the front end?
Icon with error message next to the field is not populated.
Is something else missing???
Thanks.

No way to tell what you’re doing wrong from the limited code you’ve posted so far. Try posting complete code.

******************** UserDAO **********************

public DSResponse add(DSRequest dsRequest, User user)  
                                           throws Exception {    
    DSResponse dsResponse = new DSResponse();  
    Session session = null;
    
    try{
       session = sessionFactory.getCurrentSession();  
       session.save(user); 
       session.flush();
       dsResponse.setData(user);  
       dsResponse.setStatus(DSResponse.STATUS_SUCCESS); 
       log.info("Created account for "+user.getUserName());
    }catch(org.hibernate.JDBCException he){
       dsResponse.setStatus(DSResponse.STATUS_FAILURE); 
       Map<String, String> data = new HashMap<String, String>();
       if(he.getErrorCode() == 1){ 
    	   data.put("userName", "User with specified username already exists");  
           dsResponse.addError("userName", "User with specified username already exists");  
       }else{
    	   data.put("userName", "Error creating account"); 
    	   dsResponse.addError("userName", "Error creating account"); 
       }
       dsResponse.setData(data); 
       log.error("Error creating account for "+user.getUserName(), he);
       throw he; 
    }
    return dsResponse;  
}  

******************* UserDS ************************

<DataSource ID=“UserDS” serverType=“generic” testFileName=“ds/UserDS.data.xml”>
<fields>
<field name=“userName” type=“text” title=“Username” primaryKey=“true”/>
<field name=“firstName” type=“text” title=“First Name” />
<field name=“lastName” type=“text” title=“Last Name” />
<field name=“company” type=“text” title=“Company” />
<field name=“phone” type=“text” title=“Phone” />
<field name=“email” type=“text” title=“Email” />
<field name=“password” type=“text” title=“Password” required=“true”/>
<field name=“active” type=“text” title=“Active” hidden=“true” />

&lt;/fields&gt;   

&lt;serverObject lookupStyle="spring" bean="userDao"/&gt; 

</DataSource>

************************* DynamicForm **********************

    dataSource = DataSource.get("UserDS"); 
    form.setLayoutAlign(Alignment.CENTER);	    
    form.setLayoutAlign(VerticalAlignment.CENTER);	 
    form.setWidth("90%");
    form.setHeight("400");
    form.setCellSpacing(0);
    form.setCellPadding(8);
    form.setPadding(10);
    form.setBorder("1px gray");
    form.setDataSource(dataSource);  
    form.setUseAllDataSourceFields(false);  

// form.setBackgroundColor(“red”);

    HeaderItem header = new HeaderItem();  
    header.setAlign(Alignment.CENTER);
    header.setDefaultValue("User Registration");  
   
	TextItem userNameField = new TextItem("userName", "Username"); 
	userNameField.setAttribute("width", 250);
	userNameField.setRequired(true); 
	userNameField.setAlign(Alignment.CENTER);

    form.setFields(header, userNameField);

    final IButton nextBtn = new IButton("Submit");  
    nextBtn.setWidth(60);        
    nextBtn.setLayoutAlign(Alignment.CENTER);
    nextBtn.setDisabled(true);
    nextBtn.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {  
        public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {  
        	if(!UserRegistrationForm.this.form.validate(false)) return;            	
        	UserRegistrationForm.this.form.saveData(new DSCallback(){
        		public void execute(DSResponse response, java.lang.Object rawData, DSRequest request){
        			RCRDialog dlgProps = new RCRDialog("", true, false, 
        					                           RCRColors.WINDOW_BACKGROUND, 
        					                           RCRColors.WINDOW_BACKGROUND);
        			SubmitSuccessCallback callback = new SubmitSuccessCallback();
        			boolean statusOK = response.getStatus() == DSResponse.STATUS_SUCCESS;
        			String title = "Registration form";
        			if(statusOK){
        				//SC.say(title, "Account sucessfully created", callback);
        				SC.confirm(title, "Account sucessfully created", callback, dlgProps);
        			}else{
        				boolean hasFormErrors = form.hasErrors();
        				boolean hasFieldErrors = form.hasFieldErrors("userName");
        				String[] fieldErrors = form.getFieldErrors("userName");
        				Map err1 = form.getErrors();
        				if(err1 != null) err1.size();        				
        			}
        		}
        	});  
        	            	
        }  
    });

Wrong status code - use the status code for validation errors - you’re signalling general failure.

I changed status code to:
dsResponse.setStatus(DSResponse.STATUS_VALIDATION_ERROR);

but still no luck, on the client in the callback no error is returned.
Is there any specific setting to bound form to the datasource on the server side???

boolean hasFormErrors = form.hasErrors();
boolean hasFieldErrors = form.hasFieldErrors(“userName”);
String fieldErrors = form.getFieldErrors(“userName”);
Map err1 = form.getErrors();

No, no special setting is required, and there are automated tests that ensure that there are no regressions in this area.

Note that you should not both be calling setData() and adding errors, that doesn’t make sense. However most likely the code is not taking the pass you expect - add some logging to ensure you are making the calls you think you are.

the reason why I added setData while adding errors is to see if that will work.
Anyway, I tried all bunch of scenarios and nothing works. The only workaround I could come up with is catching an exception and rethrowing it with custom message:
throw new TXRollbackException(“User with specified username already exists”, he);

As a result warning dialog with custom exception comes up.
The question is now, how do I customize that dialog - background color.. and so on.
Thanks