You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Reinhard Pötz <re...@gmx.net> on 2005/03/14 14:46:55 UTC

[cForms] Errors coming from service layer

Imagine following scenario: You have a service layer that is exposed as web 
services. cForms already does as much validation as possible but some complex 
checks can only be performed by the backend.

If I call a webservice (via Axis client) this webservice can return errors (how 
this is done hasn't been defined yet).

Are there any best practices or experiences how to map errors coming from the 
service or domain layer to cForms widgets? (The error has to appear at widget 
level.)

-- 
Mag. (FH) Reinhard Pötz                    Beratung, Training, Coaching

web(log): http://www.poetz.cc                    mail: contact@poetz.cc
mobile: +43-(0)664-4544829                   fax: +49-(0)89-1488-289439
-----------------------------------------------------------------------

Re: [cForms] Errors coming from service layer

Posted by Vilya Harvey <vi...@gmail.com>.
Reinhard Poetz wrote:

> Thank you!
>
> This means that the service layer is aware of which widgets exist? I'm 
> not sure if I (and especially my customer) likes this bi-directional 
> dependency ...

I agree it's a bad idea for the service layer to know about the widgets. 
In our case there's a fairly simple correspondence between error types 
and widgets, so we're able to infer it all in the UI layer. For more 
complicated cases I could imagine using some kind of mapping, along the 
same sort of lines as the form bindings. I haven't tried that out, so I 
don't know how well it would work in practice.

BTW, the code to set a validation error on a widget looks like:

var myWidget = form.lookupWidget("...");
var myError = new 
Packages.org.apache.cocoon.forms.validation.ValidationError(myErrorMessage);
myWidget.setValidationError(myError);

(hope that doesn't get word-wrapped...)

Vil.

Re: [cForms] Errors coming from service layer

Posted by Reinhard Poetz <re...@apache.org>.
Sylvain Wallez wrote:
> Reinhard Poetz wrote:
> 
>> Sylvain Wallez wrote:
>>
>>> Reinhard Poetz wrote:
>>>
>>>> Vilya Harvey wrote:
>>>>
>>>>> Reinhard Pötz wrote:
>>>>>
>>>>>>
>>>>>> Imagine following scenario: You have a service layer that is 
>>>>>> exposed as web services. cForms already does as much validation as 
>>>>>> possible but some complex checks can only be performed by the 
>>>>>> backend.
>>>>>>
>>>>>> If I call a webservice (via Axis client) this webservice can 
>>>>>> return errors (how this is done hasn't been defined yet).
>>>>>>
>>>>>> Are there any best practices or experiences how to map errors 
>>>>>> coming from the service or domain layer to cForms widgets? (The 
>>>>>> error has to appear at widget level.)
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> In your flowscript, you can create your own ValidationError object 
>>>>> and explicitly set that on the apppropriate widget.
>>>>>
>>>>> What we did was to define our own type of exception which included 
>>>>> information about all validation errors that were found, then wrote 
>>>>> a simple(-ish) flowscript function which handled looking up the 
>>>>> relevant widgets, creating the error objects and setting them into 
>>>>> the widgets.
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> Thank you!
>>>>
>>>> This means that the service layer is aware of which widgets exist? 
>>>> I'm not sure if I (and especially my customer) likes this 
>>>> bi-directional dependency...
>>>
>>>
>>>
>>>
>>>
>>> Nono! Your validation code has to catch the exception and translate 
>>> it into a validation error. This means you can have "regular" 
>>> validation errors (i.e. the backend could be reached but detected 
>>> invalid data) and communication-level errors, e.g. "could not 
>>> validate data, try again later".
>>
>>
>>
>>
>> let's try to express this using some pseudo-code:
>>
>> var form = new Form(...);
>> var businessObject = getBusinessObjectFromServiceLayer();
>> form.load(businessObject);
>> form.show(...);
>>
>> form.save(businessObject);
>> var errorType;
>> try {
>>   saveBusinessObjectInBackend(businessObject);
>> } catch(ex) {
>>   errorType = ex.getErrorType();
>> }
>>
>> if(errorType == "user.already.exists") {
>>   form.lookupWidget("user").setValidationError("User already exists!");
>> }
>>
>> form.show(...);
>>
>> Do you mean something like this? The problem with this is that the 
>> service layer
>> can return *a lot* of different error types and I would have to write 
>> dozens of
>> ifs ... :-(
> 
> 
> 
> You should enclose this in a while loop :
> 
> var success = false;
> while (!success) {
>  form.showForm();
>  form.save(obj);
>  try {
>    saveBusinessObjectInBackend(obj);
>    success = true;
>  } catch(ex) {
>    ...
>  }
> }
> 
> Now about the particular exception handling, you can also define a 
> higher level system to propagate errors with a data structure that 
> associates an error code with a widget name and an error message.
> 
> If the validation is separated from the saveInBackend operation, don't 
> forget that you can define a <fd:validation> on the form object itself.

thank you guys!


-- 
Reinhard Pötz           Independant Consultant, Trainer & (IT)-Coach 

{Software Engineering, Open Source, Web Applications, Apache Cocoon}

                                        web(log): http://www.poetz.cc
--------------------------------------------------------------------

Re: [cForms] Errors coming from service layer

Posted by Sylvain Wallez <sy...@apache.org>.
Reinhard Poetz wrote:

> Sylvain Wallez wrote:
>
>> Reinhard Poetz wrote:
>>
>>> Vilya Harvey wrote:
>>>
>>>> Reinhard Pötz wrote:
>>>>
>>>>>
>>>>> Imagine following scenario: You have a service layer that is 
>>>>> exposed as web services. cForms already does as much validation as 
>>>>> possible but some complex checks can only be performed by the 
>>>>> backend.
>>>>>
>>>>> If I call a webservice (via Axis client) this webservice can 
>>>>> return errors (how this is done hasn't been defined yet).
>>>>>
>>>>> Are there any best practices or experiences how to map errors 
>>>>> coming from the service or domain layer to cForms widgets? (The 
>>>>> error has to appear at widget level.)
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> In your flowscript, you can create your own ValidationError object 
>>>> and explicitly set that on the apppropriate widget.
>>>>
>>>> What we did was to define our own type of exception which included 
>>>> information about all validation errors that were found, then wrote 
>>>> a simple(-ish) flowscript function which handled looking up the 
>>>> relevant widgets, creating the error objects and setting them into 
>>>> the widgets.
>>>
>>>
>>>
>>>
>>> Thank you!
>>>
>>> This means that the service layer is aware of which widgets exist? 
>>> I'm not sure if I (and especially my customer) likes this 
>>> bi-directional dependency...
>>
>>
>>
>>
>> Nono! Your validation code has to catch the exception and translate 
>> it into a validation error. This means you can have "regular" 
>> validation errors (i.e. the backend could be reached but detected 
>> invalid data) and communication-level errors, e.g. "could not 
>> validate data, try again later".
>
>
>
> let's try to express this using some pseudo-code:
>
> var form = new Form(...);
> var businessObject = getBusinessObjectFromServiceLayer();
> form.load(businessObject);
> form.show(...);
>
> form.save(businessObject);
> var errorType;
> try {
>   saveBusinessObjectInBackend(businessObject);
> } catch(ex) {
>   errorType = ex.getErrorType();
> }
>
> if(errorType == "user.already.exists") {
>   form.lookupWidget("user").setValidationError("User already exists!");
> }
>
> form.show(...);
>
> Do you mean something like this? The problem with this is that the 
> service layer
> can return *a lot* of different error types and I would have to write 
> dozens of
> ifs ... :-(


You should enclose this in a while loop :

var success = false;
while (!success) {
  form.showForm();
  form.save(obj);
  try {
    saveBusinessObjectInBackend(obj);
    success = true;
  } catch(ex) {
    ...
  }
}

Now about the particular exception handling, you can also define a 
higher level system to propagate errors with a data structure that 
associates an error code with a widget name and an error message.

If the validation is separated from the saveInBackend operation, don't 
forget that you can define a <fd:validation> on the form object itself.

Sylvain

-- 
Sylvain Wallez                        Anyware Technologies
http://apache.org/~sylvain            http://anyware-tech.com
Apache Software Foundation Member     Research & Technology Director


Re: [cForms] Errors coming from service layer

Posted by Glen Ezkovich <gl...@hard-bop.com>.
On Mar 14, 2005, at 9:50 AM, Reinhard Poetz wrote:

> Sylvain Wallez wrote:
>> Reinhard Poetz wrote:
>>> Vilya Harvey wrote:
>>>
>>>> Reinhard Pötz wrote:
>>>>
>>>>>
>>>>> Imagine following scenario: You have a service layer that is 
>>>>> exposed as web services. cForms already does as much validation as 
>>>>> possible but some complex checks can only be performed by the 
>>>>> backend.
>>>>>
>>>>> If I call a webservice (via Axis client) this webservice can 
>>>>> return errors (how this is done hasn't been defined yet).
>>>>>
>>>>> Are there any best practices or experiences how to map errors 
>>>>> coming from the service or domain layer to cForms widgets? (The 
>>>>> error has to appear at widget level.)
>>>>
>>>>
>>>>
>>>>
>>>> In your flowscript, you can create your own ValidationError object 
>>>> and explicitly set that on the apppropriate widget.
>>>>
>>>> What we did was to define our own type of exception which included 
>>>> information about all validation errors that were found, then wrote 
>>>> a simple(-ish) flowscript function which handled looking up the 
>>>> relevant widgets, creating the error objects and setting them into 
>>>> the widgets.
>>>
>>>
>>>
>>> Thank you!
>>>
>>> This means that the service layer is aware of which widgets exist? 
>>> I'm not sure if I (and especially my customer) likes this 
>>> bi-directional dependency...
>> Nono! Your validation code has to catch the exception and translate 
>> it into a validation error. This means you can have "regular" 
>> validation errors (i.e. the backend could be reached but detected 
>> invalid data) and communication-level errors, e.g. "could not 
>> validate data, try again later".
>
>
> let's try to express this using some pseudo-code:
>
> var form = new Form(...);
> var businessObject = getBusinessObjectFromServiceLayer();
> form.load(businessObject);
> form.show(...);
>
> form.save(businessObject);
> var errorType;
> try {
>   saveBusinessObjectInBackend(businessObject);
> } catch(ex) {
>   errorType = ex.getErrorType();
> }
>
> if(errorType == "user.already.exists") {
>   form.lookupWidget("user").setValidationError("User already exists!");
> }
>
> form.show(...);
>
> Do you mean something like this? The problem with this is that the 
> service layer
> can return *a lot* of different error types and I would have to write 
> dozens of
> ifs ... :-(

Not necessarily. Because you may have multiple validation errors for 
each request, its likely the Exception will contain a Collection of 
these errors. Its simple enough to just iterate over the widgets and 
find the errors associated with it, or iterate over the errors and find 
the associated widgets. Either way you only need one if. :-) This of 
course assumes that every field is validated by the service and that 
the service doesn't just validate until an error is encountered. If it 
is just a single error then the same principle can be applied by 
getting the name of the field that had the error and looking up the 
widget.

Again both of these assume that the Exception contains the 
field/widget/element/etc name.


Glen Ezkovich
HardBop Consulting
glen at hard-bop.com



A Proverb for Paranoids:
"If they can get you asking the wrong questions, they don't have to 
worry about answers."
- Thomas Pynchon Gravity's Rainbow


Re: [cForms] Errors coming from service layer

Posted by Reinhard Poetz <re...@apache.org>.
Sylvain Wallez wrote:
> Reinhard Poetz wrote:
> 
>> Vilya Harvey wrote:
>>
>>> Reinhard Pötz wrote:
>>>
>>>>
>>>> Imagine following scenario: You have a service layer that is exposed 
>>>> as web services. cForms already does as much validation as possible 
>>>> but some complex checks can only be performed by the backend.
>>>>
>>>> If I call a webservice (via Axis client) this webservice can return 
>>>> errors (how this is done hasn't been defined yet).
>>>>
>>>> Are there any best practices or experiences how to map errors coming 
>>>> from the service or domain layer to cForms widgets? (The error has 
>>>> to appear at widget level.)
>>>
>>>
>>>
>>>
>>> In your flowscript, you can create your own ValidationError object 
>>> and explicitly set that on the apppropriate widget.
>>>
>>> What we did was to define our own type of exception which included 
>>> information about all validation errors that were found, then wrote a 
>>> simple(-ish) flowscript function which handled looking up the 
>>> relevant widgets, creating the error objects and setting them into 
>>> the widgets.
>>
>>
>>
>> Thank you!
>>
>> This means that the service layer is aware of which widgets exist? I'm 
>> not sure if I (and especially my customer) likes this bi-directional 
>> dependency...
> 
> 
> 
> Nono! Your validation code has to catch the exception and translate it 
> into a validation error. This means you can have "regular" validation 
> errors (i.e. the backend could be reached but detected invalid data) and 
> communication-level errors, e.g. "could not validate data, try again 
> later".


let's try to express this using some pseudo-code:

var form = new Form(...);
var businessObject = getBusinessObjectFromServiceLayer();
form.load(businessObject);
form.show(...);

form.save(businessObject);
var errorType;
try {
   saveBusinessObjectInBackend(businessObject);
} catch(ex) {
   errorType = ex.getErrorType();
}

if(errorType == "user.already.exists") {
   form.lookupWidget("user").setValidationError("User already exists!");
}

form.show(...);

Do you mean something like this? The problem with this is that the service layer
can return *a lot* of different error types and I would have to write dozens of
ifs ... :-(

-- 
Reinhard Pötz           Independant Consultant, Trainer & (IT)-Coach

{Software Engineering, Open Source, Web Applications, Apache Cocoon}

                                        web(log): http://www.poetz.cc
--------------------------------------------------------------------


Re: [cForms] Errors coming from service layer

Posted by Vilya Harvey <vi...@gmail.com>.
Sylvain Wallez wrote:

> Reinhard Poetz wrote:
>
>> Thank you!
>>
>> This means that the service layer is aware of which widgets exist? 
>> I'm not sure if I (and especially my customer) likes this 
>> bi-directional dependency...
>
>
>
> Nono! Your validation code has to catch the exception and translate it 
> into a validation error. This means you can have "regular" validation 
> errors (i.e. the backend could be reached but detected invalid data) 
> and communication-level errors, e.g. "could not validate data, try 
> again later".

Exactly! Although in our case, the exception gets caught and translated 
into multiple validation errors, rather than just a single one.

Vil.

Re: [cForms] Errors coming from service layer

Posted by Sylvain Wallez <sy...@apache.org>.
Reinhard Poetz wrote:

> Vilya Harvey wrote:
>
>> Reinhard Pötz wrote:
>>
>>>
>>> Imagine following scenario: You have a service layer that is exposed 
>>> as web services. cForms already does as much validation as possible 
>>> but some complex checks can only be performed by the backend.
>>>
>>> If I call a webservice (via Axis client) this webservice can return 
>>> errors (how this is done hasn't been defined yet).
>>>
>>> Are there any best practices or experiences how to map errors coming 
>>> from the service or domain layer to cForms widgets? (The error has 
>>> to appear at widget level.)
>>
>>
>>
>> In your flowscript, you can create your own ValidationError object 
>> and explicitly set that on the apppropriate widget.
>>
>> What we did was to define our own type of exception which included 
>> information about all validation errors that were found, then wrote a 
>> simple(-ish) flowscript function which handled looking up the 
>> relevant widgets, creating the error objects and setting them into 
>> the widgets.
>
>
> Thank you!
>
> This means that the service layer is aware of which widgets exist? I'm 
> not sure if I (and especially my customer) likes this bi-directional 
> dependency...


Nono! Your validation code has to catch the exception and translate it 
into a validation error. This means you can have "regular" validation 
errors (i.e. the backend could be reached but detected invalid data) and 
communication-level errors, e.g. "could not validate data, try again later".

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }


Re: [cForms] Errors coming from service layer

Posted by Reinhard Poetz <re...@apache.org>.
Vilya Harvey wrote:
> Reinhard Pötz wrote:
> 
>>
>> Imagine following scenario: You have a service layer that is exposed 
>> as web services. cForms already does as much validation as possible 
>> but some complex checks can only be performed by the backend.
>>
>> If I call a webservice (via Axis client) this webservice can return 
>> errors (how this is done hasn't been defined yet).
>>
>> Are there any best practices or experiences how to map errors coming 
>> from the service or domain layer to cForms widgets? (The error has to 
>> appear at widget level.)
> 
> 
> In your flowscript, you can create your own ValidationError object and 
> explicitly set that on the apppropriate widget.
> 
> What we did was to define our own type of exception which included 
> information about all validation errors that were found, then wrote a 
> simple(-ish) flowscript function which handled looking up the relevant 
> widgets, creating the error objects and setting them into the widgets.

Thank you!

This means that the service layer is aware of which widgets exist? I'm not sure 
if I (and especially my customer) likes this bi-directional dependency ...

-- 
Reinhard Pötz           Independant Consultant, Trainer & (IT)-Coach 

{Software Engineering, Open Source, Web Applications, Apache Cocoon}

                                        web(log): http://www.poetz.cc
--------------------------------------------------------------------

Re: [cForms] Errors coming from service layer

Posted by Vilya Harvey <vi...@gmail.com>.
Reinhard Pötz wrote:

>
> Imagine following scenario: You have a service layer that is exposed 
> as web services. cForms already does as much validation as possible 
> but some complex checks can only be performed by the backend.
>
> If I call a webservice (via Axis client) this webservice can return 
> errors (how this is done hasn't been defined yet).
>
> Are there any best practices or experiences how to map errors coming 
> from the service or domain layer to cForms widgets? (The error has to 
> appear at widget level.)

In your flowscript, you can create your own ValidationError object and 
explicitly set that on the apppropriate widget.

What we did was to define our own type of exception which included 
information about all validation errors that were found, then wrote a 
simple(-ish) flowscript function which handled looking up the relevant 
widgets, creating the error objects and setting them into the widgets.

Hope that helps!
Vil.