예제 #1
0
arangodb::aql::Graph* arangodb::lookupGraphByName(TRI_vocbase_t* vocbase,
                                                  std::string const& name) {
  SingleCollectionTransaction trx(StandaloneTransactionContext::Create(vocbase),
                                          GRAPHS, TRI_TRANSACTION_READ);

  int res = trx.begin();

  if (res != TRI_ERROR_NO_ERROR) {
    THROW_ARANGO_EXCEPTION_FORMAT(res, "while looking up graph '%s'",
                                  name.c_str());
  }
  VPackBuilder b;
  {
    VPackObjectBuilder guard(&b);
    b.add(StaticStrings::KeyString, VPackValue(name));
  }

  // Default options are enough here
  OperationOptions options;

  OperationResult result = trx.document(GRAPHS, b.slice(), options);

  // Commit or abort.
  res = trx.finish(result.code);

  if (!result.successful()) {
    THROW_ARANGO_EXCEPTION_FORMAT(result.code, "while looking up graph '%s'",
                                  name.c_str());
  }
  if (res != TRI_ERROR_NO_ERROR) {
    THROW_ARANGO_EXCEPTION_FORMAT(res, "while looking up graph '%s'",
                                  name.c_str());
  }

  return new arangodb::aql::Graph(result.slice());
}
void RestVocbaseBaseHandler::generateTransactionError(
    OperationResult const& result) {
  switch (result.code) {
    case TRI_ERROR_ARANGO_READ_ONLY:
      generateError(GeneralResponse::ResponseCode::FORBIDDEN, result.code,
                    "collection is read-only");
      return;

    case TRI_ERROR_ARANGO_UNIQUE_CONSTRAINT_VIOLATED:
      generateError(GeneralResponse::ResponseCode::CONFLICT, result.code,
                    "cannot create document, unique constraint violated");
      return;

    case TRI_ERROR_ARANGO_DOCUMENT_KEY_BAD:
      generateError(GeneralResponse::ResponseCode::BAD, result.code,
                    "invalid document key");
      return;

    case TRI_ERROR_ARANGO_DOCUMENT_HANDLE_BAD:
      generateError(GeneralResponse::ResponseCode::BAD, result.code,
                    "invalid document handle");
      return;

    case TRI_ERROR_ARANGO_INVALID_EDGE_ATTRIBUTE:
      generateError(GeneralResponse::ResponseCode::BAD, result.code,
                    "invalid edge attribute");
      return;

    case TRI_ERROR_ARANGO_OUT_OF_KEYS:
      generateError(GeneralResponse::ResponseCode::SERVER_ERROR, result.code,
                    "out of keys");
      return;

    case TRI_ERROR_ARANGO_DOCUMENT_KEY_UNEXPECTED:
      generateError(GeneralResponse::ResponseCode::BAD, result.code,
                    "collection does not allow using user-defined keys");
      return;

    case TRI_ERROR_ARANGO_DOCUMENT_NOT_FOUND:
      generateError(GeneralResponse::ResponseCode::NOT_FOUND,
                    TRI_ERROR_ARANGO_DOCUMENT_NOT_FOUND);
      return;

    case TRI_ERROR_ARANGO_DOCUMENT_TYPE_INVALID:
      generateError(GeneralResponse::ResponseCode::BAD, result.code);
      return;

    case TRI_ERROR_ARANGO_CONFLICT:
      generatePreconditionFailed(result.slice());
      return;

    case TRI_ERROR_CLUSTER_SHARD_GONE:
      generateError(GeneralResponse::ResponseCode::SERVER_ERROR, result.code,
                    "coordinator: no responsible shard found");
      return;

    case TRI_ERROR_CLUSTER_TIMEOUT:
      generateError(GeneralResponse::ResponseCode::SERVER_ERROR, result.code);
      return;

    case TRI_ERROR_CLUSTER_MUST_NOT_CHANGE_SHARDING_ATTRIBUTES:
    case TRI_ERROR_CLUSTER_NOT_ALL_SHARDING_ATTRIBUTES_GIVEN:
    case TRI_ERROR_CLUSTER_MUST_NOT_SPECIFY_KEY: {
      generateError(GeneralResponse::ResponseCode::BAD, result.code);
      return;
    }

    case TRI_ERROR_CLUSTER_UNSUPPORTED: {
      generateError(GeneralResponse::ResponseCode::NOT_IMPLEMENTED,
                    result.code);
      return;
    }

    case TRI_ERROR_FORBIDDEN: {
      generateError(GeneralResponse::ResponseCode::FORBIDDEN, result.code);
      return;
    }

    case TRI_ERROR_OUT_OF_MEMORY:
    case TRI_ERROR_LOCK_TIMEOUT:
    case TRI_ERROR_DEBUG:
    case TRI_ERROR_LOCKED:
    case TRI_ERROR_DEADLOCK: {
      generateError(GeneralResponse::ResponseCode::SERVER_ERROR, result.code);
      return;
    }

    default:
      generateError(
          GeneralResponse::ResponseCode::SERVER_ERROR, TRI_ERROR_INTERNAL,
          "failed with error: " + std::string(TRI_errno_string(result.code)));
  }
}