1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! Error type definitions.

use serde::Deserialize;
use std::fmt;
use thiserror::Error;

use crate::limits::Limits;

/// A `Result` alias where the `Err` case is `axiom::Error`.
pub type Result<T> = std::result::Result<T, Error>;

/// The error type for the Axiom client.
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
    #[error("Missing token")]
    MissingToken,
    #[error("Missing Org ID for Personal Access Token")]
    MissingOrgId,
    #[error("Invalid token (make sure there are no invalid characters)")]
    InvalidToken,
    #[error("Invalid Org ID (make sure there are no invalid characters)")]
    InvalidOrgId,
    #[error("Failed to setup HTTP client: {0}")]
    HttpClientSetup(reqwest::Error),
    #[error("Failed to deserialize response: {0}")]
    Deserialize(reqwest::Error),
    #[error("Http error: {0}")]
    Http(reqwest::Error),
    #[error(transparent)]
    Axiom(AxiomError),
    #[error("Query ID contains invisible characters (this is a server error)")]
    InvalidQueryId,
    #[error(transparent)]
    InvalidParams(#[from] serde_qs::Error),
    #[error(transparent)]
    Serialize(#[from] serde_json::Error),
    #[error("Failed to encode payload: {0}")]
    Encoding(std::io::Error),
    #[error("Duration is out of range (can't be larger than i64::MAX milliseconds)")]
    DurationOutOfRange,
    #[cfg(feature = "tokio")]
    #[error("Failed to join thread: {0}")]
    JoinError(tokio::task::JoinError),
    #[error("Rate limit exceeded: {0}")]
    RateLimitExceeded(Limits),
    #[error("Invalid URL: {0}")]
    InvalidUrl(url::ParseError),
    #[error("Error in ingest stream: {0}")]
    IngestStreamError(Box<dyn std::error::Error + Send>),
    #[error("Invalid content type: {0}")]
    InvalidContentType(String),
    #[error("Invalid content encoding: {0}")]
    InvalidContentEncoding(String),
}

/// This is the manual implementation. We don't really care if the error is
/// permanent or transient at this stage so we just return Error::Http.
impl From<backoff::Error<reqwest::Error>> for Error {
    fn from(err: backoff::Error<reqwest::Error>) -> Self {
        match err {
            backoff::Error::Permanent(err) => Error::Http(err),
            backoff::Error::Transient {
                err,
                retry_after: _,
            } => Error::Http(err),
        }
    }
}

/// An error returned by the Axiom API.
#[derive(Deserialize, Debug)]
pub struct AxiomError {
    #[serde(skip)]
    pub status: u16,
    pub message: Option<String>,
}

impl AxiomError {
    pub(crate) fn new(status: u16, message: Option<String>) -> Self {
        Self { status, message }
    }
}

impl std::error::Error for AxiomError {}

impl fmt::Display for AxiomError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(msg) = self.message.as_ref() {
            write!(f, "Error {}: {}", self.status, msg)
        } else {
            write!(f, "Error {}", self.status)
        }
    }
}