This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal



May 9, 2011, 9:23 PM
47 Posts
topic has been resolvedResolved

how to trigger onError from a server side event?

  • Category: Server Side JavaScript
  • Platform: All
  • Release: 8.5.2
  • Role:
  • Tags:
  • Replies: 6
 Hi,
 
Can anyone tell me how to trigger an onError handler on the client side in response to a condition on the server-side script? I understand there are onStart, onComplete and onError handler parameters on the EventHandler object, and can of course trigger onStart and onComplete when things go right, but i want to catch known error conditions in my server script and let the user know why their requested action could not be carried out.
 
Specifically, for those that wish to know, I am deleting documents, but as we have document locking enabled, I must lock the document first. If the user cannot get a lock on the document, say if someone else has it open for editing, I need to inform them.
 
I have tried just 'throw'ing an error, but that just gets caught at the page level as an unexpected runtime error
 
Thanks for any insight: 
 
Rich 
May 9, 2011, 10:46 PM
57 Posts
Re: how to trigger onError from a server side event?
add FacesMessage to FacesContext, either for the entire page with
 
if (doc.isInConflictMode()){
    FacesMessage m2 = new FacesMessage("doc is edited by someone else");
    FacesContext.getCurrentInstance().addMessage(null, m2);
 }
 
or to individual UIComponent
if (doc.isInConflictMode()){
   UIComponent com = JSFUtil.findComponent("inputText1");
   FacesMessage m2 = new FacesMessage("doc is edited by someone else");
   FacesContext.getCurrentInstance().addMessage(
                com.getClientId(FacesContext.getCurrentInstance()), m2);
}
put "display error" or "display errors" control on designer, then it will display those error messages. 
 

May 10, 2011, 6:58 PM
47 Posts
Re: how to trigger onError from a server side event?
 Unfortunately this does not cause the onError() call back to be executed, onStart executes correctly before the post to the server, and then onComplete executes once the response is received. BTW, there is a helper function @ErrorMessage that encapsulates much of the work related to the facesContext.addMessage stuff.
 
I was able to get the error message to show in an xp:messages control, if it was within the context being refreshed (this action only does a partial refresh). Ideally I would have a single error control that displays a dialog when there is something to report, and I could cascade the partial refresh in my onError callback.
 
Rich 
 
May 11, 2011, 8:30 AM
57 Posts
Re: how to trigger onError from a server side event?
 
To put FacesMessage in FacesContext does not mean it is Error, its just a message, therefore it can not be caught in onError()
If need to cast the facesMessages as error, then make a custom validator, then throw an exception wrapping the FacesMessage, then onError() should be able to catch it? (I never used onError() before, did not know that script there will be invoked once there is serverSide error)
 
throw new javax.faces.validator.ValidatorException(
                    new javax.faces.application.FacesMessage("doc is inConflictMode");


 
If there is just a dialog rendering FacesMessages (not Error), why not just render the dialog with serverSide script?
var dialogA = getComponent('dialog1')
dialogA.show()
 
and inside of this dialog you can access all the FacesMessage through FacesContext

May 11, 2011, 4:30 PM
47 Posts
Re: how to trigger onError from a server side event?
 Thank you again for your input. The facesMessage solves part of the problem (what message to display), but as I am using partial refresh, and the error dialog will be outside the context of the partial refresh, even if I do show() the dialog on the server side, it won't be updated on the client side until I do a XSP.partialRefreshGet() on the dialog's container on the client. For this I need the onError()  function to trigger.
 
'throw"ing an exception does not trigger onError(). In fact it just returns the server generated error page for unhandled exceptions. 
May 11, 2011, 10:08 PM
57 Posts
Re: how to trigger onError from a server side event?
I did not know that onError you mentioned was associated with xhrpost/xhrget,
was thinking that validation+error management should be handled together at either server side or client side. to perform validation and error management at different sides is new stuff for me as well........always believe that code at client side runs at render response phase, and facesmessage is requestScope, will vanish after invoke application phrase,
 
just to put some final effort here eventhough I am aware that I am not at that level to answer this question
 
How to trigger an onError handler on the client side in response to a condition on the server-side
 
 instead of triggering an onError........
to synchronize client/server side script as in http://nodejs.org/, use Remote Services from extension library...then client side script will catch real object from server side, call back function not exists only in onError but also in that editor that we normally use....
May 12, 2011, 10:27 PM
47 Posts
Re: how to trigger onError from a server side event?
 Just to answer my own question, now that I have found it, you can trigger the onError() client script for a partial refresh if the status of the response is an error code like 500:
 
 facesContext.getExternalContext().getResponse().setStatus(500);
 
Rich 

This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal