Skip to main content

acp_runtime/
errors.rs

1// Copyright 2026 ACP Project
2// Licensed under the Apache License, Version 2.0
3// See LICENSE file for details.
4
5use thiserror::Error;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
8#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
9pub enum FailReason {
10    UnsupportedVersion,
11    UnsupportedCryptoSuite,
12    UnsupportedMessageClass,
13    InvalidSignature,
14    ExpiredMessage,
15    PolicyRejected,
16    PayloadTooLarge,
17    UnsupportedProfile,
18}
19
20impl FailReason {
21    pub fn as_str(self) -> &'static str {
22        match self {
23            Self::UnsupportedVersion => "UNSUPPORTED_VERSION",
24            Self::UnsupportedCryptoSuite => "UNSUPPORTED_CRYPTO_SUITE",
25            Self::UnsupportedMessageClass => "UNSUPPORTED_MESSAGE_CLASS",
26            Self::InvalidSignature => "INVALID_SIGNATURE",
27            Self::ExpiredMessage => "EXPIRED_MESSAGE",
28            Self::PolicyRejected => "POLICY_REJECTED",
29            Self::PayloadTooLarge => "PAYLOAD_TOO_LARGE",
30            Self::UnsupportedProfile => "UNSUPPORTED_PROFILE",
31        }
32    }
33}
34
35#[derive(Debug, Error)]
36pub enum AcpError {
37    #[error("invalid argument: {0}")]
38    InvalidArgument(String),
39    #[error("validation failed: {0}")]
40    Validation(String),
41    #[error("transport failure: {0}")]
42    Transport(String),
43    #[error("crypto failure: {0}")]
44    Crypto(String),
45    #[error("discovery failure: {0}")]
46    Discovery(String),
47    #[error("key provider failure: {0}")]
48    KeyProvider(String),
49    #[error("policy rejected ({reason:?}): {detail}")]
50    Processing { reason: FailReason, detail: String },
51    #[error("io failure: {0}")]
52    Io(#[from] std::io::Error),
53    #[error("http failure: {0}")]
54    Http(#[from] reqwest::Error),
55    #[error("url parse failure: {0}")]
56    Url(#[from] url::ParseError),
57    #[error("json parse failure: {0}")]
58    Json(#[from] serde_json::Error),
59}
60
61pub type AcpResult<T> = Result<T, AcpError>;