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
use std::fmt;

use serde::Deserialize;
use thiserror::Error;

use crate::connection::Permission;

#[derive(Error, Debug)]
pub enum ClientError {
    #[error("Insufficient permission ({permission:?}) to operate: {operation}")]
    InsufficientPermission {
        permission: Permission,
        operation: String,
    },
    #[error("Server is not ArangoDB: {0}")]
    InvalidServer(String),
    #[error("Error from server: {0}")]
    Arango(#[from] ArangoError),
    #[error("error from serde")]
    Serde(#[from] serde_json::error::Error),
    #[error("HTTP client error: {0}")]
    HttpClient(String),
}

#[derive(Deserialize, Debug, Error)]
pub struct ArangoError {
    pub(crate) code: u16,
    #[serde(rename = "errorNum")]
    pub(crate) error_num: u16,
    #[serde(rename = "errorMessage")]
    pub(crate) message: String,
}

impl fmt::Display for ArangoError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}({})", self.message, self.error_num)
    }
}

impl ArangoError {
    /// Get the HTTP status code of an error response.
    pub fn code(&self) -> u16 {
        self.code
    }

    pub fn error_num(&self) -> u16 {
        self.error_num
    }

    pub fn message(&self) -> &str {
        &self.message
    }
}