Search in sources :

Example 1 with ParsingException

use of io.crate.sql.parser.ParsingException in project crate by crate.

the class SQLExceptions method createSQLActionException.

/**
     * Create a {@link SQLActionException} out of a {@link Throwable}.
     * If concrete {@link ElasticsearchException} is found, first transform it
     * to a {@link CrateException}
     */
public static SQLActionException createSQLActionException(Throwable e) {
    if (e instanceof SQLActionException) {
        return (SQLActionException) e;
    }
    e = esToCrateException(e);
    int errorCode = 5000;
    RestStatus restStatus = RestStatus.INTERNAL_SERVER_ERROR;
    if (e instanceof CrateException) {
        CrateException crateException = (CrateException) e;
        if (e instanceof ValidationException) {
            errorCode = 4000 + crateException.errorCode();
            restStatus = RestStatus.BAD_REQUEST;
        } else if (e instanceof ReadOnlyException) {
            errorCode = 4030 + crateException.errorCode();
            restStatus = RestStatus.FORBIDDEN;
        } else if (e instanceof ResourceUnknownException) {
            errorCode = 4040 + crateException.errorCode();
            restStatus = RestStatus.NOT_FOUND;
        } else if (e instanceof ConflictException) {
            errorCode = 4090 + crateException.errorCode();
            restStatus = RestStatus.CONFLICT;
        } else if (e instanceof UnhandledServerException) {
            errorCode = 5000 + crateException.errorCode();
        }
    } else if (e instanceof ParsingException) {
        errorCode = 4000;
        restStatus = RestStatus.BAD_REQUEST;
    } else if (e instanceof MapperParsingException) {
        errorCode = 4000;
        restStatus = RestStatus.BAD_REQUEST;
    }
    String message = e.getMessage();
    if (message == null) {
        if (e instanceof CrateException && e.getCause() != null) {
            // use cause because it contains a more meaningful error in most cases
            e = e.getCause();
        }
        StackTraceElement[] stackTraceElements = e.getStackTrace();
        if (stackTraceElements.length > 0) {
            message = String.format(Locale.ENGLISH, "%s in %s", e.getClass().getSimpleName(), stackTraceElements[0]);
        } else {
            message = "Error in " + e.getClass().getSimpleName();
        }
    } else {
        message = e.getClass().getSimpleName() + ": " + message;
    }
    return new SQLActionException(message, errorCode, restStatus, e.getStackTrace());
}
Also used : SQLActionException(io.crate.action.sql.SQLActionException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) RestStatus(org.elasticsearch.rest.RestStatus) ParsingException(io.crate.sql.parser.ParsingException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException)

Aggregations

SQLActionException (io.crate.action.sql.SQLActionException)1 ParsingException (io.crate.sql.parser.ParsingException)1 MapperParsingException (org.elasticsearch.index.mapper.MapperParsingException)1 RestStatus (org.elasticsearch.rest.RestStatus)1