Just to close the loop on this, the behavior of try/catch with 
xdmp:document-load is not a bug in this context.  It is  error that does not 
occur until commit time, and at that time the query has completed and is no 
longer catchable.  Some errors for xdmp:document-load are catchable (for 
example, if the file does not exist), but errors that do not occur until commit 
time are not catchable.  So the example in the doc is reasonable, but will not 
catch the non-utf8 errors.

If you want to catch errors like this, wrap the xdmp:document-load statement in 
an xdmp:eval and put the try/catch around the eval.

-Danny


From: [email protected] 
[mailto:[email protected]] On Behalf Of Tim Meagher
Sent: Tuesday, July 06, 2010 2:04 PM
To: 'General Mark Logic Developer Discussion'
Subject: Re: [MarkLogic Dev General] try-catch block seemingly notworking with 
xdmp:document-load() error

Thanks for the heads-up.  I used the wrong encoding to test the try-catch 
block.  I'm seeing some other errors my testing as well.
Interestingly enough the documentation for the try-catch block uses 
xmp:document-load for the example:

http://developer.marklogic.com:8040/4.1doc/docapp.xqy#display.xqy?fname=http://pubs/4.1doc/xml/xquery/enhanced.xml%2334280

Tim

________________________________
From: [email protected] 
[mailto:[email protected]] On Behalf Of Danny Sokolsky
Sent: Tuesday, July 06, 2010 4:50 PM
To: General Mark Logic Developer Discussion
Subject: Re: [MarkLogic Dev General] try-catch block seemingly notworking with 
xdmp:document-load() error

Hi Tim,

You should be able to do the same thing you were doing with document-load with 
document-get + document-insert, it will just be an extra line or 2 of code.  It 
should be roughly the same amount of work for MarkLogic, too, as that is 
essentially what xdmp:document-load does.  Something like;

let $uri := "some-uri"
let $node :=
try{
xdmp:document-get( .....rest of code here....
}
catch ($e) { ...do something here... }
return
xdmp:document-insert($uri, $node)

As to why document-load cannot catch the exception, I am not 100% sure.  It 
might be a bug or there might be some reason it works that way, maybe because 
the document-load call does not see the error until commit time when it is too 
late to catch.  I filed a bug on the MarkLogic end to sort that out.  But the 
workaround should be OK I think, and might actually be preferable as it catches 
the problem earlier.  In the catch, for example, you can try another encoding.  
Depending on your content, trying ISO-8859-1 for the encoding when it fails 
UTF-8 might solve the problem for some significant % of your data (if you are 
lucky....).

-Danny

From: [email protected] 
[mailto:[email protected]] On Behalf Of Tim Meagher
Sent: Tuesday, July 06, 2010 1:34 PM
To: 'General Mark Logic Developer Discussion'
Subject: Re: [MarkLogic Dev General] try-catch block seemingly not working with 
xdmp:document-load() error

Hi Danny,

Interesting - I tested it with xdmp:document-get and it successfully catches 
the error.  I wrap the call to xdmp:document-load in an http application and 
feed it the necessary options via an HTTP Post, so the advantage of using 
xdmp:document-load is that I can invoke it via HTTP from an XCC app without 
having to obtain and then rewrite the content using document-insert.  It would 
be much more advantageous if xdmp:document-load trapped the error, but I 
suppose I can add a test using xdmp-document-get as a prerequisite just to trap 
such errors prior to invoking xdmp:document-load.

What would it take for MarkLogic to trap this error in xdmp:document-load?  Is 
this a bug?

Thanks!

Tim

________________________________
From: [email protected] 
[mailto:[email protected]] On Behalf Of Danny Sokolsky
Sent: Tuesday, July 06, 2010 4:18 PM
To: General Mark Logic Developer Discussion
Subject: Re: [MarkLogic Dev General] try-catch block seemingly not working with 
xdmp:document-load() error

Hi Tim,

I'm not sure if this will help, but that error appears to be catchable in 
xdmp:document-get, so you should be able to do this using xdmp:document-get 
instead.  The combination of xdmp:document-get + xdmp:document-insert is pretty 
much the same as xdmp:document-load.

-Danny

From: [email protected] 
[mailto:[email protected]] On Behalf Of Tim Meagher
Sent: Tuesday, July 06, 2010 12:57 PM
To: 'General Mark Logic Developer Discussion'
Subject: [MarkLogic Dev General] try-catch block seemingly not working with 
xdmp:document-load() error

Hi Folks,

I wrapped an xdmp:document-load() in a try-catch block but it doesn't catch the 
error and allow me to take corrective action.

I experimented with it in cq and still get an error, for example, given the 
following query:

try {

let $uri := "/base-uri/docname.xml"

let $url := "http://[server]/[docpath]/docname.xml";
let $options :=
  <options xmlns="xdmp:document-load" >
    <uri>{$uri}</uri>
</options>

let $properties :=
  <properties>
    <datetime>2010-07-06-04:00</datetime>
  </properties>

return xdmp:document-load($url, $options)
}

catch($e) {
  "Problem loading file, received the following exception: ", $e
}


The result is as follows:
query evaluated in fabrication at Modules:/ as 1.0-ml (cq v4.1-1)
[1.0-ml] XDMP-DOCUTF8SEQ: xdmp:eval("try {&#13;&#10;&#13;&#10;let $uri := 
&quot:/base-uri/...", (), <options 
xmlns="xdmp:eval"><database>10474121819804666683</database><modules>15178358166571...</options>)
 -- Invalid UTF-8 escape sequence at http://[server]/[docpath]/docname.xml line 
68 -- document is not UTF-8 encoded
Stack trace:
in /eval.xqy line 111:
I expected the error to begin with: "Problem loading file, received the 
following exception: "
Tim Meagher
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to