If you have a lot of different codes to handle, you could use
switch($ex/error:code) instead of if-then-else. A switch expression also has
the nice property of allowing multiple cases for a single return: I wish
typeswitch allowed that too.
switch($ex/error:code)
case 'a'
case 'b' return handle-errors-a-b()
default return xdmp:rethrow()
But in most cases I only care about one or two codes: this isn't java. Without
that 3.0 catch syntax, that looks pretty clean to me:
try {
... }
catch($ex) {
(: Rethrow by default. :)
if (not($ex/error:code = (...))) then xdmp:rethrow()
(: Handle known exceptions. :)
else ... }
I find the code easier to read if I keep the rethrow at the top of the
expression. Here's some similar code from taskbot:
try {
xdmp:spawn-function($fn, $eval-options) }
catch($ex) {
if ($ex/error:code ne 'XDMP-MAXTASKS') then xdmp:rethrow()
else switch($policy)
...
-- Mike
On 2 Jun 2014, at 09:44 , Florent Georges <[email protected]> wrote:
> Hi,
>
> XQuery 3.0 has a convenient selective error catching mechanism. You
> just use the error name (a QName) in the new catch clause, like this:
>
> try {
> ...
> }
> catch fn:FOER0000 {
> ...
> }
>
> Of course, fn:FOER0000 can be any QName, so you can throw your own
> error codes like this: error(xs:QName('my:error-name'), 'message'),
> and let your users catch it if they want. You can even catch "error
> names" like my:err1 | my:err2 to catch several codes at once, or my:*
> to catch all codes in the namespace my:.
>
> Unfortunately, it seems that all MarkLogic errors are thrown without
> any specific code (which results in the default code fn:FOER0000 for
> all of them), even though they all have a different code assigned
> (but in a different way): XDMP-INVZIP, etc.
>
> So for know, one has to catch all errors, inspect the value of
> $err:additional, see if its /error:error/error:code is the correct
> one, and if not rethrow the error.
>
> Should not instead the MarkLogic errors be thrown using each its own
> code? For instance in the namespace http://marklogic.com/xdmp/error,
> already existing, by having a mapping like:
>
> XDMP-INVZIP -> error:invzip
>
> allowing to use:
>
> try {
> ...
> }
> catch error:invzip {
> ...
> }
>
> instead of:
>
> try {
> ...
> }
> catch fn:FOER0000 { (: catch all non-declared errors :)
> if ( $err:additional/error:error/error:code eq 'XDMP-INVZIP' ) then
> ...
> else
> xdmp:rethrow()
> }
>
> Regards,
>
> --
> Florent Georges
> http://fgeorges.org/
> http://h2oconsulting.be/
>
>
> _______________________________________________
> General mailing list
> [email protected]
> http://developer.marklogic.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general