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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 Copyright 2017 LambdaStack All rights reserved.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

/*
 Portions borrowed from the rusoto project. See README.md
*/

use std::num::ParseIntError;
use std::str::ParseBoolError;
use std::error::Error;
use std::fmt;

use aws::errors::aws::*;
use aws::errors::creds::CredentialsError;
use aws::common::xmlutil::*;
use aws::common::params::*;
use aws::common::common::*;
use aws::s3::writeparse::*;
use aws::s3::object::*;

/// S3Error is a larger container error struct that contains a more general error about the task
/// that was requested + `AWSError` which contains the details from AWS S3.
//#[derive(Debug)]
#[derive(Debug, Default, RustcDecodable, RustcEncodable)]
pub struct S3Error {
    pub message: String,
    pub aws: AWSError
}

//#[derive(Debug, Default)]
#[derive(Debug, Default, RustcDecodable, RustcEncodable)]
pub struct S3ClientError {
    pub version_id: ObjectVersionId,
    pub code: Code,
    pub message: S3ClientMessage,
    pub key: ObjectKey,
}

/// Parse `S3ClientError` from XML
pub struct S3ClientErrorParser;

/// Write `S3ClientError` contents to a `SignedRequest`
pub struct S3ClientErrorWriter;


// Impls below...

impl S3Error {
    /// implements the basic S3Error without a valid `AWSError` (default).
    pub fn new<S>(message: S) -> S3Error where S: Into<String> {
        S3Error { message: message.into(), aws: AWSError::default() }
    }

    /// implements the S3Error with a specific `AWSError` with details about the AWS error.
    pub fn with_aws<S>(message: S, aws: AWSError) -> S3Error where S: Into<String> {
        S3Error { message: message.into(), aws: aws }
    }
}

impl fmt::Display for S3Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.description())
    }
}

impl Error for S3Error {
    fn description(&self) -> &str {
        &self.message
    }
}

impl From<CredentialsError> for S3Error {
    fn from(err: CredentialsError) -> S3Error {
        S3Error { message: err.description().to_owned(), aws: AWSError::default() }
    }
}

impl From<ParseIntError> for S3Error {
    fn from(err: ParseIntError) -> S3Error {
        S3Error { message: err.description().to_owned(), aws: AWSError::default() }
    }
}

impl From<ParseBoolError> for S3Error {
    fn from(err: ParseBoolError) -> S3Error {
        S3Error { message: err.description().to_owned(), aws: AWSError::default() }
    }
}

impl From<XmlParseError> for S3Error {
    fn from(err: XmlParseError) -> S3Error {
        let XmlParseError(message) = err;
        S3Error { message: message.to_owned(), aws: AWSError::default() }
    }
}

impl S3ClientErrorParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<S3ClientError, XmlParseError> {
        try!(start_element(tag_name, stack));
        let mut obj = S3ClientError::default();
        loop {
            let current_name = try!(peek_at_name(stack));
            if current_name == "VersionId" {
                obj.version_id = try!(ObjectVersionIdParser::parse_xml("VersionId", stack));
                continue;
            }
            if current_name == "Code" {
                obj.code = try!(CodeParser::parse_xml("Code", stack));
                continue;
            }
            if current_name == "Message" {
                obj.message = try!(S3ClientMessageParser::parse_xml("Message", stack));
                continue;
            }
            if current_name == "Key" {
                obj.key = try!(ObjectKeyParser::parse_xml("Key", stack));
                continue;
            }
            break;
        }
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}

impl S3ClientErrorWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &S3ClientError) {
        let mut prefix = name.to_string();
        if prefix != "" { prefix.push_str("."); }
        ObjectVersionIdWriter::write_params(params, &(prefix.to_string() + "VersionId"), &obj.version_id);
        CodeWriter::write_params(params, &(prefix.to_string() + "Code"), &obj.code);
        S3ClientMessageWriter::write_params(params, &(prefix.to_string() + "Message"), &obj.message);
        ObjectKeyWriter::write_params(params, &(prefix.to_string() + "Key"), &obj.key);
    }
}