pub const GQLSTATUS_GENERAL: &str = "42000";
pub const GQLSTATUS_SYNTAX: &str = "42N06";
pub(crate) const GQLSTATUS_PROCEDURE_CALL: &str = "42001";
pub(crate) const GQLSTATUS_PARAMETER_MISSING: &str = "42N02";
pub(crate) const GQLSTATUS_TYPE: &str = "22G03";
pub const GQLSTATUS_DATABASE_NOT_FOUND: &str = "42N00";
pub(crate) const GQLSTATUS_UNSUPPORTED_ADMINISTRATION_COMMAND: &str = "51N27";
pub(crate) const GQLSTATUS_WRITE_ON_READ_ONLY: &str = "42N18";
const GQLSTATUS_ACCESS_MODE: &str = "42N02";
pub const ENTITY_NOT_FOUND_CODE: &str = "Neo.ClientError.Statement.EntityNotFound";
pub const ACCESS_MODE_CODE: &str = "Neo.ClientError.Statement.AccessMode";
pub(crate) const GQLSTATUS_INTERNAL: &str = "50N42";
pub(crate) const GQLSTATUS_CONSTRAINT_CREATION_FAILED: &str = "50N11";
const GQLSTATUS_CONSTRAINT_DROP_FAILED: &str = "50N12";
const GQLSTATUS_CONSTRAINT_EQUIVALENT: &str = "22N65";
const GQLSTATUS_CONSTRAINT_CONFLICTING: &str = "22N66";
const GQLSTATUS_CONSTRAINT_DUPLICATED_NAME: &str = "22N67";
const GQLSTATUS_INDEX_EQUIVALENT: &str = "22N70";
const GQLSTATUS_INDEX_DUPLICATED_NAME: &str = "22N71";
const GQLSTATUS_INDEX_NOT_FOUND: &str = "22N69";
const GQLSTATUS_INDEX_STILL_POPULATING: &str = "51N63";
const GQLSTATUS_INCOMPATIBLE_GRAPH_TYPE_DEPENDENCE: &str = "22NB2";
const GQLSTATUS_NOT_SYSTEM_DATABASE: &str = "51N28";
const GQLSTATUS_EXECUTION_FAILED: &str = "50N00";
macro_rules! nested_error_classes {
($($(#[$meta:meta])* $variant:ident,)+) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NestedErrorClass {
$($(#[$meta])* $variant,)+
}
impl NestedErrorClass {
pub(crate) const ALL: &'static [NestedErrorClass] =
&[$(NestedErrorClass::$variant,)+];
pub fn as_str(self) -> &'static str {
match self {
$(NestedErrorClass::$variant => stringify!($variant),)+
}
}
}
};
}
nested_error_classes! {
ConstraintVerificationFailed,
ArgumentError,
EntityNotFound,
ProcedureError,
SyntaxError,
SemanticError,
UnsupportedAdministrationCommand,
WriteOnReadOnlyAccessDatabase,
AccessMode,
DatabaseNotFound,
TransactionError,
ConstraintAlreadyExists,
EquivalentConstraintAlreadyExists,
ConstraintConflict,
ConstraintNotFound,
DependentConstraintManagedIndividually,
DuplicatedIndexName,
IndexAlreadyExists,
EquivalentSchemaRuleAlreadyExists,
IncompatibleGraphTypeDependence,
IndexDoesNotExist,
IndexStillPopulating,
SystemDatabaseRequired,
ElementIdUnexpectedFormat,
}
enum ClassRow {
Token(&'static str),
Coded {
own_code: &'static str,
gql_status: &'static str,
},
Condition(&'static str),
}
impl ClassRow {
fn gql_status(&self) -> &'static str {
match self {
ClassRow::Token(gql_status) | ClassRow::Condition(gql_status) => gql_status,
ClassRow::Coded { gql_status, .. } => gql_status,
}
}
}
impl NestedErrorClass {
fn row(self) -> ClassRow {
use NestedErrorClass::*;
match self {
ConstraintVerificationFailed => ClassRow::Token(GQLSTATUS_GENERAL),
ArgumentError => ClassRow::Token(GQLSTATUS_GENERAL),
EntityNotFound => ClassRow::Coded {
own_code: ENTITY_NOT_FOUND_CODE,
gql_status: GQLSTATUS_PROCEDURE_CALL,
},
ProcedureError => ClassRow::Token(GQLSTATUS_GENERAL),
SyntaxError => ClassRow::Token(GQLSTATUS_SYNTAX),
SemanticError => ClassRow::Token(GQLSTATUS_GENERAL),
UnsupportedAdministrationCommand => {
ClassRow::Token(GQLSTATUS_UNSUPPORTED_ADMINISTRATION_COMMAND)
}
WriteOnReadOnlyAccessDatabase => ClassRow::Token(GQLSTATUS_WRITE_ON_READ_ONLY),
AccessMode => ClassRow::Coded {
own_code: ACCESS_MODE_CODE,
gql_status: GQLSTATUS_ACCESS_MODE,
},
DatabaseNotFound => ClassRow::Token(GQLSTATUS_DATABASE_NOT_FOUND),
TransactionError => ClassRow::Token(GQLSTATUS_GENERAL),
ConstraintAlreadyExists => ClassRow::Condition(GQLSTATUS_CONSTRAINT_DUPLICATED_NAME),
EquivalentConstraintAlreadyExists => {
ClassRow::Condition(GQLSTATUS_CONSTRAINT_EQUIVALENT)
}
ConstraintConflict => ClassRow::Condition(GQLSTATUS_CONSTRAINT_CONFLICTING),
ConstraintNotFound | DependentConstraintManagedIndividually => {
ClassRow::Condition(GQLSTATUS_CONSTRAINT_DROP_FAILED)
}
DuplicatedIndexName | IndexAlreadyExists => {
ClassRow::Condition(GQLSTATUS_INDEX_DUPLICATED_NAME)
}
EquivalentSchemaRuleAlreadyExists => ClassRow::Condition(GQLSTATUS_INDEX_EQUIVALENT),
IncompatibleGraphTypeDependence => {
ClassRow::Condition(GQLSTATUS_INCOMPATIBLE_GRAPH_TYPE_DEPENDENCE)
}
IndexDoesNotExist => ClassRow::Condition(GQLSTATUS_INDEX_NOT_FOUND),
IndexStillPopulating => ClassRow::Condition(GQLSTATUS_INDEX_STILL_POPULATING),
SystemDatabaseRequired => ClassRow::Condition(GQLSTATUS_NOT_SYSTEM_DATABASE),
ElementIdUnexpectedFormat => ClassRow::Condition(GQLSTATUS_EXECUTION_FAILED),
}
}
pub fn binding_class(self) -> Option<&'static str> {
match self.row() {
ClassRow::Token(_) | ClassRow::Coded { .. } => Some(self.as_str()),
ClassRow::Condition(_) => None,
}
}
}
pub fn nested_error_class(payload: &str) -> Option<NestedErrorClass> {
let name = payload
.split_once(':')
.map_or(payload, |(name, _)| name)
.trim();
let leading = NestedErrorClass::ALL
.iter()
.copied()
.find(|class| class.as_str() == name);
if leading.is_some() {
return leading;
}
if payload.contains("ConstraintValidationFailed:")
|| payload.contains("ConstraintVerificationFailed:")
{
return Some(NestedErrorClass::ConstraintVerificationFailed);
}
None
}
pub(crate) fn stringly_gql_status(
nested: Option<NestedErrorClass>,
own: &'static str,
) -> &'static str {
match nested {
None => own,
Some(nested) => nested.row().gql_status(),
}
}
pub(crate) fn nested_own_code(nested: NestedErrorClass) -> Option<&'static str> {
match nested.row() {
ClassRow::Coded { own_code, .. } => Some(own_code),
ClassRow::Token(_) | ClassRow::Condition(_) => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn condition_tokens_keep_the_binding_class_vocabulary() {
assert_eq!(
NestedErrorClass::ArgumentError.binding_class(),
Some("ArgumentError")
);
assert_eq!(
NestedErrorClass::ConstraintAlreadyExists.binding_class(),
None
);
assert_eq!(NestedErrorClass::IndexDoesNotExist.binding_class(), None);
}
#[test]
fn nested_error_class_recovers_the_embedded_names() {
assert_eq!(
nested_error_class("ArgumentError: x"),
Some(NestedErrorClass::ArgumentError)
);
assert_eq!(
nested_error_class("outer context: ConstraintVerificationFailed: duplicate key"),
Some(NestedErrorClass::ConstraintVerificationFailed)
);
assert_eq!(
nested_error_class("outer: ConstraintValidationFailed: bad"),
Some(NestedErrorClass::ConstraintVerificationFailed)
);
assert_eq!(nested_error_class("no class here"), None);
assert_eq!(nested_error_class("nested ParameterMissing: p"), None);
assert_eq!(nested_error_class(""), None);
}
#[test]
fn every_nested_class_renders_its_embedded_token() {
for &class in NestedErrorClass::ALL {
let token = class.as_str();
let scanned = nested_error_class(&format!("{token}: x"))
.unwrap_or_else(|| panic!("token {token} not recognized by the scan"));
assert_eq!(scanned.as_str(), token);
}
}
}