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
use std::error; use std::fmt; mod consts { pub const UNIMPLEMENTED: &str = "Unimplemented."; pub const CONVERT_FROM_REQWEST_RESPONSE_ERROR: &str = "Convert from reqwest::Response was failed."; pub const IMAGE_DECODE_ERROR: &str = "Image decoding was failed."; } #[derive(Debug)] pub enum Error { Unimplemented , ReqwestError( reqwest::Error ) , HTTPStatusError( reqwest::StatusCode ) , ConvertFromReqwestResponseError , ImageError( image::ImageError ) , ImageDecodeError , IOError( std::io::Error ) } impl fmt::Display for Error { fn fmt( &self, f: &mut fmt::Formatter ) -> fmt::Result { match *self { Error::Unimplemented => write!( f, "Error: {}", consts::UNIMPLEMENTED ) , Error::ReqwestError( ref e ) => write!( f, "Error/ReqwestError: {}", e ) , Error::HTTPStatusError( ref e ) => write!( f, "Error/HTTPStatusError: {}", e ) , Error::ConvertFromReqwestResponseError => write!( f, "Error: {}", consts::CONVERT_FROM_REQWEST_RESPONSE_ERROR ) , Error::ImageError( ref e ) => write!( f, "Error/ImageError: {}", e ) , Error::ImageDecodeError => write!( f, "Error: {}", consts::IMAGE_DECODE_ERROR ) , Error::IOError( ref e ) => write!( f, "Error/IOError: {}", e ) } } } impl error::Error for Error { fn description( &self ) -> &str { match *self { Error::Unimplemented => consts::UNIMPLEMENTED , Error::ReqwestError( ref e ) => e.description() , Error::HTTPStatusError( ref _e ) => "HTTP Status Code Error" , Error::ConvertFromReqwestResponseError => consts::CONVERT_FROM_REQWEST_RESPONSE_ERROR , Error::ImageError( ref e) => e.description() , Error::ImageDecodeError => consts::IMAGE_DECODE_ERROR , Error::IOError( ref e) => e.description() } } fn cause( &self ) -> Option< &error::Error > { match *self { Error::Unimplemented => None , Error::ReqwestError( ref e ) => Some( e ) , Error::HTTPStatusError( ref _e ) => None , Error::ConvertFromReqwestResponseError => None , Error::ImageError( ref e ) => Some( e ) , Error::ImageDecodeError => None , Error::IOError( ref e ) => Some( e ) } } }