Make error messages clearer when required features are not enabled (#1822)

This commit is contained in:
Rushmore Mushambi 2023-04-23 13:08:21 +02:00 committed by GitHub
parent cd16d4af5c
commit a15c8c3564
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 292 additions and 178 deletions

View file

@ -20,6 +20,7 @@ use crate::api::ExtraFeatures;
use crate::api::Response;
use crate::api::Result;
use crate::api::Surreal;
use crate::error::Db as DbError;
use flume::Receiver;
use once_cell::sync::OnceCell;
#[cfg(feature = "protocol-http")]
@ -62,43 +63,66 @@ impl Connection for Any {
let mut features = HashSet::new();
match address.endpoint.scheme() {
#[cfg(feature = "kv-fdb")]
"fdb" => {
#[cfg(feature = "kv-fdb")]
{
features.insert(ExtraFeatures::Backup);
engine::local::native::router(address, conn_tx, route_rx);
conn_rx.into_recv_async().await??
}
#[cfg(feature = "kv-mem")]
#[cfg(not(feature = "kv-fdb"))]
return Err(
DbError::Ds("Cannot connect to the `foundationdb` storage engine as it is not enabled in this build of SurrealDB".to_owned()).into()
);
}
"mem" => {
#[cfg(feature = "kv-mem")]
{
features.insert(ExtraFeatures::Backup);
engine::local::native::router(address, conn_tx, route_rx);
conn_rx.into_recv_async().await??
}
#[cfg(not(feature = "kv-mem"))]
return Err(
DbError::Ds("Cannot connect to the `memory` storage engine as it is not enabled in this build of SurrealDB".to_owned()).into()
);
}
"file" | "rocksdb" => {
#[cfg(feature = "kv-rocksdb")]
"rocksdb" => {
{
features.insert(ExtraFeatures::Backup);
engine::local::native::router(address, conn_tx, route_rx);
conn_rx.into_recv_async().await??
}
#[cfg(feature = "kv-rocksdb")]
"file" => {
features.insert(ExtraFeatures::Backup);
engine::local::native::router(address, conn_tx, route_rx);
conn_rx.into_recv_async().await??
#[cfg(not(feature = "kv-rocksdb"))]
return Err(DbError::Ds(
"Cannot connect to the `rocksdb` storage engine as it is not enabled in this build of SurrealDB".to_owned(),
)
.into());
}
#[cfg(feature = "kv-tikv")]
"tikv" => {
#[cfg(feature = "kv-tikv")]
{
features.insert(ExtraFeatures::Backup);
engine::local::native::router(address, conn_tx, route_rx);
conn_rx.into_recv_async().await??
}
#[cfg(feature = "protocol-http")]
#[cfg(not(feature = "kv-tikv"))]
return Err(
DbError::Ds("Cannot connect to the `tikv` storage engine as it is not enabled in this build of SurrealDB".to_owned()).into()
);
}
"http" | "https" => {
#[cfg(feature = "protocol-http")]
{
features.insert(ExtraFeatures::Auth);
features.insert(ExtraFeatures::Backup);
let headers = http::default_headers();
@ -122,8 +146,16 @@ impl Connection for Any {
engine::remote::http::native::router(base_url, client, route_rx);
}
#[cfg(feature = "protocol-ws")]
#[cfg(not(feature = "protocol-http"))]
return Err(DbError::Ds(
"Cannot connect to the `HTTP` remote engine as it is not enabled in this build of SurrealDB".to_owned(),
)
.into());
}
"ws" | "wss" => {
#[cfg(feature = "protocol-ws")]
{
features.insert(ExtraFeatures::Auth);
let url = address.endpoint.join(engine::remote::ws::PATH)?;
#[cfg(any(feature = "native-tls", feature = "rustls"))]
@ -155,6 +187,13 @@ impl Connection for Any {
);
}
#[cfg(not(feature = "protocol-ws"))]
return Err(DbError::Ds(
"Cannot connect to the `WebSocket` remote engine as it is not enabled in this build of SurrealDB".to_owned(),
)
.into());
}
scheme => {
return Err(Error::Scheme(scheme.to_owned()).into());
}

View file

@ -15,6 +15,7 @@ use crate::api::ExtraFeatures;
use crate::api::Response;
use crate::api::Result;
use crate::api::Surreal;
use crate::error::Db as DbError;
use flume::Receiver;
use once_cell::sync::OnceCell;
use serde::de::DeserializeOwned;
@ -50,62 +51,99 @@ impl Connection for Any {
let mut features = HashSet::new();
match address.endpoint.scheme() {
#[cfg(feature = "kv-fdb")]
"fdb" => {
#[cfg(feature = "kv-fdb")]
{
engine::local::wasm::router(address, conn_tx, route_rx);
if let Err(error) = conn_rx.into_recv_async().await? {
return Err(error);
}
}
#[cfg(feature = "kv-indxdb")]
#[cfg(not(feature = "kv-fdb"))]
return Err(
DbError::Ds("Cannot connect to the `foundationdb` storage engine as it is not enabled in this build of SurrealDB".to_owned()).into()
);
}
"indxdb" => {
#[cfg(feature = "kv-indxdb")]
{
engine::local::wasm::router(address, conn_tx, route_rx);
if let Err(error) = conn_rx.into_recv_async().await? {
return Err(error);
}
}
#[cfg(feature = "kv-mem")]
#[cfg(not(feature = "kv-indxdb"))]
return Err(
DbError::Ds("Cannot connect to the `indxdb` storage engine as it is not enabled in this build of SurrealDB".to_owned()).into()
);
}
"mem" => {
#[cfg(feature = "kv-mem")]
{
engine::local::wasm::router(address, conn_tx, route_rx);
if let Err(error) = conn_rx.into_recv_async().await? {
return Err(error);
}
}
#[cfg(not(feature = "kv-mem"))]
return Err(
DbError::Ds("Cannot connect to the `memory` storage engine as it is not enabled in this build of SurrealDB".to_owned()).into()
);
}
"file" | "rocksdb" => {
#[cfg(feature = "kv-rocksdb")]
"rocksdb" => {
{
engine::local::wasm::router(address, conn_tx, route_rx);
if let Err(error) = conn_rx.into_recv_async().await? {
return Err(error);
}
}
#[cfg(feature = "kv-rocksdb")]
"file" => {
engine::local::wasm::router(address, conn_tx, route_rx);
if let Err(error) = conn_rx.into_recv_async().await? {
return Err(error);
}
#[cfg(not(feature = "kv-rocksdb"))]
return Err(DbError::Ds(
"Cannot connect to the `rocksdb` storage engine as it is not enabled in this build of SurrealDB".to_owned(),
)
.into());
}
#[cfg(feature = "kv-tikv")]
"tikv" => {
#[cfg(feature = "kv-tikv")]
{
engine::local::wasm::router(address, conn_tx, route_rx);
if let Err(error) = conn_rx.into_recv_async().await? {
return Err(error);
}
}
#[cfg(feature = "protocol-http")]
#[cfg(not(feature = "kv-tikv"))]
return Err(
DbError::Ds("Cannot connect to the `tikv` storage engine as it is not enabled in this build of SurrealDB".to_owned()).into()
);
}
"http" | "https" => {
#[cfg(feature = "protocol-http")]
{
features.insert(ExtraFeatures::Auth);
engine::remote::http::wasm::router(address, conn_tx, route_rx);
}
#[cfg(feature = "protocol-ws")]
#[cfg(not(feature = "protocol-http"))]
return Err(DbError::Ds(
"Cannot connect to the `HTTP` remote engine as it is not enabled in this build of SurrealDB".to_owned(),
)
.into());
}
"ws" | "wss" => {
#[cfg(feature = "protocol-ws")]
{
features.insert(ExtraFeatures::Auth);
let mut address = address;
address.endpoint = address.endpoint.join(engine::remote::ws::PATH)?;
@ -115,6 +153,13 @@ impl Connection for Any {
}
}
#[cfg(not(feature = "protocol-ws"))]
return Err(DbError::Ds(
"Cannot connect to the `WebSocket` remote engine as it is not enabled in this build of SurrealDB".to_owned(),
)
.into());
}
scheme => {
return Err(Error::Scheme(scheme.to_owned()).into());
}

View file

@ -24,7 +24,7 @@ pub enum Error {
#[error("There was an error processing a remote WS request")]
Ws(String),
/// There specified scheme does not match any supported protocol or storage engine
/// The specified scheme does not match any supported protocol or storage engine
#[error("Unsupported protocol or storage engine, `{0}`")]
Scheme(String),

View file

@ -97,8 +97,9 @@ impl Datastore {
/// ```
pub async fn new(path: &str) -> Result<Datastore, Error> {
match path {
#[cfg(feature = "kv-mem")]
"memory" => {
#[cfg(feature = "kv-mem")]
{
info!(target: LOG, "Starting kvs store in {}", path);
let v = super::mem::Datastore::new().await.map(|v| Datastore {
inner: Inner::Mem(v),
@ -106,9 +107,14 @@ impl Datastore {
info!(target: LOG, "Started kvs store in {}", path);
v
}
#[cfg(not(feature = "kv-mem"))]
return Err(Error::Ds("Cannot connect to the `memory` storage engine as it is not enabled in this build of SurrealDB".to_owned()));
}
// Parse and initiate an File database
#[cfg(feature = "kv-rocksdb")]
s if s.starts_with("file:") => {
#[cfg(feature = "kv-rocksdb")]
{
info!(target: LOG, "Starting kvs store at {}", path);
let s = s.trim_start_matches("file://");
let s = s.trim_start_matches("file:");
@ -118,9 +124,14 @@ impl Datastore {
info!(target: LOG, "Started kvs store at {}", path);
v
}
#[cfg(not(feature = "kv-rocksdb"))]
return Err(Error::Ds("Cannot connect to the `rocksdb` storage engine as it is not enabled in this build of SurrealDB".to_owned()));
}
// Parse and initiate an RocksDB database
#[cfg(feature = "kv-rocksdb")]
s if s.starts_with("rocksdb:") => {
#[cfg(feature = "kv-rocksdb")]
{
info!(target: LOG, "Starting kvs store at {}", path);
let s = s.trim_start_matches("rocksdb://");
let s = s.trim_start_matches("rocksdb:");
@ -130,9 +141,14 @@ impl Datastore {
info!(target: LOG, "Started kvs store at {}", path);
v
}
#[cfg(not(feature = "kv-rocksdb"))]
return Err(Error::Ds("Cannot connect to the `rocksdb` storage engine as it is not enabled in this build of SurrealDB".to_owned()));
}
// Parse and initiate an IndxDB database
#[cfg(feature = "kv-indxdb")]
s if s.starts_with("indxdb:") => {
#[cfg(feature = "kv-indxdb")]
{
info!(target: LOG, "Starting kvs store at {}", path);
let s = s.trim_start_matches("indxdb://");
let s = s.trim_start_matches("indxdb:");
@ -142,9 +158,14 @@ impl Datastore {
info!(target: LOG, "Started kvs store at {}", path);
v
}
#[cfg(not(feature = "kv-indxdb"))]
return Err(Error::Ds("Cannot connect to the `indxdb` storage engine as it is not enabled in this build of SurrealDB".to_owned()));
}
// Parse and initiate a TiKV database
#[cfg(feature = "kv-tikv")]
s if s.starts_with("tikv:") => {
#[cfg(feature = "kv-tikv")]
{
info!(target: LOG, "Connecting to kvs store at {}", path);
let s = s.trim_start_matches("tikv://");
let s = s.trim_start_matches("tikv:");
@ -154,9 +175,14 @@ impl Datastore {
info!(target: LOG, "Connected to kvs store at {}", path);
v
}
#[cfg(not(feature = "kv-tikv"))]
return Err(Error::Ds("Cannot connect to the `tikv` storage engine as it is not enabled in this build of SurrealDB".to_owned()));
}
// Parse and initiate a FoundationDB database
#[cfg(feature = "kv-fdb")]
s if s.starts_with("fdb:") => {
#[cfg(feature = "kv-fdb")]
{
info!(target: LOG, "Connecting to kvs store at {}", path);
let s = s.trim_start_matches("fdb://");
let s = s.trim_start_matches("fdb:");
@ -166,6 +192,10 @@ impl Datastore {
info!(target: LOG, "Connected to kvs store at {}", path);
v
}
#[cfg(not(feature = "kv-fdb"))]
return Err(Error::Ds("Cannot connect to the `foundationdb` storage engine as it is not enabled in this build of SurrealDB".to_owned()));
}
// The datastore path is not valid
_ => {
info!(target: LOG, "Unable to load the specified datastore {}", path);