Search in sources :

Example 1 with RecoverableJsonException

use of org.apache.drill.exec.store.easy.json.parser.TokenIterator.RecoverableJsonException in project drill by apache.

the class JsonStructureParser method recover.

/**
 * Attempt recovery from a JSON syntax error by skipping to the next
 * record. The Jackson parser is quite limited in its recovery abilities.
 *
 * @return {@code true}  if another record can be read, {@code false}
 * if EOF.
 * @throws org.apache.drill.common.exceptions.UserException if the error is unrecoverable
 * @see <a href="https://issues.apache.org/jira/browse/DRILL-4653">DRILL-4653</a>
 * @see <a href="https://issues.apache.org/jira/browse/DRILL-5953">DRILL-5953</a>
 */
private boolean recover() {
    logger.warn("Attempting recovery from JSON syntax error. " + tokenizer.context());
    boolean firstAttempt = true;
    while (true) {
        while (true) {
            try {
                if (tokenizer.getParser().isClosed()) {
                    throw errorFactory().unrecoverableError();
                }
                JsonToken token = tokenizer.next();
                if (token == null) {
                    if (firstAttempt && !options().skipMalformedDocument) {
                        throw errorFactory().unrecoverableError();
                    }
                    return false;
                }
                if (token == JsonToken.NOT_AVAILABLE) {
                    return false;
                }
                if (token == JsonToken.END_OBJECT) {
                    break;
                }
                firstAttempt = false;
            } catch (RecoverableJsonException e) {
            // Ignore, keep trying
            }
        }
        try {
            JsonToken token = tokenizer.next();
            if (token == null || token == JsonToken.NOT_AVAILABLE) {
                return false;
            }
            if (token == JsonToken.START_OBJECT) {
                logger.warn("Attempting to resume JSON parse. " + tokenizer.context());
                tokenizer.unget(token);
                errorRecoveryCount++;
                return true;
            }
        } catch (RecoverableJsonException e) {
        // Ignore, keep trying
        }
    }
}
Also used : RecoverableJsonException(org.apache.drill.exec.store.easy.json.parser.TokenIterator.RecoverableJsonException) JsonToken(com.fasterxml.jackson.core.JsonToken)

Aggregations

JsonToken (com.fasterxml.jackson.core.JsonToken)1 RecoverableJsonException (org.apache.drill.exec.store.easy.json.parser.TokenIterator.RecoverableJsonException)1