com.rethinkdb.ErrorBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qj-rethinkdb-driver Show documentation
Show all versions of qj-rethinkdb-driver Show documentation
Java driver for RethinkDB (Modified from Official 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;
}
}