rinfluxdb_lineprotocol/
client.rs

1// Copyright Claudio Mattera 2021.
2// Distributed under the MIT License or Apache 2.0 License at your option.
3// See accompanying files License-MIT.txt and License-Apache-2.0, or online at
4// https://opensource.org/licenses/MIT
5// https://opensource.org/licenses/Apache-2.0
6
7use serde::Deserialize;
8
9use serde_json::from_str;
10
11use thiserror::Error;
12
13pub mod r#async;
14pub mod blocking;
15
16/// An error occurred during interfacing with an InfluxDB server
17#[derive(Error, Debug)]
18pub enum ClientError {
19    /// Error occurred within the Reqwest library
20    #[error("Reqwest error")]
21    ReqwestError(#[from] reqwest::Error),
22
23    /// Error occurred while parsing a URL
24    #[error("URL parse error")]
25    UrlError(#[from] url::ParseError),
26
27    /// Specified a field with conflicting type
28    #[error("Field type conflict")]
29    FieldTypeConflict,
30
31    /// Database was not found
32    #[error("Database not found")]
33    DatabaseNotFound,
34
35    /// Unknown error
36    #[error("Unknown error")]
37    Unknown,
38}
39
40fn parse_error(text: &str) -> ClientError {
41    let response: Result<Response, _> = from_str(text);
42    match response {
43        Ok(response) => {
44            if response.error.starts_with("field type conflict") {
45                ClientError::FieldTypeConflict
46            } else if response.error.starts_with("database not found") {
47                ClientError::DatabaseNotFound
48            } else {
49                ClientError::Unknown
50            }
51        }
52        Err(_) => ClientError::Unknown,
53    }
54
55}
56
57#[derive(Debug, Deserialize)]
58struct Response {
59    error: String,
60}