Wednesday, July 20, 2011

Throwing exceptions in XQuery - fn:error()

Generating proper error messages is the foundation for a well maintainable application.

One should embed proper assertions in XQuery, using fn:error()

See chapter 3 in here http://www.w3.org/TR/xpath-functions/

A (dummy)example is here:

xquery version "1.0" encoding "Cp1252";
(:: pragma parameter="$anyType1" type="xs:anyType" ::)
(:: pragma type="xs:anyType" ::)

declare namespace xf = "http://tempuri.org/PVTests/raiseTest/";

declare function xf:raiseTest($anyType1 as element(*))
as element(*) {
if ($anyType1/id/text() = 23) then
fn:error(xs:QName('localnameblablabla'), '23 is not a good id')
else
};

declare variable $anyType1 as element(*) external;

xf:raiseTest($anyType1)


and you test it with



23




In OSB only the xs:QName($someString) constructor is supported, and the someString must be the localname (don't use a http://acme.com/errorcode1234 style, it will raise an exception)
although the normal QName constructor supports all possible variations:

http://download.oracle.com/javase/1.5.0/docs/api/javax/xml/namespace/QName.html#QName%28java.lang.String,%20java.lang.String,%20java.lang.String%29


Your error handler will receive a fault:

weblogic.xml.query.exceptions.XQueryUserException: line 1, column 1: localnameblablabla: 23 is not a good id:


<con:fault xmlns:con="http://www.bea.com/wli/sb/context">
<con:errorCode>BEA-382510</con:errorCode>
<con:reason>
OSB Assign action failed updating variable "bla": weblogic.xml.query.exceptions.XQueryUserException: line 1, column 1: localnameblablabla: 23 is not a good id
</con:reason>
<con:location>
<con:node>PipelinePairNode1</con:node>
<con:pipeline>PipelinePairNode1_request</con:pipeline>
<con:stage>stage1</con:stage>
<con:path>request-pipeline</con:path>
</con:location>
</con:fault>

No comments: