Skip to main content

exo_api/
error.rs

1// Copyright 2026 Exochain Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at:
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// SPDX-License-Identifier: Apache-2.0
16
17//! API-specific errors.
18use thiserror::Error;
19
20/// Errors returned by the exo-api layer.
21#[derive(Debug, Error)]
22pub enum ApiError {
23    #[error("peer not found: {0}")]
24    PeerNotFound(String),
25    #[error("message verification failed: {reason}")]
26    VerificationFailed { reason: String },
27    #[error("rate limited: {peer_id}")]
28    RateLimited { peer_id: String },
29    #[error("replayed message from {peer_id} with nonce {nonce}")]
30    ReplayDetected { peer_id: String, nonce: u64 },
31    #[error("invalid schema: {reason}")]
32    InvalidSchema { reason: String },
33    #[error("serialization error: {0}")]
34    SerializationError(String),
35}
36/// Convenience alias for results with [`ApiError`].
37pub type Result<T> = std::result::Result<T, ApiError>;
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42    #[test]
43    fn all_variants_display() {
44        let es: Vec<ApiError> = vec![
45            ApiError::PeerNotFound("x".into()),
46            ApiError::VerificationFailed { reason: "x".into() },
47            ApiError::RateLimited {
48                peer_id: "x".into(),
49            },
50            ApiError::ReplayDetected {
51                peer_id: "x".into(),
52                nonce: 1,
53            },
54            ApiError::InvalidSchema { reason: "x".into() },
55            ApiError::SerializationError("x".into()),
56        ];
57        for e in &es {
58            assert!(!e.to_string().is_empty());
59        }
60    }
61    #[test]
62    fn result_alias() {
63        let ok: Result<u32> = Ok(1);
64        assert!(ok.is_ok());
65    }
66}