I am creating forms in Orbeon 2021.1.2 PE and I have a problem with handling error calls. I’m creating HTTP service and Action via form builder. I hope to work it out and be able to stay with Builder.
I call API witch works similarly to Twitter, so it returns Error HTTP Status Codes also for business errors (see doc https://developer.twitter.com/ja/docs/basics/response-codes and https://developer.twitter.com/en/docs/twitter-ads-api/response-codes).
For easiest example: if I try to find something a if its not in database, I get code 404 with a detailed error in response body.
I need to get a status code + full body (headers will be good too) to the form. I don’t want to use modal windows (witch is default in Orbeon) to handle this call. It disturb users + calls often changes the entire workflow (what is visible, etc.). Some calls are even asynchronous.
What is the best solution to this problem in Orbeon? I tried a few things and find block in several places.
1] Solution through property oxf.fr.detail.process.action-service-error.*.*
Here is set default error modal window. I deleted it and found that I can get some values and write them in the form, for example:
xf:setvalue(ref="//control-1", value="event('response-status-code')")
Specifically, these are the values:
event('error-type')
event('response-status-code')
event('resource-uri')
event('response-headers') - get specific via event('response-headers')[lower-case(name) = 'content-lenght']/value
event('response-body') - This not working, it is still supported?
Because it is a global configuration, I start to working where to put these values. I tried to create my own instance but ended up with the fr:insert()
function.
I wanted to create a new instance for each submission and then enter separate values (event ('response-status-code')
, …). However, process with fr:insert()
does not work and the documentation has poor example (https://doc.orbeon.com/form-runner/advanced/buttons-and-processes/actions-xforms#xf-insert)
This do what i want, but in form:
<xf:insert context="xxf:instance('HTTP-ERROR-RESULT')" ref="responses" origin="xxf:instance('HTTP-ERROR-TEMPLATE')"/>
I tried to transform it to process, but with no luck:
xf:insert(into="xxf:instance('HTTP-ERROR-RESULT')/reponses", origin="xxf:instance('HTTP-ERROR-TEMPLATE')")
If this is the right way? How to fix my semantic error in xf:insert()
and how do I get response-body (event ('response-body')
doesn’t work…)? And is some way to get name of submission or action in process (I need some id for find in instance).
2] Another way I tried is through xforms-submit-error
in submit but it didn’t work too.
Under the structure of what the builder creates:
<xf:action event="xforms-submit-done" ev:observer="echo-submission">
<xf:action class="fr-set-control-value-action">
<xf:var name="control-name" value="'control-2'"/>
<xf:var name="control-value" value="/*"/>
</xf:action>
</xf:action>
I tried to insert the something similar but with xforms-submit-error
:
<xf:action event="xforms-submit-error" ev:observer="echo-submission">
<xf:action class="fr-set-control-value-action">
<xf:var name="control-name" value="'control-2'"/>
<xf:var name="control-value" value="/*"/>
</xf:action>
</xf:action>
I found that when using class="fr-service"
in submission, I cant catch xforms-submit-error
. So is the right solution to write the whole submission yourself outside the builder and work with xforms-submit-error
?
Or is there an another elegant solution in Orbeon? Thanks for any reply!
2
Answers
Based on answers from @avernet I make generic handler for
xforms-submit-error
. First, as was said above, I disable the default dialog by set this property.Then I put my own custom model to
WEB-INF/resources/forms/resources
. It's whole look this:Took attention to two conditions (in insert and first setvalue). Because of them model save only last error of one submission. If you want for some reason history, you can delete them.
Writing your own XForms will give you the most flexibility. You can put that XForms either directly in the form definition, using Edit Source in Form Builder, or in a custom model, which is a file on disk on the server, which I would recommend, see doc.
You seem to have found all the pieces (you did some good research there!), but here is a summary. If you have a service
my-service
, you can listen toxforms-submit-error
, and store the value of event attributes in your own instance. If using a custom model, that file would have the following content, and you can also put the content of the<xf:model>
below directly inside the<xf:model>
of the form definition through Edit Source in Form Builder. Here the logic just saves the status code in an instancemy-error-instance
, and then uses an<xf:message>
to show if for debugging.You’ll also want to disable the default dialog on errors by setting the following property.
Finally, I put below the full source of a form definition that you can use to test this, with a
my-service
HTTP service, making a request to http://httpbin.org/status/404, and called for form load, which I used to test the above custom model logic.