libjmap/
error.rs

1// Copyright 2025 Hugo Osvaldo Barrera
2//
3// SPDX-License-Identifier: ISC
4
5use http::StatusCode;
6
7/// JMAP-specific error types
8#[derive(thiserror::Error, Debug)]
9pub enum Error {
10    #[error("JSON deserialization failed: {0}")]
11    JsonDeserialize(#[from] serde_json::Error),
12
13    #[error("HTTP request failed: {0}")]
14    Http(#[from] hyper::Error),
15
16    #[error("HTTP error: {0}")]
17    HttpStatus(StatusCode),
18
19    #[error("Failed to build URL: {0}")]
20    UrlBuild(#[from] http::Error),
21
22    #[error("Request failed: {0}")]
23    Request(#[from] crate::RequestError),
24
25    #[error("Client connection failed: {0}")]
26    ClientConnection(Box<dyn std::error::Error + Send + Sync>),
27
28    #[error("Invalid data: {message}")]
29    InvalidData { message: String },
30
31    #[error("Operation incomplete: {message}")]
32    IncompleteOperation { message: String },
33
34    #[error(
35        "Server returned error: {error_type}{}",
36        description.as_ref().map(|s| format!(" - {s}")).unwrap_or_default()
37    )]
38    ServerError {
39        error_type: String,
40        description: Option<String>,
41    },
42
43    #[error("Invalid response: {reason}")]
44    InvalidResponse { reason: String },
45}
46
47impl From<&str> for Error {
48    fn from(value: &str) -> Self {
49        Error::InvalidResponse {
50            reason: value.to_string(),
51        }
52    }
53}
54
55impl From<String> for Error {
56    fn from(value: String) -> Self {
57        Error::InvalidResponse { reason: value }
58    }
59}
60
61impl From<StatusCode> for Error {
62    fn from(status: StatusCode) -> Self {
63        Error::HttpStatus(status)
64    }
65}
66
67pub type Result<T, E = Error> = std::result::Result<T, E>;