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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
extern crate reqwest;

use self::reqwest::StatusCode;

use std::error::Error;
use std::fmt;
use std::io;
use std::result::Result;
use std::string;

/// Result type that represents the result of calls to the Media Manager API via [Client](struct.Client.html)
pub type MMCResult<T> = Result<T, MMCError>;

/// Error type that represents failures from [Client](struct.Client.html)
#[derive(Debug)]
pub enum MMCError {
    /// Generated by when a request tries to access an resource it is not authorized for
    NotAuthorized,

    /// Generated by a failure to find a requested resource
    ResourceNotFound,

    /// Generated by unknown server failures from the remote server
    APIFailure(StatusCode),

    /// Generated by a bad request response from the server with a reason attached
    BadRequest(String),

    /// Generated by a failure to parse an API response
    Convert(string::FromUtf8Error),

    /// Generated by the networking client
    Network(reqwest::Error),

    /// Generated by handling of network responses
    Io(io::Error),

    /// Generated when an endpoint string can not be parsed
    UnknownEndpoint(String),

    /// Generated when trying to move an object to an unsupported parent
    UnsupportedMoveParent(String),
}

impl fmt::Display for MMCError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            MMCError::NotAuthorized => write!(
                f,
                "Not authorized to access this resource. Ensure that valid key/secret pair \
                 with authorization to the resource have been supplied."
            ),
            MMCError::ResourceNotFound => write!(f, "Specified resource could not be found."),
            MMCError::APIFailure(ref status_code) => write!(
                f,
                "Unknown failure of the API endpoint. API returned a {} status.",
                status_code
            ),
            MMCError::BadRequest(ref err_msg) => {
                write!(f, "API did not understand request. {}", err_msg)
            }
            MMCError::Convert(ref err) => err.fmt(f),
            MMCError::Network(ref err) => err.fmt(f),
            MMCError::Io(ref err) => err.fmt(f),
            MMCError::UnknownEndpoint(ref endpoint) => write!(
                f,
                "Unable to parse the endpoint {} into an Endpoint type",
                endpoint
            ),
            MMCError::UnsupportedMoveParent(ref endpoint) => write!(
                f,
                "Unable to create an object move request to a parent of the {} type",
                endpoint
            ),
        }
    }
}

impl Error for MMCError {
    fn description(&self) -> &str {
        match *self {
            MMCError::NotAuthorized => "Not authorized response from the API",
            MMCError::ResourceNotFound => "Specified resource could not be found",
            MMCError::APIFailure(_) => "Unknown failure occured",
            MMCError::BadRequest(ref err_msg) => err_msg.as_str(),
            MMCError::Convert(ref err) => err.description(),
            MMCError::Network(ref err) => err.description(),
            MMCError::Io(ref err) => err.description(),
            MMCError::UnknownEndpoint(_) => "Can not parse endpoint into type",
            MMCError::UnsupportedMoveParent(_) => "Unable to create move request",
        }
    }

    fn cause(&self) -> Option<&Error> {
        match *self {
            MMCError::Convert(ref err) => Some(err),
            MMCError::Network(ref err) => Some(err),
            MMCError::Io(ref err) => Some(err),
            _ => None,
        }
    }
}

impl From<string::FromUtf8Error> for MMCError {
    fn from(err: string::FromUtf8Error) -> MMCError {
        MMCError::Convert(err)
    }
}

impl From<reqwest::Error> for MMCError {
    fn from(err: reqwest::Error) -> MMCError {
        MMCError::Network(err)
    }
}

impl From<io::Error> for MMCError {
    fn from(err: io::Error) -> MMCError {
        MMCError::Io(err)
    }
}