luno_rs/
error.rs

1#![allow(unreachable_patterns)]
2
3use async_std::future::TimeoutError;
4use serde::Deserialize;
5use std::collections::HashMap;
6use std::fmt;
7use url::ParseError;
8
9#[derive(Debug, Deserialize)]
10pub struct LunoError {
11    pub error: String,
12    pub error_code: String,
13    pub error_action: HashMap<String, String>,
14}
15
16#[derive(Debug)]
17pub enum Error {
18    UrlParseError(String),
19    SurfError(String),
20    TimeoutError(String),
21    ApiError(LunoError),
22    InvalidCurrencyPair(String),
23    InvalidOrderType(String),
24}
25
26impl fmt::Display for Error {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Error::UrlParseError(message) => write!(f, "URL parse error: {}", message),
30            Error::SurfError(message) => write!(f, "Surf Error: {}", message),
31            Error::TimeoutError(message) => write!(f, "Timeout Error: {}", message),
32            Error::ApiError(error) => write!(f, "{}: {}", error.error_code, error.error),
33            Error::InvalidCurrencyPair(str) => {
34                write!(f, "Cannot convert {} to any currency pair", str)
35            }
36            Error::InvalidOrderType(str) => {
37                write!(f, "Cannot convert {} to any order type", str)
38            }
39            _ => write!(f, "Unable to process request at this time"),
40        }
41    }
42}
43
44impl From<ParseError> for Error {
45    fn from(err: ParseError) -> Self {
46        Error::UrlParseError(err.to_string())
47    }
48}
49
50impl From<surf::Error> for Error {
51    fn from(err: surf::Error) -> Self {
52        Error::SurfError(err.to_string())
53    }
54}
55
56impl From<TimeoutError> for Error {
57    fn from(err: TimeoutError) -> Self {
58        Error::TimeoutError(err.to_string())
59    }
60}