Is it possible to retrieve the namespace of a raised error?

╄→尐↘猪︶ㄣ 提交于 2019-12-25 08:57:40

问题


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:


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.



来源:https://stackoverflow.com/questions/38119198/is-it-possible-to-retrieve-the-namespace-of-a-raised-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!