Is there a safe way in Saxon-JS to switch to fallback XML locally when a request to API XML returns 404, timeout or other errors?
Added by Gary Cornelius about 4 years ago
Hi Guys,
I hope someone can help. I have a Saxon-JS application that reads XML that is dynamically created by a URL request and then will go on and process it to produce a result for the user. The difficulty I am having is that sometimes the application makes a request for data that isn't there and I need to then switch to another bit in the program and ask if the user wants to create new data there. The API request for XML usually gets a 404 in this situation... initially I just changed the webserver default 404 page to result as a quick fix but I don't like this solution and would like to replace this with some logic and better error messages in the XSLT instead so all the logic is together.
Can anyone suggest an effective way to try to get XML and if not successful return an XML error fragment from a local variable instead?
I had read through this which was most useful: https://www.saxonica.com/saxon-js/documentation/index.html#!development/http/termination
It seems like it is possible to schedule-action, detect a 404 or even a timeout/wrong header using what is documented and combine this with using xsl:try and xsl:catch and return valid XML input internally from variables in the XSLT but I can't seem to figure out how to get it working. I think I am most unsure exactly where to put the catch in the scenario with a 404 returned from a schedule action or another unexpected result. Does anyone have any example code for this they could share? Or could anyone help more if I dug out code one of my many attempts that didn't work?
Best wishes, Gary
Replies (5)
Please register to reply
RE: Is there a safe way in Saxon-JS to switch to fallback XML locally when a request to API XML returns 404, timeout or other errors? - Added by Martin Honnen about 4 years ago
Doesn't the ?status
of the response suffice to tell you whether you have 200 or 404 and in case of 404 you don't read the ?body
but return the XML you want? I am not sure where you need try/catch
. At the XPath level e.g. if (?status = 404) then $default-xml else ?body
should do.
RE: Is there a safe way in Saxon-JS to switch to fallback XML locally when a request to API XML returns 404, timeout or other errors? - Added by Michael Kay about 4 years ago
Yes, if it actually is a 404 then I think you can just check the status and respond accordingly: there's no XSLT error in this case.
In practice other things may happen: you might get an HTML page back, followed by an XML parsing error, or you might get an XML page whose content helpfully says "Oops: Error 404 occurred.". And of course you might get a timeout.
RE: Is there a safe way in Saxon-JS to switch to fallback XML locally when a request to API XML returns 404, timeout or other errors? - Added by Gary Cornelius about 4 years ago
Many thanks for this guys! It looks like I had got confused with knowing you could get the status with having tried to process the body. I also confused myself by changing the error pages on the API to produce XML on 404 and other error as this also appears to have removed the correct ?status from the response. I have something working now roughly along the lines...
<xsl:template name="handle-response">
<xsl:context-item as="map(*)" use="required"/>
<xsl:variable name="default-xml"><error/></xsl:variable>
<xsl:variable name="content" select="if (?status = 200) then $default-xml else ?body"/>
<xsl:result-document href="#page" method="ixsl:replace-content">
<xsl:copy select="$content"/>
I found it useful to debug:
<xsl:message>Message: <xsl:value-of select="?message"/></xsl:message>
<xsl:message>Type: <xsl:value-of select="?type"/></xsl:message>
<xsl:message>Status: <xsl:value-of select="?status"/></xsl:message>
It is great to be able to handle this all in the XSLT and I will add something for other errors too. I think can also add custom headers with the scheduled request and catch a few more errors that I can get from the HTTP requests. I am not really sure how to trap a timeout yet but will read back through the documentation and hope to work that out too!
RE: Is there a safe way in Saxon-JS to switch to fallback XML locally when a request to API XML returns 404, timeout or other errors? - Added by Gary Cornelius about 4 years ago
As follow up... I did have to ensure all the error pages from the server API being accessed produced valid XML/XHTML else I would get a parsing error page from the server that would display a message in the browser as follows after the 404:
"Misplaced or malformed XML: This page contains the following errors:error on line 15 at column 8: Opening and ending tag mismatch: hr line 0 and body Below is a rendering of the page up to the first error."
This was because the <hr>
was not closed in the standard html error message from the API server so I had to change that. I think this is probably a fault in the schedule-action code because the header of the responding API saxon-js was talking to did say it was outputting 'Content-Type: text/html;charset=iso-8859-1' so really the get request on ixsl:schedule-action should know it was not a XML content type!
Please register to reply