use thiserror::Error;
use crate::proto::{INTERNAL_ERROR, INVALID_PARAMS, INVALID_REQUEST, UNSUPPORTED_PROTOCOL_VERSION};
#[allow(clippy::enum_variant_names)] #[derive(Debug, Error)]
#[non_exhaustive]
pub enum ToolError {
#[error("error-atpmcp-tool-1 Failed to serialize record to DAG-CBOR: {reason}")]
SerializationFailed {
reason: String,
},
#[error("error-atpmcp-tool-2 Lexicon schema validation failed: {reason}")]
ValidationFailed {
reason: String,
},
#[error("error-atpmcp-tool-3 Handle resolution failed: {reason}")]
HandleResolutionFailed {
reason: String,
},
#[error("error-atpmcp-tool-4 Identity resolution failed: {reason}")]
IdentityResolutionFailed {
reason: String,
},
#[error("error-atpmcp-tool-5 Facet parsing failed: {reason}")]
FacetParsingFailed {
reason: String,
},
#[error("error-atpmcp-tool-6 Record retrieval failed: {reason}")]
RecordRetrievalFailed {
reason: String,
},
#[error("error-atpmcp-tool-7 Lexicon retrieval failed: {reason}")]
LexiconRetrievalFailed {
reason: String,
},
#[error("error-atpmcp-tool-8 XRPC request failed: {reason}")]
XrpcRequestFailed {
reason: String,
},
#[error("error-atpmcp-tool-9 XRPC validation failed: {reason}")]
XrpcValidationFailed {
reason: String,
},
#[error("error-atpmcp-tool-10 Blob upload failed: {reason}")]
BlobUploadFailed {
reason: String,
},
#[error("error-atpmcp-tool-11 Blob download failed: {reason}")]
BlobDownloadFailed {
reason: String,
},
#[error("error-atpmcp-tool-12 Blob CID mismatch: requested {requested}, computed {computed}")]
BlobCidMismatch {
requested: String,
computed: String,
},
#[error(
"error-atpmcp-tool-16 Endpoint refused by policy: {endpoint}: {reason}. Set ATPMCP_ALLOW_INSECURE_ENDPOINTS to allow it."
)]
EndpointRefused {
endpoint: String,
reason: String,
},
#[error(
"error-atpmcp-tool-17 Blob too large to inline: {size} bytes exceeds {limit}. The bytes were downloaded; drop 'inline' to take the path of the verified file."
)]
BlobTooLargeToInline {
size: u64,
limit: u64,
},
#[error("error-atpmcp-tool-13 Blob too large: {size} bytes exceeds limit {limit}")]
BlobTooLarge {
size: u64,
limit: u64,
},
#[error(
"error-atpmcp-tool-20 Blob too large to download: at least {size} bytes exceeds limit {limit}"
)]
BlobDownloadTooLarge {
size: u64,
limit: u64,
},
#[error(
"error-atpmcp-tool-22 Blob size mismatch: the blob reference declares {declared} bytes, but {}",
found_size(*.actual, *.at_least)
)]
BlobSizeMismatch {
declared: u64,
actual: u64,
at_least: bool,
},
#[error("error-atpmcp-tool-18 XRPC error response: HTTP {status}: {body}")]
XrpcErrorResponse {
status: u16,
body: String,
},
#[error(
"error-atpmcp-tool-19 Blob fetch error response: {endpoint} answered HTTP {status}: {body}"
)]
BlobErrorResponse {
endpoint: String,
status: u16,
body: String,
},
#[error("error-atpmcp-tool-21 Could not read back the verified blob: {path}: {reason}")]
BlobReadBackFailed {
path: String,
reason: String,
},
#[error(
"error-atpmcp-tool-14 Could not determine content type from bytes; pass content_type explicitly"
)]
ContentTypeUndetermined,
#[error("error-atpmcp-tool-15 Local file access failed: {path}: {reason}")]
LocalFileFailed {
path: String,
reason: String,
},
#[error("error-atpmcp-tool-23 Post generation failed: {reason}")]
PostGenerationFailed {
reason: String,
},
#[error("error-atpmcp-tool-24 Link card failed: {url}: {reason}")]
LinkCardFailed {
url: String,
reason: String,
},
#[error("error-atpmcp-tool-25 Image failed: {reason}")]
ImageFailed {
reason: String,
},
}
fn found_size(actual: u64, at_least: bool) -> String {
if at_least {
format!("the host sent at least {actual}")
} else {
format!("the blob is {actual}")
}
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ResourceError {
#[error("error-atpmcp-resource-1 Malformed resource URI: {uri}: {reason}")]
MalformedUri {
uri: String,
reason: String,
},
#[error("error-atpmcp-resource-2 Unsupported resource scheme: {uri}")]
UnsupportedScheme {
uri: String,
},
#[error("error-atpmcp-resource-3 Resource not found: {uri}: {reason}")]
NotFound {
uri: String,
reason: String,
},
#[error("error-atpmcp-resource-4 Blob cache failure: {reason}")]
CacheFailure {
reason: String,
},
#[error(
"error-atpmcp-resource-5 Resource too large to inline: {size} bytes exceeds {limit}, the ATPMCP_BLOB_MAX_INLINE_BYTES ceiling"
)]
TooLarge {
size: u64,
limit: u64,
},
#[error(
"error-atpmcp-resource-6 Resource too large to fetch: at least {size} bytes exceeds {limit}, the ATPMCP_BLOB_MAX_DOWNLOAD_BYTES ceiling"
)]
TooLargeToFetch {
size: u64,
limit: u64,
},
}
impl ResourceError {
#[must_use]
pub fn code(&self) -> i64 {
match self {
Self::MalformedUri { .. }
| Self::UnsupportedScheme { .. }
| Self::NotFound { .. }
| Self::TooLarge { .. }
| Self::TooLargeToFetch { .. } => INVALID_PARAMS,
Self::CacheFailure { .. } => INTERNAL_ERROR,
}
}
#[must_use]
pub fn uri(&self) -> Option<&str> {
match self {
Self::MalformedUri { uri, .. }
| Self::UnsupportedScheme { uri }
| Self::NotFound { uri, .. } => Some(uri),
Self::CacheFailure { .. } | Self::TooLarge { .. } | Self::TooLargeToFetch { .. } => {
None
}
}
}
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ProtocolError {
#[error("error-atpmcp-protocol-1 Missing required _meta field: {field}")]
MissingMeta {
field: String,
},
#[error("error-atpmcp-protocol-2 Unsupported protocol version: {requested}")]
UnsupportedVersion {
requested: String,
},
#[error("error-atpmcp-protocol-3 Malformed request: {reason}")]
Malformed {
reason: String,
},
#[error("error-atpmcp-protocol-4 Message exceeds {limit} bytes")]
MessageTooLarge {
limit: usize,
},
#[error("error-atpmcp-protocol-5 Server busy: {inflight} requests in flight")]
Busy {
inflight: usize,
},
#[error("error-atpmcp-protocol-6 Internal error: {reason}")]
Internal {
reason: String,
},
}
impl ProtocolError {
#[must_use]
pub fn code(&self) -> i64 {
match self {
Self::MissingMeta { .. } | Self::Malformed { .. } => INVALID_PARAMS,
Self::UnsupportedVersion { .. } => UNSUPPORTED_PROTOCOL_VERSION,
Self::MessageTooLarge { .. } => INVALID_REQUEST,
Self::Busy { .. } | Self::Internal { .. } => INTERNAL_ERROR,
}
}
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ConfigError {
#[error(
"error-atpmcp-config-1 {name}={value:?} is not a whole number. Write it as digits alone, like its default of {default}"
)]
NotANumber {
name: &'static str,
value: String,
default: u64,
},
#[error("error-atpmcp-config-2 {name}={value} is below its minimum of {minimum}")]
BelowMinimum {
name: &'static str,
value: u64,
minimum: u64,
},
#[error(
"error-atpmcp-config-3 {name}={value:?} is neither on nor off. Use 1, true, yes or on to turn it on, or 0, false, no, off or an empty value to leave it off"
)]
NotAWaiver {
name: &'static str,
value: String,
},
#[error(
"error-atpmcp-config-4 {name}={value:?} is not a usable PLC directory: {reason}. Write a hostname, like plc.directory, or an http:// or https:// URL, like http://localhost:2582"
)]
NotAPlcDirectory {
name: &'static str,
value: String,
reason: String,
},
#[error(
"error-atpmcp-config-5 {name}={value:?} is not a list of nameserver addresses: {entry:?} is not an IP address. Separate addresses with '{separator}', like 1.1.1.1{separator}8.8.8.8"
)]
NotANameserverList {
name: &'static str,
value: String,
entry: String,
separator: char,
},
}