Skip to main content

indexnow_api/error/
mod.rs

1use std::fmt::{Debug, Display, Formatter};
2
3/// Error returned by [`IndexNowApi::send_urls`](crate::IndexNowApi::send_urls).
4///
5/// Implements [`std::error::Error`], so it works with `?` in functions that
6/// return `Result<(), Box<dyn std::error::Error>>`.
7#[derive(Clone, PartialEq, Eq)]
8pub enum IndexNowError {
9    /// The request could not be sent (DNS, TLS, timeout, ...) or the response
10    /// body could not be read. Holds the transport error message.
11    Connection(String),
12    /// The search engine answered with a status other than 200 or 202.
13    Status {
14        /// HTTP status code, e.g. 400, 403, 422, 429.
15        code: u16,
16        /// Response body as returned by the endpoint.
17        body: String,
18    },
19}
20
21impl Display for IndexNowError {
22    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23        match self {
24            IndexNowError::Connection(e) => f.write_str(e),
25            IndexNowError::Status { code, body } => {
26                write!(f, "IndexNow API returned status {}: {}", code, body)
27            }
28        }
29    }
30}
31
32impl Debug for IndexNowError {
33    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
34        Display::fmt(self, f)
35    }
36}
37
38impl std::error::Error for IndexNowError {}
39
40/// Former name of [`IndexNowError`], kept so 1.0 code keeps compiling.
41#[deprecated(since = "1.1.0", note = "renamed to `IndexNowError`")]
42pub type GoogleApiError = IndexNowError;