use std::fmt;
pub const CANONICAL: [&str; 25] = [
"INVALID_TOOL_INPUT",
"MUTATION_REQUIRED",
"QUERY_REQUIRED",
"INVALID_VARIABLES",
"MISSING_GRAPHQL_VARIABLES",
"UNDECLARED_GRAPHQL_VARIABLES",
"UNSUPPORTED_GRAPHQL_VARIABLE_TYPE",
"UNSUPPORTED_GRAPHQL_VARIABLE_DEFAULT",
"INVALID_GRAPHQL_VARIABLE_VALUE",
"INVALID_GRAPHQL_QUERY",
"INVALID_GRAPHQL_MUTATION",
"QUERY_NOT_ALLOWED",
"NOT_A_MUTATION",
"RESERVED_MUTATION_ALIAS",
"INVALID_DATE_FILTER",
"QUERY_FORBIDDEN",
"QUERY_TIMEOUT",
"DATABASE_UNAVAILABLE",
"QUERY_EXECUTION_FAILED",
"MUTATION_EXECUTION_FAILED",
"MUTATION_RESULT_UNREADABLE",
"INVALID_QUERY_RESPONSE",
"INVALID_MUTATION_RESPONSE",
"PAGINATION_FAILED",
"QUERY_DATA_FAILED",
];
pub const UNSPECIFIED: &str = "ACTION_FAILED";
const MAX_CODE_BYTES: usize = 64;
const MIN_CODE_BYTES: usize = 2;
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Code {
InvalidToolInput,
MutationRequired,
QueryRequired,
InvalidVariables,
MissingGraphqlVariables,
UndeclaredGraphqlVariables,
UnsupportedGraphqlVariableType,
UnsupportedGraphqlVariableDefault,
InvalidGraphqlVariableValue,
InvalidGraphqlQuery,
InvalidGraphqlMutation,
QueryNotAllowed,
NotAMutation,
ReservedMutationAlias,
InvalidDateFilter,
QueryForbidden,
QueryTimeout,
DatabaseUnavailable,
QueryExecutionFailed,
MutationExecutionFailed,
MutationResultUnreadable,
InvalidQueryResponse,
InvalidMutationResponse,
PaginationFailed,
QueryDataFailed,
Custom(String),
}
impl Code {
pub fn as_str(&self) -> &str {
match self {
Code::InvalidToolInput => "INVALID_TOOL_INPUT",
Code::MutationRequired => "MUTATION_REQUIRED",
Code::QueryRequired => "QUERY_REQUIRED",
Code::InvalidVariables => "INVALID_VARIABLES",
Code::MissingGraphqlVariables => "MISSING_GRAPHQL_VARIABLES",
Code::UndeclaredGraphqlVariables => "UNDECLARED_GRAPHQL_VARIABLES",
Code::UnsupportedGraphqlVariableType => "UNSUPPORTED_GRAPHQL_VARIABLE_TYPE",
Code::UnsupportedGraphqlVariableDefault => "UNSUPPORTED_GRAPHQL_VARIABLE_DEFAULT",
Code::InvalidGraphqlVariableValue => "INVALID_GRAPHQL_VARIABLE_VALUE",
Code::InvalidGraphqlQuery => "INVALID_GRAPHQL_QUERY",
Code::InvalidGraphqlMutation => "INVALID_GRAPHQL_MUTATION",
Code::QueryNotAllowed => "QUERY_NOT_ALLOWED",
Code::NotAMutation => "NOT_A_MUTATION",
Code::ReservedMutationAlias => "RESERVED_MUTATION_ALIAS",
Code::InvalidDateFilter => "INVALID_DATE_FILTER",
Code::QueryForbidden => "QUERY_FORBIDDEN",
Code::QueryTimeout => "QUERY_TIMEOUT",
Code::DatabaseUnavailable => "DATABASE_UNAVAILABLE",
Code::QueryExecutionFailed => "QUERY_EXECUTION_FAILED",
Code::MutationExecutionFailed => "MUTATION_EXECUTION_FAILED",
Code::MutationResultUnreadable => "MUTATION_RESULT_UNREADABLE",
Code::InvalidQueryResponse => "INVALID_QUERY_RESPONSE",
Code::InvalidMutationResponse => "INVALID_MUTATION_RESPONSE",
Code::PaginationFailed => "PAGINATION_FAILED",
Code::QueryDataFailed => "QUERY_DATA_FAILED",
Code::Custom(code) => code.as_str(),
}
}
pub fn wire(&self) -> &str {
match self {
Code::Custom(code) if !wire_safe(code) => UNSPECIFIED,
other => other.as_str(),
}
}
pub fn unspecified() -> Code {
Code::Custom(UNSPECIFIED.to_string())
}
pub fn category(&self) -> Category {
match self {
Code::InvalidToolInput
| Code::MutationRequired
| Code::QueryRequired
| Code::InvalidVariables
| Code::MissingGraphqlVariables
| Code::UndeclaredGraphqlVariables
| Code::UnsupportedGraphqlVariableType
| Code::UnsupportedGraphqlVariableDefault
| Code::InvalidGraphqlVariableValue
| Code::InvalidGraphqlQuery
| Code::InvalidGraphqlMutation
| Code::QueryNotAllowed
| Code::NotAMutation
| Code::ReservedMutationAlias
| Code::InvalidDateFilter => Category::Validation,
Code::QueryForbidden => Category::Authorization,
Code::QueryTimeout => Category::Timeout,
Code::DatabaseUnavailable => Category::Availability,
Code::QueryExecutionFailed
| Code::MutationExecutionFailed
| Code::MutationResultUnreadable => Category::Execution,
Code::InvalidQueryResponse | Code::InvalidMutationResponse => Category::Response,
Code::PaginationFailed => Category::Pagination,
Code::QueryDataFailed => Category::Internal,
Code::Custom(_) => Category::Execution,
}
}
pub fn retryable(&self) -> bool {
matches!(self, Code::QueryTimeout | Code::DatabaseUnavailable)
}
}
impl fmt::Display for Code {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.wire())
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Category {
Validation,
Authorization,
Timeout,
Availability,
Execution,
Response,
Pagination,
Internal,
}
impl Category {
pub fn as_str(&self) -> &str {
match self {
Category::Validation => "validation",
Category::Authorization => "authorization",
Category::Timeout => "timeout",
Category::Availability => "availability",
Category::Execution => "execution",
Category::Response => "response",
Category::Pagination => "pagination",
Category::Internal => "internal",
}
}
}
impl fmt::Display for Category {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
fn wire_safe(code: &str) -> bool {
(MIN_CODE_BYTES..=MAX_CODE_BYTES).contains(&code.len())
&& code
.bytes()
.all(|byte| byte.is_ascii_uppercase() || byte.is_ascii_digit() || byte == b'_')
&& code.as_bytes()[0].is_ascii_uppercase()
}
#[cfg(test)]
mod tests {
use super::*;
const EVERY: [Code; 25] = [
Code::InvalidToolInput,
Code::MutationRequired,
Code::QueryRequired,
Code::InvalidVariables,
Code::MissingGraphqlVariables,
Code::UndeclaredGraphqlVariables,
Code::UnsupportedGraphqlVariableType,
Code::UnsupportedGraphqlVariableDefault,
Code::InvalidGraphqlVariableValue,
Code::InvalidGraphqlQuery,
Code::InvalidGraphqlMutation,
Code::QueryNotAllowed,
Code::NotAMutation,
Code::ReservedMutationAlias,
Code::InvalidDateFilter,
Code::QueryForbidden,
Code::QueryTimeout,
Code::DatabaseUnavailable,
Code::QueryExecutionFailed,
Code::MutationExecutionFailed,
Code::MutationResultUnreadable,
Code::InvalidQueryResponse,
Code::InvalidMutationResponse,
Code::PaginationFailed,
Code::QueryDataFailed,
];
#[test]
fn every_variant_is_canonical() {
for code in EVERY.iter() {
let known = match code {
Code::Custom(_) => false,
_ => CANONICAL.contains(&code.as_str()),
};
assert!(known, "{} is not in CANONICAL", code.as_str());
}
assert_eq!(EVERY.len(), CANONICAL.len());
}
#[test]
fn canonical_order_matches_the_enum() {
let listed: Vec<&str> = EVERY.iter().map(Code::as_str).collect();
assert_eq!(listed, CANONICAL.to_vec());
}
#[test]
fn only_confirmed_timeouts_and_outages_are_retryable() {
let retryable: Vec<&str> = EVERY
.iter()
.filter(|code| code.retryable())
.map(Code::as_str)
.collect();
assert_eq!(retryable, vec!["QUERY_TIMEOUT", "DATABASE_UNAVAILABLE"]);
}
#[test]
fn a_custom_code_the_wire_cannot_carry_is_replaced() {
assert_eq!(Code::Custom("INVOICE_PAID".into()).wire(), "INVOICE_PAID");
assert_eq!(Code::Custom("invoice paid".into()).wire(), UNSPECIFIED);
assert_eq!(Code::Custom(String::new()).wire(), UNSPECIFIED);
assert_eq!(Code::Custom("_LEADING".into()).wire(), UNSPECIFIED);
assert_eq!(Code::Custom("X".repeat(65)).wire(), UNSPECIFIED);
}
}