pub const GQLSTATUS_GENERAL: &str = "42000";
pub const GQLSTATUS_SYNTAX: &str = "42N06";
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";
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_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,
DatabaseNotFound,
TransactionError,
ConstraintAlreadyExists,
EquivalentConstraintAlreadyExists,
ConstraintConflict,
ConstraintNotFound,
DependentConstraintManagedIndividually,
DuplicatedIndexName,
IndexAlreadyExists,
EquivalentSchemaRuleAlreadyExists,
IncompatibleGraphTypeDependence,
IndexDoesNotExist,
SystemDatabaseRequired,
ElementIdUnexpectedFormat,
}
impl NestedErrorClass {
pub fn binding_class(self) -> Option<&'static str> {
match self {
NestedErrorClass::ConstraintVerificationFailed
| NestedErrorClass::ArgumentError
| NestedErrorClass::EntityNotFound
| NestedErrorClass::ProcedureError
| NestedErrorClass::SyntaxError
| NestedErrorClass::SemanticError
| NestedErrorClass::UnsupportedAdministrationCommand
| NestedErrorClass::WriteOnReadOnlyAccessDatabase
| NestedErrorClass::DatabaseNotFound
| NestedErrorClass::TransactionError => Some(self.as_str()),
NestedErrorClass::ConstraintAlreadyExists
| NestedErrorClass::EquivalentConstraintAlreadyExists
| NestedErrorClass::ConstraintConflict
| NestedErrorClass::ConstraintNotFound
| NestedErrorClass::DependentConstraintManagedIndividually
| NestedErrorClass::DuplicatedIndexName
| NestedErrorClass::IndexAlreadyExists
| NestedErrorClass::EquivalentSchemaRuleAlreadyExists
| NestedErrorClass::IncompatibleGraphTypeDependence
| NestedErrorClass::IndexDoesNotExist
| NestedErrorClass::SystemDatabaseRequired
| NestedErrorClass::ElementIdUnexpectedFormat => 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(NestedErrorClass::DatabaseNotFound) => GQLSTATUS_DATABASE_NOT_FOUND,
Some(NestedErrorClass::UnsupportedAdministrationCommand) => {
GQLSTATUS_UNSUPPORTED_ADMINISTRATION_COMMAND
}
Some(NestedErrorClass::WriteOnReadOnlyAccessDatabase) => GQLSTATUS_WRITE_ON_READ_ONLY,
Some(NestedErrorClass::SyntaxError) => GQLSTATUS_SYNTAX,
Some(NestedErrorClass::ConstraintAlreadyExists) => GQLSTATUS_CONSTRAINT_DUPLICATED_NAME,
Some(NestedErrorClass::EquivalentConstraintAlreadyExists) => {
GQLSTATUS_CONSTRAINT_EQUIVALENT
}
Some(NestedErrorClass::ConstraintConflict) => GQLSTATUS_CONSTRAINT_CONFLICTING,
Some(
NestedErrorClass::ConstraintNotFound
| NestedErrorClass::DependentConstraintManagedIndividually,
) => GQLSTATUS_CONSTRAINT_DROP_FAILED,
Some(NestedErrorClass::DuplicatedIndexName | NestedErrorClass::IndexAlreadyExists) => {
GQLSTATUS_INDEX_DUPLICATED_NAME
}
Some(NestedErrorClass::EquivalentSchemaRuleAlreadyExists) => GQLSTATUS_INDEX_EQUIVALENT,
Some(NestedErrorClass::IndexDoesNotExist) => GQLSTATUS_INDEX_NOT_FOUND,
Some(NestedErrorClass::IncompatibleGraphTypeDependence) => {
GQLSTATUS_INCOMPATIBLE_GRAPH_TYPE_DEPENDENCE
}
Some(NestedErrorClass::SystemDatabaseRequired) => GQLSTATUS_NOT_SYSTEM_DATABASE,
Some(NestedErrorClass::ElementIdUnexpectedFormat) => GQLSTATUS_EXECUTION_FAILED,
Some(
NestedErrorClass::ConstraintVerificationFailed
| NestedErrorClass::ArgumentError
| NestedErrorClass::EntityNotFound
| NestedErrorClass::ProcedureError
| NestedErrorClass::SemanticError
| NestedErrorClass::TransactionError,
) => GQLSTATUS_GENERAL,
}
}
#[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);
}
}
}