Search in sources :

Example 1 with ErrorCode

use of org.exist.xquery.ErrorCodes.ErrorCode in project exist by eXist-db.

the class TryCatchExpression method extractErrorCode.

/**
 *  Extract and construct errorcode from error text.
 */
private ErrorCode extractErrorCode(final XPathException xpe) {
    // Get message from string
    final String message = xpe.getMessage();
    // if the 9th position has a ":" it is probably a custom error text
    if (':' == message.charAt(8)) {
        final String[] data = extractLocalName(xpe.getMessage());
        final ErrorCode errorCode = new ErrorCode(data[0], data[1]);
        LOG.debug("Parsed string '{}' for Errorcode. Qname='{}' message='{}'", xpe.getMessage(), data[0], data[1]);
        return errorCode;
    }
    // Convert xpe to Throwable
    Throwable retVal = xpe;
    // Swap with cause if present
    Throwable cause = xpe.getCause();
    if (cause != null && !(cause instanceof XPathException)) {
        retVal = cause;
    }
    // Fallback, create java error
    return new ErrorCodes.JavaErrorCode(retVal);
}
Also used : ErrorCode(org.exist.xquery.ErrorCodes.ErrorCode) JavaErrorCode(org.exist.xquery.ErrorCodes.JavaErrorCode) JavaErrorCode(org.exist.xquery.ErrorCodes.JavaErrorCode)

Example 2 with ErrorCode

use of org.exist.xquery.ErrorCodes.ErrorCode in project exist by eXist-db.

the class FunError method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // Define default values
    ErrorCode errorCode = DEFAULT_ERROR;
    String errorDesc = DEFAULT_DESCRIPTION;
    Sequence errorVal = Sequence.EMPTY_SEQUENCE;
    // Enter if one or more parameters are supplied
    if (args.length > 0) {
        // use 2nd argument for error description
        if (args.length > 1) {
            errorDesc = args[1].getStringValue();
        }
        // and construct error code
        if (!args[0].isEmpty()) {
            QName errorQName = ((QNameValue) args[0].itemAt(0)).getQName();
            String prefix = errorQName.getPrefix();
            if (prefix == null) {
                final String ns = errorQName.getNamespaceURI();
                prefix = getContext().getPrefixForURI(ns);
                errorQName = new QName(errorQName.getLocalPart(), errorQName.getNamespaceURI(), prefix);
            }
            errorCode = new ErrorCode(errorQName, errorDesc);
        }
        // If there is a third argument, use it.
        if (args.length == 3) {
            errorVal = args[2];
        }
    }
    if (LOG.isTraceEnabled()) {
        logger.trace("{}: {}", errorDesc, errorCode.toString());
    }
    throw new XPathException(this, errorCode, errorDesc, errorVal);
}
Also used : XPathException(org.exist.xquery.XPathException) QName(org.exist.dom.QName) QNameValue(org.exist.xquery.value.QNameValue) ErrorCode(org.exist.xquery.ErrorCodes.ErrorCode) Sequence(org.exist.xquery.value.Sequence)

Aggregations

ErrorCode (org.exist.xquery.ErrorCodes.ErrorCode)2 QName (org.exist.dom.QName)1 JavaErrorCode (org.exist.xquery.ErrorCodes.JavaErrorCode)1 XPathException (org.exist.xquery.XPathException)1 QNameValue (org.exist.xquery.value.QNameValue)1 Sequence (org.exist.xquery.value.Sequence)1