use crate::exit::ExitCode;
#[derive(Debug, thiserror::Error)]
pub enum ApiError {
#[error("transport error talking to Tracker")]
Transport(#[from] reqwest::Error),
#[error("not authenticated: the token was rejected (401)")]
Unauthorized,
#[error("forbidden (403): the account lacks rights, or the organisation header is wrong")]
Forbidden,
#[error(
"the Wiki refused this token: it needs the wiki:read permission — sign in again \
with `ytcli auth login` — or this account cannot see that page"
)]
WikiForbidden,
#[error(
"the Wiki is not set up in this organisation yet — open https://wiki.yandex.ru once, \
signed in to it, and try again"
)]
WikiNotEnabled,
#[error(
"the Wiki refused this write: the token needs the wiki:write permission — sign in \
again with `ytcli auth login` — or this account may not edit that page"
)]
WikiWriteForbidden,
#[error("{0} not found")]
NotFound(String),
#[error("rate limited by Tracker (429)")]
RateLimited,
#[error("Tracker rejected the request ({status}): {message}")]
Rejected {
status: reqwest::StatusCode,
message: String,
},
#[error("could not decode the Tracker response")]
Decode(#[source] serde_json::Error),
}
impl ApiError {
#[must_use]
pub fn exit_code(&self) -> ExitCode {
match self {
Self::Unauthorized | Self::WikiForbidden | Self::WikiWriteForbidden => ExitCode::Auth,
Self::NotFound(_) => ExitCode::NotFound,
Self::Forbidden | Self::WikiNotEnabled | Self::RateLimited | Self::Rejected { .. } => {
ExitCode::ApiRejected
}
Self::Transport(_) | Self::Decode(_) => ExitCode::Failure,
}
}
}