1use reqwest::StatusCode;
2use thiserror::Error;
3
4use crate::QuotaInfo;
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, Error)]
9pub enum Error {
10 #[error("failed to build HTTP client: {0}")]
11 ClientBuild(#[source] reqwest::Error),
12
13 #[error("invalid base URL: {0}")]
14 InvalidBaseUrl(String),
15
16 #[error("request failed: {0}")]
17 Request(#[from] reqwest::Error),
18
19 #[error("Toggl API returned {status}: {body}")]
20 Api {
21 status: StatusCode,
22 body: String,
23 quota: Option<QuotaInfo>,
24 },
25
26 #[error("authentication failed; check your Toggl API token")]
27 Authentication,
28
29 #[error("not found: {0}")]
30 NotFound(String),
31
32 #[error("invalid input: {0}")]
33 InvalidInput(String),
34}
35
36impl Error {
37 pub(crate) fn api(status: StatusCode, body: String, quota: Option<QuotaInfo>) -> Self {
38 match status {
39 StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => Self::Authentication,
40 StatusCode::NOT_FOUND => Self::NotFound(body),
41 _ => Self::Api {
42 status,
43 body,
44 quota,
45 },
46 }
47 }
48}
49
50#[derive(Debug, Clone, Error)]
51#[error("Toggl API returned {status}: {body}")]
52pub struct ApiError {
53 pub status: StatusCode,
54 pub body: String,
55 pub quota: Option<QuotaInfo>,
56}