Is it possible to retrieve the namespace of a rais

2019-09-04 23:10发布

When I raise an error from within an XQuery query, for instance with:

error( fn:QName( 'http://example.com', 'XMPL0001' ), 'Conflict' )

... the following is returned by BaseX (be it when communicating with the server, or from within the GUI)

Stopped at ., 1/7:  
[XMPL0001] Conflict

Is it somehow possible to retrieve the namespace of the error (in this case http://example.com) as well?

I'm using a customized PHP client and I would like to use this information to prevent possible (future) conflicts with my custom error codes and parse the errors to throw either a standard BaseX\Exception or a custom SomeNamespace\Exception, depending on the namespace of the error.

I could, of course, simply use another error code pattern than the typical ABCD1234 XQuery pattern, to prevent possible (future) error code conflicts, but the possible use of a namespace appeals to me more, because I can then define an uniform Exception interface, such as:

interface ExceptionInterface
{
    public function getCategory(); // the 4 alpha character part
    public function getCode();  // the 4 digit part
}

I'm currently using BaseX 7.7.2, by the way.

1条回答
爷、活的狠高调
2楼-- · 2019-09-04 23:23

Yes, you can retrieve information about the error using a few variables in the error namespace, which are in scope of the try-catch statement, like so:

declare namespace err = "http://www.w3.org/2005/xqt-errors";

try {
    error( fn:QName( 'http://example.com', 'XMPL0001' ), 'Conflict' )
}
catch * {
    namespace-uri-from-QName($err:code)
}

This assumes that you are using XQuery 3.0.

查看更多
登录 后发表回答