use crate::nested_class::{
nested_error_class, stringly_gql_status, GQLSTATUS_CONSTRAINT_CREATION_FAILED,
GQLSTATUS_DATABASE_NOT_FOUND, GQLSTATUS_GENERAL, GQLSTATUS_INTERNAL,
GQLSTATUS_PARAMETER_MISSING, GQLSTATUS_SYNTAX, GQLSTATUS_TYPE,
};
use relate_core::{ArithmeticFailure, Error};
pub const SYNTAX_ERROR_CODE: &str = "Neo.ClientError.Statement.SyntaxError";
pub const PROCEDURE_NOT_FOUND_CODE: &str = "Neo.ClientError.Procedure.ProcedureNotFound";
pub const PARAMETER_MISSING_CODE: &str = "Neo.ClientError.Statement.ParameterMissing";
pub const DATABASE_NOT_FOUND_CODE: &str = "Neo.ClientError.Database.DatabaseNotFound";
pub const SEMANTIC_ERROR_CODE: &str = "Neo.ClientError.Statement.SemanticError";
pub const TYPE_ERROR_CODE: &str = "Neo.ClientError.Statement.TypeError";
pub const ARGUMENT_ERROR_CODE: &str = "Neo.ClientError.General.InvalidArguments";
pub const ARGUMENT_VALUE_ERROR_CODE: &str = "Neo.ClientError.Statement.ArgumentError";
pub const ARITHMETIC_ERROR_CODE: &str = "Neo.ClientError.Statement.ArithmeticError";
pub const CONSTRAINT_VALIDATION_FAILED_CODE: &str =
"Neo.ClientError.Schema.ConstraintValidationFailed";
pub const TRANSACTION_TIMED_OUT_CODE: &str = "Neo.ClientError.Transaction.TransactionTimedOut";
pub const OUTDATED_CODE: &str = "Neo.TransientError.Transaction.Outdated";
pub const TRANSACTION_OUT_OF_MEMORY_CODE: &str =
"Neo.ClientError.General.TransactionOutOfMemoryError";
pub const MEMORY_POOL_OUT_OF_MEMORY_CODE: &str =
"Neo.TransientError.General.MemoryPoolOutOfMemoryError";
pub const UNKNOWN_ERROR_CODE: &str = "Neo.DatabaseError.General.UnknownError";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorStage {
Compile,
Runtime,
Internal,
}
impl ErrorStage {
pub fn as_str(self) -> &'static str {
match self {
ErrorStage::Compile => "compile",
ErrorStage::Runtime => "runtime",
ErrorStage::Internal => "internal",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ErrorInfo {
pub neo4j_code: &'static str,
pub gql_status: &'static str,
pub stage: ErrorStage,
pub retryable: bool,
}
pub fn client_message(e: &Error) -> std::borrow::Cow<'_, str> {
match error_info(e).stage {
ErrorStage::Compile | ErrorStage::Runtime => std::borrow::Cow::Owned(e.to_string()),
ErrorStage::Internal => std::borrow::Cow::Borrowed(INTERNAL_MESSAGE),
}
}
pub fn log_internal_original(e: &Error, boundary: &str) {
if let ErrorStage::Internal = error_info(e).stage {
eprintln!("{boundary} internal error: {e}");
}
}
pub const INTERNAL_MESSAGE: &str = "internal error";
pub const INTERNAL_ERROR_INFO: ErrorInfo = ErrorInfo {
neo4j_code: UNKNOWN_ERROR_CODE,
gql_status: GQLSTATUS_INTERNAL,
stage: ErrorStage::Internal,
retryable: false,
};
pub fn error_info(e: &Error) -> ErrorInfo {
use ErrorStage::{Compile, Runtime};
match e {
Error::Syntax(payload) => ErrorInfo {
neo4j_code: SYNTAX_ERROR_CODE,
gql_status: stringly_gql_status(nested_error_class(payload), GQLSTATUS_SYNTAX),
stage: Compile,
retryable: false,
},
Error::ProcedureNotFound { .. } => ErrorInfo {
neo4j_code: PROCEDURE_NOT_FOUND_CODE,
gql_status: GQLSTATUS_SYNTAX,
stage: Compile,
retryable: false,
},
Error::ParameterMissing(_) => ErrorInfo {
neo4j_code: PARAMETER_MISSING_CODE,
gql_status: GQLSTATUS_PARAMETER_MISSING,
stage: Compile,
retryable: false,
},
Error::DatabaseNotFound { .. } => ErrorInfo {
neo4j_code: DATABASE_NOT_FOUND_CODE,
gql_status: GQLSTATUS_DATABASE_NOT_FOUND,
stage: Compile,
retryable: false,
},
Error::ArgumentError(_) => ErrorInfo {
neo4j_code: ARGUMENT_ERROR_CODE,
gql_status: GQLSTATUS_GENERAL,
stage: Compile,
retryable: false,
},
Error::ArgumentValue { .. } => ErrorInfo {
neo4j_code: ARGUMENT_VALUE_ERROR_CODE,
gql_status: GQLSTATUS_GENERAL,
stage: Runtime,
retryable: false,
},
Error::Type(_) | Error::TypeError(_) | Error::InvalidArgumentType(_) => ErrorInfo {
neo4j_code: TYPE_ERROR_CODE,
gql_status: GQLSTATUS_TYPE,
stage: Runtime,
retryable: false,
},
Error::Arithmetic(ArithmeticFailure::DivisionByZero | ArithmeticFailure::ModuloByZero) => {
ErrorInfo {
neo4j_code: ARITHMETIC_ERROR_CODE,
gql_status: GQLSTATUS_TYPE,
stage: Runtime,
retryable: false,
}
}
Error::Arithmetic(ArithmeticFailure::RoundingNecessary) => ErrorInfo {
neo4j_code: ARITHMETIC_ERROR_CODE,
gql_status: GQLSTATUS_GENERAL,
stage: Runtime,
retryable: false,
},
Error::UniquenessViolation { .. } => ErrorInfo {
neo4j_code: CONSTRAINT_VALIDATION_FAILED_CODE,
gql_status: GQLSTATUS_GENERAL,
stage: Runtime,
retryable: false,
},
Error::Cancelled => ErrorInfo {
neo4j_code: TRANSACTION_TIMED_OUT_CODE,
gql_status: GQLSTATUS_GENERAL,
stage: Runtime,
retryable: false,
},
Error::Conflict => ErrorInfo {
neo4j_code: OUTDATED_CODE,
gql_status: GQLSTATUS_GENERAL,
stage: Runtime,
retryable: true,
},
Error::Capacity { .. } => ErrorInfo {
neo4j_code: TRANSACTION_OUT_OF_MEMORY_CODE,
gql_status: GQLSTATUS_GENERAL,
stage: Runtime,
retryable: false,
},
Error::ResourceLimit { retryable, .. } => ErrorInfo {
neo4j_code: if *retryable {
MEMORY_POOL_OUT_OF_MEMORY_CODE
} else {
TRANSACTION_OUT_OF_MEMORY_CODE
},
gql_status: GQLSTATUS_GENERAL,
stage: Runtime,
retryable: *retryable,
},
Error::Semantic(payload) | Error::Execution(payload) => {
let nested = nested_error_class(payload);
ErrorInfo {
neo4j_code: SEMANTIC_ERROR_CODE,
gql_status: stringly_gql_status(nested, GQLSTATUS_GENERAL),
stage: Runtime,
retryable: false,
}
}
Error::ConstraintCreationFailed { .. } => ErrorInfo {
neo4j_code: CONSTRAINT_VALIDATION_FAILED_CODE,
gql_status: GQLSTATUS_CONSTRAINT_CREATION_FAILED,
stage: Runtime,
retryable: false,
},
Error::Internal(_) => INTERNAL_ERROR_INFO,
Error::Storage(_) | Error::Io(_) | Error::Locked(_) => ErrorInfo {
gql_status: GQLSTATUS_GENERAL,
..INTERNAL_ERROR_INFO
},
}
}
#[cfg(test)]
mod tests;