Javascript 为什么可以’;我不能捕获MarkLogic请求中的某些异常吗?
我有一些代码来练习。在本例中,我在数据库中的Javascript 为什么可以’;我不能捕获MarkLogic请求中的某些异常吗?,javascript,marklogic,nosql,Javascript,Marklogic,Nosql,我有一些代码来练习。在本例中,我在数据库中的onDate元素上配置了dateTime元素范围索引(它将应用于XML元素和JSON属性)。我已将该索引设置为拒绝无效值。此设置意味着如果我尝试设置onDate元素的值,但它不能强制转换为dateTime或为null(JSON中的literalnull或XML中的xsi:nil=“true”),则更新将失败。(相反的行为是完全忽略无效值。) 我在MarkLogic 8.0-4的服务器端JavaScript中尝试了以下代码: 'use strict';
onDate
元素上配置了dateTime
元素范围索引(它将应用于XML元素和JSON属性)。我已将该索引设置为拒绝无效值。此设置意味着如果我尝试设置onDate
元素的值,但它不能强制转换为dateTime
或为null(JSON中的literalnull
或XML中的xsi:nil=“true”
),则更新将失败。(相反的行为是完全忽略无效值。)
我在MarkLogic 8.0-4的服务器端JavaScript中尝试了以下代码:
'use strict';
declareUpdate();
var errors = [];
var inputs = {
'/37107-valid.json': (new Date()).toISOString(),
'/37107-invalid.json': 'asdf', // Should throw an error
'/37107-null.json': null
};
for(var uri in inputs) {
try {
xdmp.documentInsert(
uri,
{ 'onDate': inputs[uri] },
xdmp.defaultPermissions(),
['37107'] // Collections
);
} catch(err) {
errors.push(err);
}
}
errors.length;
我希望我的请求会成功,并以
1===errors.length
结束,因为只有第二次插入会失败,因为'asdf'
不能作为日期时间强制转换,并且它不是空的。然而,我得到了一个XDMP-RANGEINDEX
错误,我的事务失败了。为什么我的try
/catch
在这里不起作用?问题是MarkLogic如何处理更新事务。MarkLogic不会在每次调用xdmp.documentInsert(…)
时实际更改数据,而是将所有更新排队,并在请求结束时自动应用它们。(这也是为什么在同一事务中看不到数据库更新的原因。)因此,直到循环执行并且数据库尝试提交排队的事务之后,才会抛出错误。这种行为在XQuery中是相同的(稍微简化):
我想,为了避免填选票,你两天之内不能接受自己的答案。
let $uris := (
'/37107-valid.xml',
'/37107-invalid.xml',
'/37107-null.xml'
)
let $docs := (
<onDate>{fn:current-dateTime()}</onDate>,
<onDate>asdf</onDate>,
<onDate xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
)
return
for $uri at $i in $uris
return
try {
xdmp:document-insert($uri, $docs[$i], (), ('37107'))
} catch($err) {
xdmp:log($err)
}
'use strict';
var errors = [];
var inputs = {
'/37107-valid.json': (new Date()).toISOString(),
'/37107-invalid.json': 'asdf',
'/37107-null.json': null
};
var insert = applyAs(
function(uri, value) {
return xdmp.documentInsert(
uri,
{ 'onDate': inputs[uri] },
xdmp.defaultPermissions(),
['37107']
);
},
{ isolation: 'different-transaction', transactionMode: 'update' },
'one'
);
for(var uri in inputs) {
try {
insert(uri, inputs[uri]);
} catch(err) {
errors.push(err);
}
}
errors.length; // Correctly returns 1
// <https://gist.github.com/jmakeig/0a331823ad9a458167f6>
function applyAs(fct, options, returnType /* 'many', 'one', 'iterable' (default) */) {
options = options || {};
return function() {
var params = Array.prototype.slice.call(arguments);
// Curry the function to include the params by closure.
// xdmp.invokeFunction requires that invoked functions have
// an arity of zero.
var f = (function() {
return fct.apply(null, params);
}).bind(this);
// Allow passing in user name, rather than id
if(options.user) { options.userId = xdmp.user(options.user); delete options.user; }
// Allow the functions themselves to declare their transaction mode
if(fct.transactionMode && !(options.transactionMode)) { options.transactionMode = fct.transactionMode; }
var result = xdmp.invokeFunction(f, options); // xdmp.invokeFunction returns a ValueIterator
switch(returnType) {
case 'one':
// return fn.head(result); // 8.0-5
return result.next().value;
case 'many':
return result.toArray();
case 'iterable':
default:
return result;
}
}
}