use super::ErrorKind;
pub const SESSION_NOT_LOADED_CODE: i64 = -32024;
pub const SESSION_STREAM_MISMATCH_CODE: i64 = -32025;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ErrorCode {
ParseError,
InvalidRequest,
MethodNotFound,
InvalidParams,
Internal,
Overloaded,
InputTooLarge,
CapabilityRequired,
NotFound,
Interrupted,
Cancelled,
SessionNotFound,
SessionInUse,
SessionAmbiguous,
ForkBoundaryInvalid,
SessionNotLoaded,
SessionStreamMismatch,
WorkspaceNotTrusted,
CommandRejected,
Backpressured,
SkillNotFound,
ComputerUseUnavailable,
MarketplaceNotFound,
PluginNotFound,
MarketplaceAlreadyExists,
PluginMutationsDisabled,
UploadNotFound,
ViewTruncated,
OutputUnavailable,
Boundary,
ApprovalNotFound,
ApprovalAlreadyResolved,
ApprovalChoiceInvalid,
ApprovalRequirementStale,
ApprovalReviewerUnavailable,
UserInputNotFound,
UserInputAlreadySettled,
UserInputAnswerInvalid,
MissingAnchor,
PrunedPrefix,
WrongStream,
UnknownStream,
UnknownProjection,
AdmissionNotFound,
AdmissionAlreadyResolved,
AdmissionChoiceInvalid,
}
#[derive(Clone, Copy)]
struct ErrorCodeDefinition {
code: i64,
kind: ErrorKind,
override_kinds: &'static [ErrorKind],
retryable: Option<bool>,
experimental: bool,
}
impl ErrorCodeDefinition {
const fn new(
code: i64,
kind: ErrorKind,
override_kinds: &'static [ErrorKind],
retryable: Option<bool>,
) -> Self {
Self {
code,
kind,
override_kinds,
retryable,
experimental: false,
}
}
const fn experimental(code: i64, kind: ErrorKind) -> Self {
Self {
code,
kind,
override_kinds: &[],
retryable: Some(false),
experimental: true,
}
}
}
const fn fatal(code: i64, kind: ErrorKind) -> ErrorCodeDefinition {
ErrorCodeDefinition::new(code, kind, &[], Some(false))
}
const fn drains(code: i64, kind: ErrorKind) -> ErrorCodeDefinition {
ErrorCodeDefinition::new(code, kind, &[], Some(true))
}
const fn preempted(code: i64, kind: ErrorKind) -> ErrorCodeDefinition {
ErrorCodeDefinition::new(code, kind, &[], None)
}
impl ErrorCode {
pub const ALL: [Self; 46] = [
Self::ParseError,
Self::InvalidRequest,
Self::MethodNotFound,
Self::InvalidParams,
Self::Internal,
Self::Overloaded,
Self::InputTooLarge,
Self::CapabilityRequired,
Self::NotFound,
Self::Interrupted,
Self::Cancelled,
Self::SessionNotFound,
Self::SessionInUse,
Self::SessionAmbiguous,
Self::ForkBoundaryInvalid,
Self::SessionNotLoaded,
Self::SessionStreamMismatch,
Self::WorkspaceNotTrusted,
Self::CommandRejected,
Self::Backpressured,
Self::SkillNotFound,
Self::ComputerUseUnavailable,
Self::MarketplaceNotFound,
Self::PluginNotFound,
Self::MarketplaceAlreadyExists,
Self::PluginMutationsDisabled,
Self::UploadNotFound,
Self::ViewTruncated,
Self::OutputUnavailable,
Self::Boundary,
Self::ApprovalNotFound,
Self::ApprovalAlreadyResolved,
Self::ApprovalChoiceInvalid,
Self::ApprovalRequirementStale,
Self::ApprovalReviewerUnavailable,
Self::UserInputNotFound,
Self::UserInputAlreadySettled,
Self::UserInputAnswerInvalid,
Self::MissingAnchor,
Self::PrunedPrefix,
Self::WrongStream,
Self::UnknownStream,
Self::UnknownProjection,
Self::AdmissionNotFound,
Self::AdmissionAlreadyResolved,
Self::AdmissionChoiceInvalid,
];
const fn definition(self) -> ErrorCodeDefinition {
match self {
Self::ParseError => fatal(-32700, ErrorKind::ParseError),
Self::InvalidRequest => ErrorCodeDefinition::new(
-32600,
ErrorKind::InvalidRequest,
&[ErrorKind::NotInitialized, ErrorKind::AlreadyInitialized],
Some(false),
),
Self::MethodNotFound => ErrorCodeDefinition::new(
-32601,
ErrorKind::MethodNotFound,
&[ErrorKind::ExperimentalRequired],
Some(false),
),
Self::InvalidParams => ErrorCodeDefinition::new(
-32602,
ErrorKind::InvalidParams,
&[ErrorKind::ExperimentalRequired],
Some(false),
),
Self::Internal => ErrorCodeDefinition::new(
-32603,
ErrorKind::Internal,
&[
ErrorKind::PageEventTooLarge,
ErrorKind::OutputResultTooLarge,
],
Some(false),
),
Self::Overloaded => drains(-32001, ErrorKind::Overloaded),
Self::InputTooLarge => fatal(-32002, ErrorKind::InputTooLarge),
Self::CapabilityRequired => fatal(-32010, ErrorKind::CapabilityRequired),
Self::NotFound => fatal(-32011, ErrorKind::NotFound),
Self::Interrupted => preempted(-32013, ErrorKind::Interrupted),
Self::Cancelled => preempted(-32014, ErrorKind::Cancelled),
Self::SessionNotFound => fatal(-32020, ErrorKind::SessionNotFound),
Self::SessionInUse => fatal(-32021, ErrorKind::SessionInUse),
Self::SessionAmbiguous => fatal(-32022, ErrorKind::SessionAmbiguous),
Self::ForkBoundaryInvalid => fatal(-32023, ErrorKind::ForkBoundaryInvalid),
Self::SessionNotLoaded => fatal(SESSION_NOT_LOADED_CODE, ErrorKind::SessionNotLoaded),
Self::SessionStreamMismatch => fatal(
SESSION_STREAM_MISMATCH_CODE,
ErrorKind::SessionStreamMismatch,
),
Self::WorkspaceNotTrusted => {
ErrorCodeDefinition::experimental(-32029, ErrorKind::WorkspaceNotTrusted)
}
Self::CommandRejected => fatal(-32030, ErrorKind::CommandRejected),
Self::Backpressured => drains(-32031, ErrorKind::Backpressured),
Self::SkillNotFound => fatal(-32032, ErrorKind::SkillNotFound),
Self::ComputerUseUnavailable => fatal(-32033, ErrorKind::ComputerUseUnavailable),
Self::MarketplaceNotFound => fatal(-32034, ErrorKind::MarketplaceNotFound),
Self::PluginNotFound => fatal(-32035, ErrorKind::PluginNotFound),
Self::MarketplaceAlreadyExists => fatal(-32036, ErrorKind::MarketplaceAlreadyExists),
Self::PluginMutationsDisabled => fatal(-32037, ErrorKind::PluginMutationsDisabled),
Self::UploadNotFound => fatal(-32038, ErrorKind::UploadNotFound),
Self::ViewTruncated => fatal(-32040, ErrorKind::ViewTruncated),
Self::OutputUnavailable => fatal(-32041, ErrorKind::OutputUnavailable),
Self::Boundary => ErrorCodeDefinition::new(
-32042,
ErrorKind::BoundaryPruned,
&[ErrorKind::BoundaryUnusable, ErrorKind::NoBoundary],
Some(false),
),
Self::ApprovalNotFound => fatal(-32050, ErrorKind::ApprovalNotFound),
Self::ApprovalAlreadyResolved => fatal(-32051, ErrorKind::ApprovalAlreadyResolved),
Self::ApprovalChoiceInvalid => fatal(-32052, ErrorKind::ApprovalChoiceInvalid),
Self::ApprovalRequirementStale => fatal(-32053, ErrorKind::ApprovalRequirementStale),
Self::ApprovalReviewerUnavailable => {
fatal(-32054, ErrorKind::ApprovalReviewerUnavailable)
}
Self::UserInputNotFound => fatal(-32055, ErrorKind::UserInputNotFound),
Self::UserInputAlreadySettled => fatal(-32056, ErrorKind::UserInputAlreadySettled),
Self::UserInputAnswerInvalid => fatal(-32057, ErrorKind::UserInputAnswerInvalid),
Self::MissingAnchor => {
ErrorCodeDefinition::experimental(-32060, ErrorKind::MissingAnchor)
}
Self::PrunedPrefix => {
ErrorCodeDefinition::experimental(-32061, ErrorKind::PrunedPrefix)
}
Self::WrongStream => ErrorCodeDefinition::experimental(-32062, ErrorKind::WrongStream),
Self::UnknownStream => {
ErrorCodeDefinition::experimental(-32063, ErrorKind::UnknownStream)
}
Self::UnknownProjection => {
ErrorCodeDefinition::experimental(-32064, ErrorKind::UnknownProjection)
}
Self::AdmissionNotFound => {
ErrorCodeDefinition::experimental(-32070, ErrorKind::AdmissionNotFound)
}
Self::AdmissionAlreadyResolved => {
ErrorCodeDefinition::experimental(-32071, ErrorKind::AdmissionAlreadyResolved)
}
Self::AdmissionChoiceInvalid => {
ErrorCodeDefinition::experimental(-32072, ErrorKind::AdmissionChoiceInvalid)
}
}
}
#[must_use]
pub const fn code(self) -> i64 {
self.definition().code
}
#[must_use]
pub const fn kind(self) -> ErrorKind {
self.definition().kind
}
#[must_use]
pub const fn override_kinds(self) -> &'static [ErrorKind] {
self.definition().override_kinds
}
#[must_use]
pub const fn retryable(self) -> Option<bool> {
self.definition().retryable
}
#[must_use]
pub const fn is_experimental(self) -> bool {
self.definition().experimental
}
}