All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rethinkdb.ErrorBuilder Maven / Gradle / Ivy

There is a newer version: 2.3.2.20160729
Show newest version
package com.rethinkdb;

import com.rethinkdb.ast.Query;
import com.rethinkdb.ast.ReqlAst;
import com.rethinkdb.gen.proto.ErrorType;
import com.rethinkdb.gen.proto.ResponseType;
import com.rethinkdb.model.Backtrace;
import com.rethinkdb.gen.exc.*;

import java.util.Optional;
import java.util.function.Function;

public class ErrorBuilder {
    final String msg;
    final ResponseType responseType;
    Optional backtrace = Optional.empty();
    Optional errorType = Optional.empty();
    Optional term = Optional.empty();

    public ErrorBuilder(String msg, ResponseType responseType) {
        this.msg = msg;
        this.responseType = responseType;
    }

    public ErrorBuilder setBacktrace(Optional backtrace) {
        this.backtrace = backtrace;
        return this;
    }

    public ErrorBuilder setErrorType(Optional errorType) {
        this.errorType = errorType;
        return this;
    }

    public ErrorBuilder setTerm(Query query) {
        this.term = query.term;
        return this;
    }

    public ReqlError build() {
        assert (msg != null);
        assert (responseType != null);
        Function con;
        switch (responseType) {
            case CLIENT_ERROR:
                con = ReqlClientError::new;
                break;
            case COMPILE_ERROR:
                con = ReqlServerCompileError::new;
                break;
            case RUNTIME_ERROR: {
                con = errorType.>map(et -> {
                    switch (et) {
                        case INTERNAL:
                            return ReqlInternalError::new;
                        case RESOURCE_LIMIT:
                            return ReqlResourceLimitError::new;
                        case QUERY_LOGIC:
                            return ReqlQueryLogicError::new;
                        case NON_EXISTENCE:
                            return ReqlNonExistenceError::new;
                        case OP_FAILED:
                            return ReqlOpFailedError::new;
                        case OP_INDETERMINATE:
                            return ReqlOpIndeterminateError::new;
                        case USER:
                            return ReqlUserError::new;
                        case PERMISSION_ERROR:
                            return ReqlPermissionError::new;
                        default:
                            return ReqlRuntimeError::new;
                    }
                }).orElse(ReqlRuntimeError::new);
                break;
            }
            default:
                con = ReqlError::new;
        }
        ReqlError res = con.apply(msg);
        backtrace.ifPresent(res::setBacktrace);
        term.ifPresent(res::setTerm);
        return res;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy