use crate::error_kinds::{ErrorKind, TckErrorClass};
use crate::Error;
pub fn unsupported_administration_command(statement: &str) -> Error {
Error::semantic(format!(
"{}: Unsupported administration command: {statement}",
ErrorKind::UnsupportedAdministrationCommand.as_str()
))
}
pub fn graph_not_found(name: &str) -> Error {
Error::database_not_found(name)
}
pub fn composite_only_graph_function(attempted: &str) -> Error {
Error::semantic(format!(
"{}: Dynamic graph lookup not allowed here. \
This feature is only available on composite databases.\n\
Attempted to access graph {attempted}",
TckErrorClass::SemanticError.as_str()
))
}
pub(crate) fn graph_selector_arity_error(attempted: &str, expected: usize, got: usize) -> Error {
Error::syntax(format!(
"{}: {attempted} expects {expected} argument(s), got {got}",
ErrorKind::InvalidNumberOfArguments.as_str()
))
}
pub fn check_graph_selector_arity(attempted: &str, name: &str, got: usize) -> Result<(), Error> {
match crate::namespace::graph_selector_arity(name) {
Some(expected) if got != expected => {
Err(graph_selector_arity_error(attempted, expected, got))
}
_ => Ok(()),
}
}
pub fn multiple_graphs(attempted: &str) -> Error {
Error::syntax(format!(
"Multiple graphs in the same query not allowed here. \
This feature is only available on composite databases.\n\
Attempted to access graph {attempted}"
))
}
pub fn element_id_no_namespace(id: &str) -> Error {
Error::semantic(format!(
"{}: No database is corresponding to `graph.byElementId({id})`. \
Verify that the elementId is correct.",
TckErrorClass::SemanticError.as_str()
))
}
pub fn unsupported_system_clauses(clauses: &str) -> Error {
Error::semantic(format!(
"{}: The following unsupported clauses were used: {clauses}. \n\
The system database supports a restricted set of Cypher clauses. \
The supported clause structure for procedure calls is: CALL, YIELD, RETURN. \
YIELD and RETURN clauses are optional. The order of the clauses is fixed \
and each can only occur once.",
TckErrorClass::SemanticError.as_str()
))
}
pub fn clauses_not_allowed_on_system_database(clauses: &str) -> Error {
Error::semantic(format!(
"{}: The following clauses are not allowed on a system database: {clauses}.",
TckErrorClass::SemanticError.as_str()
))
}
pub fn use_clause_with_administration_command() -> Error {
Error::syntax(format!(
"{}: The `USE` clause is not required for administration commands. \
Retry your query without the `USE` clause, and it will be routed automatically.",
ErrorKind::UseClauseWithAdministrationCommand.as_str()
))
}
pub fn administration_command_needs_system_database(command: &str) -> Error {
Error::semantic(format!(
"{}: This is an administration command and it should be executed \
against the system database: {command}",
ErrorKind::SystemDatabaseRequired.as_str()
))
}
pub fn system_ontologies_not_queryable() -> Error {
Error::semantic(format!(
"{}: This database is not queryable. Use the db.ontology.* procedures on the user database 'neo4j' instead.",
TckErrorClass::SemanticError.as_str()
))
}
pub fn read_only_write() -> Error {
Error::semantic(format!(
"{}: No write operations are allowed on this database. The database is in read-only mode on this Neo4j instance.",
ErrorKind::WriteOnReadOnlyAccessDatabase.as_str()
))
}
pub fn alter_unknown_database(name: &str) -> Error {
Error::semantic(format!(
"{}: Failed to alter the specified database '{name}': Database does not exist.",
ErrorKind::DatabaseNotFound.as_str()
))
}
pub fn database_does_not_exist(name: &str) -> Error {
graph_not_found(name)
}
pub fn ontology_neo4j_only() -> Error {
Error::semantic(format!(
"{}: Ontologies can only be stored for the user database 'neo4j'.",
TckErrorClass::SemanticError.as_str()
))
}
pub fn ontology_implicit_transaction_only(procedure: &str) -> Error {
Error::semantic(format!(
"{}: '{procedure}' can only be executed in an implicit transaction, \
but tried to execute in an explicit transaction.",
TckErrorClass::SemanticError.as_str()
))
}
pub fn call_in_transactions_implicit_only() -> Error {
Error::semantic(format!(
"{}: A query with 'CALL {{ ... }} IN TRANSACTIONS' can only be executed \
in an implicit transaction, but tried to execute in an explicit transaction.",
ErrorKind::TransactionError.as_str()
))
}
pub fn missing_transaction_id() -> Error {
Error::semantic(format!(
"{}: Missing transaction id to terminate, the transaction id can be found using `SHOW TRANSACTIONS`.",
TckErrorClass::SemanticError.as_str()
))
}
pub fn cross_database_write() -> Error {
Error::semantic(format!(
"{}: Writing to more than one database per transaction is not allowed.",
TckErrorClass::SemanticError.as_str()
))
}
pub fn namespace_change_in_transaction() -> Error {
Error::semantic(format!(
"{}: Cannot change the namespace while a transaction is open.",
ErrorKind::TransactionError.as_str()
))
}
#[cfg(test)]
mod tests {
use super::*;
fn assert_display_ends_with(error: &Error, expected_suffix: &str) {
let display = error.to_string();
assert!(
display.ends_with(expected_suffix),
"Display {display:?} does not end with {expected_suffix:?}"
);
}
fn assert_ce_text(error: &Error, expected_suffix: &str) {
assert!(
matches!(error, Error::Semantic(_)),
"expected Error::Semantic, got {error:?}"
);
assert_display_ends_with(error, expected_suffix);
}
fn assert_ce_syntax_text(error: &Error, expected_suffix: &str) {
assert!(
matches!(error, Error::Syntax(_)),
"expected Error::Syntax, got {error:?}"
);
assert_display_ends_with(error, expected_suffix);
}
#[test]
fn unsupported_administration_command_echoes_statement() {
assert_ce_text(
&unsupported_administration_command("CREATE DATABASE foo"),
"UnsupportedAdministrationCommand: Unsupported administration command: CREATE DATABASE foo",
);
}
#[test]
fn missing_transaction_id_is_the_22n06_text() {
assert_ce_text(
&missing_transaction_id(),
"SemanticError: Missing transaction id to terminate, the transaction id can be \
found using `SHOW TRANSACTIONS`.",
);
}
#[test]
fn administration_command_needs_system_database_names_the_command() {
for command in ["SHOW DATABASES", "ALTER DATABASE"] {
assert_ce_text(
&administration_command_needs_system_database(command),
&format!(
"SystemDatabaseRequired: This is an administration command and \
it should be executed against the system database: {command}"
),
);
}
}
fn assert_database_not_found(error: &Error, name: &str) {
assert!(
matches!(error, Error::DatabaseNotFound { name: n } if n == name),
"expected Error::DatabaseNotFound, got {error:?}"
);
let expected = format!("DatabaseNotFound: Graph not found: {name}");
assert_eq!(
error.to_string(),
Error::semantic(expected.clone()).to_string()
);
assert_eq!(
error.typed_detail_text().as_deref(),
Some(expected.as_str())
);
}
#[test]
fn graph_not_found_names_the_graph() {
assert_database_not_found(&graph_not_found("movies"), "movies");
}
#[test]
fn graph_selector_arity_guard_reads_the_one_table() {
for (name, _) in crate::GRAPH_SELECTOR_FUNCTIONS {
let error = check_graph_selector_arity(&format!("{name}()"), name, 9)
.expect_err("nine arguments are never the right count");
assert!(
matches!(error, Error::Syntax(_)),
"{name}: the arity refusal is a SyntaxError"
);
assert!(error.to_string().contains(name), "{error}");
assert!(
error.to_string().contains(&format!(
"{}:",
ErrorKind::InvalidNumberOfArguments.as_str()
)),
"{name}: {error}"
);
}
for (name, arity) in crate::GRAPH_SELECTOR_FUNCTIONS {
check_graph_selector_arity(name, name, arity).expect("the documented arity passes");
}
check_graph_selector_arity("other()", "other", 0).expect("not a selector");
}
#[test]
fn composite_only_graph_function_names_the_attempted_graph() {
assert_ce_text(
&composite_only_graph_function("graph.byName('neo4j')"),
"SemanticError: Dynamic graph lookup not allowed here. This feature is only available on composite databases.\nAttempted to access graph graph.byName('neo4j')",
);
}
#[test]
fn multiple_graphs_names_the_attempted_graph() {
assert_ce_syntax_text(
&multiple_graphs("system"),
"Multiple graphs in the same query not allowed here. This feature is only available on composite databases.\nAttempted to access graph system",
);
}
#[test]
fn element_id_no_namespace_echoes_the_id() {
assert_ce_text(
&element_id_no_namespace("4:00000000-0000-0000-0000-000000000000:n1"),
"SemanticError: No database is corresponding to `graph.byElementId(4:00000000-0000-0000-0000-000000000000:n1)`. Verify that the elementId is correct.",
);
}
#[test]
fn unsupported_system_clauses_lists_clauses() {
assert_ce_text(
&unsupported_system_clauses("MATCH"),
"SemanticError: The following unsupported clauses were used: MATCH. \nThe system database supports a restricted set of Cypher clauses. The supported clause structure for procedure calls is: CALL, YIELD, RETURN. YIELD and RETURN clauses are optional. The order of the clauses is fixed and each can only occur once.",
);
}
#[test]
fn clauses_not_allowed_on_system_database_lists_clauses() {
assert_ce_text(
&clauses_not_allowed_on_system_database("WITH"),
"SemanticError: The following clauses are not allowed on a system database: WITH.",
);
}
#[test]
fn use_clause_with_administration_command_text_is_stable() {
let error = use_clause_with_administration_command();
assert!(
matches!(error, Error::Syntax(_)),
"expected Error::Syntax, got {error:?}"
);
assert!(
error.to_string().ends_with(
"UseClauseWithAdministrationCommand: The `USE` clause is not required \
for administration commands. Retry your query without the `USE` clause, \
and it will be routed automatically."
),
"{}",
error
);
}
#[test]
fn read_only_write_text_is_stable() {
assert_ce_text(
&read_only_write(),
"WriteOnReadOnlyAccessDatabase: No write operations are allowed on this database. The database is in read-only mode on this Neo4j instance.",
);
}
#[test]
fn system_ontologies_not_queryable_text_is_stable() {
assert_ce_text(
&system_ontologies_not_queryable(),
"SemanticError: This database is not queryable. Use the db.ontology.* procedures on the user database 'neo4j' instead.",
);
}
#[test]
fn alter_unknown_database_names_the_database() {
assert_ce_text(
&alter_unknown_database("movies"),
"DatabaseNotFound: Failed to alter the specified database 'movies': Database does not exist.",
);
}
#[test]
fn database_does_not_exist_matches_graph_not_found() {
assert_database_not_found(&database_does_not_exist("movies"), "movies");
assert_eq!(database_does_not_exist("movies"), graph_not_found("movies"));
}
#[test]
fn ontology_procedure_texts_are_stable() {
assert_ce_text(
&ontology_neo4j_only(),
"SemanticError: Ontologies can only be stored for the user database 'neo4j'.",
);
assert_ce_text(
&ontology_implicit_transaction_only("db.ontology.set"),
"SemanticError: 'db.ontology.set' can only be executed in an implicit transaction, but tried to execute in an explicit transaction.",
);
assert_ce_text(
&ontology_implicit_transaction_only("db.ontology.drop"),
"SemanticError: 'db.ontology.drop' can only be executed in an implicit transaction, but tried to execute in an explicit transaction.",
);
assert_ce_text(
&cross_database_write(),
"SemanticError: Writing to more than one database per transaction is not allowed.",
);
}
#[test]
fn namespace_change_in_transaction_text_is_stable() {
assert_ce_text(
&namespace_change_in_transaction(),
"TransactionError: Cannot change the namespace while a transaction is open.",
);
}
}