1use std::fmt;
7use std::ops::RangeInclusive;
8use std::path::PathBuf;
9
10use aws_sdk_s3::operation::abort_multipart_upload::AbortMultipartUploadError;
11use aws_sdk_s3::operation::complete_multipart_upload::CompleteMultipartUploadError;
12use aws_sdk_s3::operation::create_multipart_upload::CreateMultipartUploadError;
13use aws_sdk_s3::operation::get_object::GetObjectError;
14use aws_sdk_s3::operation::head_object::HeadObjectError;
15use aws_sdk_s3::operation::list_objects_v2::ListObjectsV2Error;
16use aws_sdk_s3::operation::put_object::PutObjectError;
17use aws_sdk_s3::operation::upload_part::UploadPartError;
18use aws_sdk_s3::operation::RequestIdExt;
19use aws_smithy_observability::unit_of_work::checksums::{
20 ChecksumOutcome, ResponseChecksumAlgorithm,
21};
22use aws_smithy_runtime_api::client::orchestrator::HttpResponse;
23use aws_smithy_runtime_api::client::result::{ConnectorError, SdkError};
24use aws_smithy_types::error::metadata::ProvideErrorMetadata;
25use aws_smithy_types::retry::ErrorKind as RetryErrorKind;
26use aws_types::request_id::RequestId;
27
28use crate::model::ChecksumAlgorithm;
29use crate::types::{FailedDownload, FailedUpload};
30
31pub use crate::model::UnknownVariantError;
32
33pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
35
36#[derive(Debug)]
41pub struct Error {
42 kind: ErrorKind,
43 source: BoxError,
44 extra: Option<Box<ErrorExtra>>,
47}
48
49#[derive(Debug, Default)]
51struct ErrorExtra {
52 service: Option<ServiceMetadata>,
54 http_status: Option<u16>,
56 location: Option<ChunkRef>,
58 local: Option<LocalContext>,
60 upload_id: Option<String>,
62 failed_uploads: Option<Vec<FailedUpload>>,
64 failed_downloads: Option<Vec<FailedDownload>>,
66 transient_transport: bool,
74}
75
76#[derive(Clone, Debug, Eq, PartialEq)]
83#[non_exhaustive]
84pub enum ErrorKind {
85 InputInvalid,
93
94 IOError,
103
104 RuntimeError,
107
108 ObjectNotDiscoverable,
111
112 ServiceError,
134
135 IntegrityError(IntegrityError),
138
139 ChildOperationFailed,
148
149 OperationCancelled,
152}
153
154#[derive(Clone, Debug, Eq, PartialEq)]
160#[non_exhaustive]
161pub struct IntegrityError {
162 algorithm: Option<ChecksumAlgorithm>,
163 expected: Option<String>,
164 computed: Option<String>,
165}
166
167impl IntegrityError {
168 pub fn algorithm(&self) -> Option<&ChecksumAlgorithm> {
170 self.algorithm.as_ref()
171 }
172
173 pub fn expected(&self) -> Option<&str> {
175 self.expected.as_deref()
176 }
177
178 pub fn computed(&self) -> Option<&str> {
180 self.computed.as_deref()
181 }
182}
183
184#[derive(Clone, Debug, Eq, PartialEq)]
186#[non_exhaustive]
187pub struct ChunkRef {
188 seq: u64,
189 range: Option<RangeInclusive<u64>>,
190}
191
192impl ChunkRef {
193 pub(crate) fn new(seq: u64, range: Option<RangeInclusive<u64>>) -> Self {
194 Self { seq, range }
195 }
196
197 pub fn seq(&self) -> u64 {
199 self.seq
200 }
201
202 pub fn byte_range(&self) -> Option<&RangeInclusive<u64>> {
204 self.range.as_ref()
205 }
206}
207
208#[derive(Debug)]
214struct LocalContext {
215 action: &'static str,
216 path: PathBuf,
217 key: Option<String>,
219}
220
221#[derive(Debug)]
224struct ServiceMetadata {
225 operation: &'static str,
226 code: Option<String>,
227 message: Option<String>,
228 request_id: Option<String>,
229 extended_request_id: Option<String>,
230}
231
232impl Error {
233 pub fn new<E>(kind: ErrorKind, err: E) -> Error
235 where
236 E: Into<BoxError>,
237 {
238 Error {
239 kind,
240 source: err.into(),
241 extra: None,
242 }
243 }
244
245 pub fn kind(&self) -> &ErrorKind {
247 &self.kind
248 }
249
250 #[cfg(test)]
254 pub(crate) fn test_transient_transport() -> Error {
255 Error {
256 kind: ErrorKind::ServiceError,
257 source: "injected transient transport".into(),
258 extra: Some(Box::new(ErrorExtra {
259 transient_transport: true,
260 ..Default::default()
261 })),
262 }
263 }
264
265 #[cfg(test)]
270 pub(crate) fn test_service_error(code: &str) -> Error {
271 Error {
272 kind: ErrorKind::ServiceError,
273 source: "injected service error".into(),
274 extra: Some(Box::new(ErrorExtra {
275 service: Some(ServiceMetadata {
276 operation: "TestOperation",
277 code: Some(code.to_owned()),
278 message: None,
279 request_id: None,
280 extended_request_id: None,
281 }),
282 ..Default::default()
283 })),
284 }
285 }
286
287 fn service(&self) -> Option<&ServiceMetadata> {
288 self.extra.as_ref().and_then(|e| e.service.as_ref())
289 }
290
291 fn extra_mut(&mut self) -> &mut ErrorExtra {
293 self.extra
294 .get_or_insert_with(|| Box::new(ErrorExtra::default()))
295 }
296
297 pub fn operation_name(&self) -> Option<&str> {
300 self.service().map(|m| m.operation)
301 }
302
303 pub fn code(&self) -> Option<&str> {
305 self.service().and_then(|m| m.code.as_deref())
306 }
307
308 pub(crate) fn http_status(&self) -> Option<u16> {
310 self.extra.as_ref().and_then(|e| e.http_status)
311 }
312
313 pub fn message(&self) -> Option<&str> {
315 self.service().and_then(|m| m.message.as_deref())
316 }
317
318 pub fn request_id(&self) -> Option<&str> {
320 self.service().and_then(|m| m.request_id.as_deref())
321 }
322
323 pub fn extended_request_id(&self) -> Option<&str> {
325 self.service()
326 .and_then(|m| m.extended_request_id.as_deref())
327 }
328
329 pub fn is_not_found(&self) -> bool {
332 matches!(
333 self.code(),
334 Some("NotFound" | "NoSuchKey" | "NoSuchUpload" | "NoSuchBucket")
335 )
336 }
337
338 pub(crate) fn is_transient_transport(&self) -> bool {
344 self.extra.as_ref().is_some_and(|e| e.transient_transport)
345 }
346
347 pub fn chunk(&self) -> Option<&ChunkRef> {
349 self.extra.as_ref().and_then(|e| e.location.as_ref())
350 }
351
352 pub fn upload_id(&self) -> Option<&str> {
370 self.extra.as_ref().and_then(|e| e.upload_id.as_deref())
371 }
372
373 pub fn failed_uploads(&self) -> Option<&[FailedUpload]> {
375 self.extra
376 .as_ref()
377 .and_then(|e| e.failed_uploads.as_deref())
378 }
379
380 pub fn failed_downloads(&self) -> Option<&[FailedDownload]> {
382 self.extra
383 .as_ref()
384 .and_then(|e| e.failed_downloads.as_deref())
385 }
386
387 pub(crate) fn with_chunk(mut self, location: ChunkRef) -> Self {
389 self.extra_mut().location = Some(location);
390 self
391 }
392
393 pub(crate) fn with_local(mut self, action: &'static str, path: impl Into<PathBuf>) -> Self {
396 self.extra_mut().local = Some(LocalContext {
397 action,
398 path: path.into(),
399 key: None,
400 });
401 self
402 }
403
404 pub(crate) fn with_local_key(mut self, key: impl Into<String>) -> Self {
407 if let Some(local) = self.extra.as_mut().and_then(|e| e.local.as_mut()) {
408 local.key = Some(key.into());
409 }
410 self
411 }
412
413 pub(crate) fn with_upload_id(mut self, upload_id: String) -> Self {
416 self.extra_mut().upload_id = Some(upload_id);
417 self
418 }
419
420 pub(crate) fn with_failed_uploads(mut self, failed: Vec<FailedUpload>) -> Self {
422 self.extra_mut().failed_uploads = Some(failed);
423 self
424 }
425
426 pub(crate) fn with_failed_downloads(mut self, failed: Vec<FailedDownload>) -> Self {
428 self.extra_mut().failed_downloads = Some(failed);
429 self
430 }
431}
432
433impl fmt::Display for Error {
434 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
438 match &self.kind {
439 ErrorKind::InputInvalid => write!(f, "invalid input")?,
440 ErrorKind::IOError => write!(f, "I/O error")?,
441 ErrorKind::RuntimeError => write!(f, "runtime error")?,
442 ErrorKind::ObjectNotDiscoverable => write!(f, "object discovery failed")?,
443 ErrorKind::ChildOperationFailed => {
444 write!(f, "child operation failed")?;
445 let first_and_count = self
450 .failed_uploads()
451 .map(|f| {
452 let id = f
453 .first()
454 .and_then(|u| u.source_path())
455 .map(|p| p.display().to_string());
456 (id, f.len())
457 })
458 .or_else(|| {
459 self.failed_downloads().map(|f| {
460 let id = f.first().and_then(|d| d.input().key()).map(str::to_owned);
461 (id, f.len())
462 })
463 });
464 if let Some((id, n)) = first_and_count {
465 match id {
466 Some(id) => write!(f, ": {id}")?,
467 None => write!(f, ": 1 object")?,
468 }
469 if n > 1 {
470 write!(f, " (and {} more)", n - 1)?;
471 }
472 }
473 }
474 ErrorKind::OperationCancelled => write!(f, "operation cancelled")?,
475 ErrorKind::ServiceError => {
476 write!(f, "service error")?;
477 if let Some(m) = self.service() {
478 write!(f, " calling {}", m.operation)?;
479 if let Some(c) = &m.code {
480 write!(f, " ({c})")?;
481 }
482 }
483 }
484 ErrorKind::IntegrityError(ie) => {
485 write!(f, "integrity error")?;
486 if let Some(a) = &ie.algorithm {
487 write!(f, " ({a})")?;
488 }
489 }
490 }
491
492 if let Some(local) = self.extra.as_ref().and_then(|e| e.local.as_ref()) {
493 write!(f, " {} \"{}\"", local.action, local.path.display())?;
494 if let Some(key) = &local.key {
495 write!(f, " (key \"{key}\")")?;
496 }
497 }
498
499 if let Some(loc) = self.chunk() {
500 match &loc.range {
501 Some(r) => write!(f, " [chunk {}, bytes {}-{}]", loc.seq, r.start(), r.end())?,
502 None => write!(f, " [chunk {}]", loc.seq)?,
503 }
504 }
505
506 if let Some(m) = self.service() {
509 match (&m.request_id, &m.extended_request_id) {
510 (Some(rid), Some(ext)) => {
511 write!(f, " (request id: {rid}, extended request id: {ext})")?
512 }
513 (Some(rid), None) => write!(f, " (request id: {rid})")?,
514 _ => {}
515 }
516 }
517 Ok(())
518 }
519}
520
521impl std::error::Error for Error {
522 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
529 if matches!(self.kind, ErrorKind::ChildOperationFailed) {
530 let first_child = self
531 .failed_uploads()
532 .and_then(<[FailedUpload]>::first)
533 .map(FailedUpload::error)
534 .or_else(|| {
535 self.failed_downloads()
536 .and_then(<[FailedDownload]>::first)
537 .map(FailedDownload::error)
538 });
539 if let Some(child) = first_child {
540 return Some(child);
541 }
542 }
543 Some(self.source.as_ref())
544 }
545}
546
547impl From<std::io::Error> for Error {
548 fn from(value: std::io::Error) -> Self {
549 Self::new(ErrorKind::IOError, value)
550 }
551}
552
553impl From<aws_smithy_types::error::operation::BuildError> for Error {
554 fn from(value: aws_smithy_types::error::operation::BuildError) -> Self {
555 Self::new(ErrorKind::InputInvalid, value)
556 }
557}
558
559pub(crate) fn invalid_input<E>(err: E) -> Error
560where
561 E: Into<BoxError>,
562{
563 Error::new(ErrorKind::InputInvalid, err)
564}
565
566pub(crate) fn io_at(action: &'static str, path: impl Into<PathBuf>, err: std::io::Error) -> Error {
573 Error::new(ErrorKind::IOError, err).with_local(action, path)
574}
575
576pub(crate) fn discovery_failed<E>(err: E) -> Error
580where
581 E: Into<BoxError>,
582{
583 Error::new(ErrorKind::ObjectNotDiscoverable, err)
584}
585
586pub(crate) fn from_kind<E>(kind: ErrorKind) -> impl FnOnce(E) -> Error
587where
588 E: Into<BoxError>,
589{
590 |err| Error::new(kind, err)
591}
592
593pub(crate) fn body_read_error<E>(err: E, checksum: Option<ChecksumOutcome>) -> Error
600where
601 E: Into<BoxError>,
602{
603 match checksum {
604 Some(ChecksumOutcome::Mismatch {
605 algorithm,
606 expected,
607 computed,
608 }) => Error {
609 kind: ErrorKind::IntegrityError(IntegrityError {
610 algorithm: Some(checksum_algorithm(algorithm)),
611 expected: Some(expected),
612 computed: Some(computed),
613 }),
614 source: err.into(),
615 extra: None,
616 },
617 _ => Error::new(ErrorKind::IOError, err),
618 }
619}
620
621pub(crate) fn integrity_mismatch(
625 algorithm: ChecksumAlgorithm,
626 expected: String,
627 computed: String,
628) -> Error {
629 Error::new(
630 ErrorKind::IntegrityError(IntegrityError {
631 algorithm: Some(algorithm),
632 expected: Some(expected),
633 computed: Some(computed),
634 }),
635 "calculated object checksum does not match its stored value",
636 )
637}
638
639pub(crate) fn checksum_algorithm(algorithm: ResponseChecksumAlgorithm) -> ChecksumAlgorithm {
644 ChecksumAlgorithm::from(algorithm.as_str().to_ascii_uppercase().as_str())
645}
646
647fn service_metadata<E, R>(operation: &'static str, e: &SdkError<E, R>) -> ServiceMetadata
650where
651 E: ProvideErrorMetadata,
652 SdkError<E, R>: RequestId + RequestIdExt,
653{
654 ServiceMetadata {
655 operation,
656 code: e.code().map(str::to_owned),
657 message: e.message().map(str::to_owned),
658 request_id: e.request_id().map(str::to_owned),
659 extended_request_id: e.extended_request_id().map(str::to_owned),
660 }
661}
662
663fn connector_error_is_transient(conn: &ConnectorError) -> bool {
669 conn.is_io() || conn.is_timeout() || conn.as_other() == Some(RetryErrorKind::TransientError)
670}
671
672fn is_sdk_transient_transport<E, R>(e: &SdkError<E, R>) -> bool {
689 if let SdkError::TimeoutError(_) = e {
690 return true;
691 }
692 let conn = match e {
695 SdkError::DispatchFailure(df) => df.as_connector_error(),
696 _ => return false,
697 };
698 let Some(conn) = conn else { return false };
699 if connector_error_is_transient(conn) {
700 return true;
701 }
702 let mut source = std::error::Error::source(conn);
703 while let Some(err) = source {
704 if let Some(conn) = err.downcast_ref::<ConnectorError>() {
705 if connector_error_is_transient(conn) {
706 return true;
707 }
708 }
709 source = err.source();
710 }
711 false
712}
713
714pub(crate) trait SdkOperationError:
716 ProvideErrorMetadata + std::error::Error + Send + Sync + 'static
717{
718 const OPERATION_NAME: &'static str;
719}
720
721pub(crate) fn service_error<E: SdkOperationError>(e: SdkError<E, HttpResponse>) -> Error {
725 let service = service_metadata(E::OPERATION_NAME, &e);
726 let http_status = e.raw_response().map(|response| response.status().as_u16());
727 let transient_transport = is_sdk_transient_transport(&e);
728 Error {
729 kind: ErrorKind::ServiceError,
730 source: Box::new(e),
731 extra: Some(Box::new(ErrorExtra {
732 service: Some(service),
733 http_status,
734 transient_transport,
735 ..Default::default()
736 })),
737 }
738}
739
740pub(crate) fn head_object_error(e: SdkError<HeadObjectError, HttpResponse>) -> Error {
748 let mut error = service_error(e);
749 let code = error
750 .extra
751 .as_ref()
752 .and_then(|extra| extra.http_status)
753 .and_then(head_status_code);
754 if let (Some(code), Some(service)) = (
755 code,
756 error
757 .extra
758 .as_mut()
759 .and_then(|extra| extra.service.as_mut()),
760 ) {
761 service.code.get_or_insert_with(|| code.to_owned());
762 }
763 error
764}
765
766fn head_status_code(status: u16) -> Option<&'static str> {
768 match status {
769 304 => Some("NotModified"),
770 412 => Some("PreconditionFailed"),
771 416 => Some("InvalidRange"),
772 _ => None,
773 }
774}
775
776pub(crate) fn invalid_service_response<O>(
783 operation: &'static str,
784 output: &O,
785 message: impl Into<String>,
786) -> Error
787where
788 O: RequestId + RequestIdExt,
789{
790 invalid_service_response_ids(
791 operation,
792 output.request_id(),
793 output.extended_request_id(),
794 message,
795 )
796}
797
798pub(crate) fn invalid_service_response_ids(
801 operation: &'static str,
802 request_id: Option<&str>,
803 extended_request_id: Option<&str>,
804 message: impl Into<String>,
805) -> Error {
806 let message = message.into();
807 Error {
808 kind: ErrorKind::ServiceError,
809 source: message.clone().into(),
810 extra: Some(Box::new(ErrorExtra {
811 service: Some(ServiceMetadata {
812 operation,
813 code: None,
814 message: Some(message),
815 request_id: request_id.map(str::to_owned),
816 extended_request_id: extended_request_id.map(str::to_owned),
817 }),
818 ..Default::default()
819 })),
820 }
821}
822
823macro_rules! sdk_operation_error {
824 ($err:ty, $op:literal) => {
825 impl SdkOperationError for $err {
826 const OPERATION_NAME: &'static str = $op;
827 }
828 };
829}
830
831sdk_operation_error!(GetObjectError, "GetObject");
832sdk_operation_error!(HeadObjectError, "HeadObject");
833sdk_operation_error!(PutObjectError, "PutObject");
834sdk_operation_error!(UploadPartError, "UploadPart");
835sdk_operation_error!(CreateMultipartUploadError, "CreateMultipartUpload");
836sdk_operation_error!(CompleteMultipartUploadError, "CompleteMultipartUpload");
837sdk_operation_error!(AbortMultipartUploadError, "AbortMultipartUpload");
838sdk_operation_error!(ListObjectsV2Error, "ListObjectsV2");
839
840impl From<crate::io::walk::WalkError> for Error {
841 fn from(e: crate::io::walk::WalkError) -> Self {
850 use crate::io::walk::WalkErrorKind;
851 let path = e.path().map(std::path::Path::to_path_buf);
852 let local = |err: Error| match path {
853 Some(path) => err.with_local("walking", path),
854 None => err,
855 };
856 match e.kind() {
857 WalkErrorKind::Service => {
858 match e
861 .into_source()
862 .downcast::<SdkError<ListObjectsV2Error, HttpResponse>>()
863 {
864 Ok(sdk) => service_error(*sdk),
865 Err(src) => Error::new(ErrorKind::ObjectNotDiscoverable, src),
866 }
867 }
868 WalkErrorKind::SourceUnreadable | WalkErrorKind::NotADirectory => {
869 local(Error::new(ErrorKind::InputInvalid, e))
870 }
871 WalkErrorKind::Io
872 | WalkErrorKind::PermissionDenied
873 | WalkErrorKind::BrokenSymlink
874 | WalkErrorKind::SymlinkCycle => local(Error::new(ErrorKind::IOError, e)),
875 }
876 }
877}
878
879#[cfg(test)]
880mod tests {
881 use super::*;
882
883 fn service(
885 operation: &'static str,
886 code: Option<&str>,
887 request_id: Option<&str>,
888 extended_request_id: Option<&str>,
889 ) -> Error {
890 Error {
891 kind: ErrorKind::ServiceError,
892 source: "boom".into(),
893 extra: Some(Box::new(ErrorExtra {
894 service: Some(ServiceMetadata {
895 operation,
896 code: code.map(str::to_owned),
897 message: Some("the message".to_owned()),
898 request_id: request_id.map(str::to_owned),
899 extended_request_id: extended_request_id.map(str::to_owned),
900 }),
901 ..Default::default()
902 })),
903 }
904 }
905
906 #[test]
907 fn display_plain_kind() {
908 let e = Error::new(ErrorKind::OperationCancelled, "x");
909 assert_eq!(e.to_string(), "operation cancelled");
910 }
911
912 #[test]
916 fn body_read_error_classifies_mismatch_from_checksum_outcome() {
917 let outcome = ChecksumOutcome::Mismatch {
918 algorithm: ResponseChecksumAlgorithm::Crc32,
919 expected: "AAAAAA==".to_owned(),
920 computed: "BBBBBB==".to_owned(),
921 };
922 let e = body_read_error("opaque body error", Some(outcome));
923 let ErrorKind::IntegrityError(ie) = e.kind() else {
924 panic!("expected IntegrityError, got {:?}", e.kind());
925 };
926 assert_eq!(ie.algorithm(), Some(&ChecksumAlgorithm::Crc32));
927 assert_eq!(ie.expected(), Some("AAAAAA=="));
928 assert_eq!(ie.computed(), Some("BBBBBB=="));
929 }
930
931 #[test]
934 fn checksum_algorithm_maps_each_known_name() {
935 for (algorithm, expected) in [
936 (ResponseChecksumAlgorithm::Crc32, ChecksumAlgorithm::Crc32),
937 (ResponseChecksumAlgorithm::Crc32c, ChecksumAlgorithm::Crc32C),
938 (
939 ResponseChecksumAlgorithm::Crc64Nvme,
940 ChecksumAlgorithm::Crc64Nvme,
941 ),
942 (ResponseChecksumAlgorithm::Sha1, ChecksumAlgorithm::Sha1),
943 (ResponseChecksumAlgorithm::Sha256, ChecksumAlgorithm::Sha256),
944 ] {
945 assert_eq!(checksum_algorithm(algorithm), expected);
946 }
947 }
948
949 #[test]
950 fn display_service_error_full() {
951 let e = service(
952 "GetObject",
953 Some("NoSuchKey"),
954 Some("REQ123"),
955 Some("EXT456"),
956 );
957 assert_eq!(
958 e.to_string(),
959 "service error calling GetObject (NoSuchKey) (request id: REQ123, extended request id: EXT456)"
960 );
961 }
962
963 #[test]
964 fn display_service_error_request_id_only() {
965 let e = service("PutObject", Some("AccessDenied"), Some("REQ123"), None);
966 assert_eq!(
967 e.to_string(),
968 "service error calling PutObject (AccessDenied) (request id: REQ123)"
969 );
970 }
971
972 #[test]
973 fn display_service_error_no_metadata() {
974 let e = service("HeadObject", None, None, None);
975 assert_eq!(e.to_string(), "service error calling HeadObject");
976 }
977
978 #[test]
979 fn display_omits_source_message() {
980 let e = service("GetObject", Some("NoSuchKey"), None, None);
982 assert!(!e.to_string().contains("the message"));
983 }
984
985 #[test]
986 fn display_integrity_error() {
987 let e = Error {
988 kind: ErrorKind::IntegrityError(IntegrityError {
989 algorithm: Some(ChecksumAlgorithm::Crc32),
990 expected: Some("AAAA".to_owned()),
991 computed: Some("BBBB".to_owned()),
992 }),
993 source: "mismatch".into(),
994 extra: None,
995 };
996 assert_eq!(e.to_string(), "integrity error (CRC32)");
998 }
999
1000 #[test]
1001 fn display_with_chunk() {
1002 let e = Error::new(ErrorKind::IOError, "x").with_chunk(ChunkRef::new(3, Some(0..=1023)));
1003 assert_eq!(e.to_string(), "I/O error [chunk 3, bytes 0-1023]");
1004 }
1005
1006 #[test]
1007 fn display_chunk_no_range() {
1008 let e = Error::new(ErrorKind::IOError, "x").with_chunk(ChunkRef::new(7, None));
1009 assert_eq!(e.to_string(), "I/O error [chunk 7]");
1010 }
1011
1012 #[test]
1013 fn is_not_found_matches_codes() {
1014 assert!(service("GetObject", Some("NoSuchKey"), None, None).is_not_found());
1015 assert!(!service("GetObject", Some("AccessDenied"), None, None).is_not_found());
1016 }
1017
1018 #[test]
1019 fn display_child_operation_failed_bare() {
1020 let e = Error::new(ErrorKind::ChildOperationFailed, "x");
1022 assert_eq!(e.to_string(), "child operation failed");
1023 }
1024
1025 #[test]
1026 fn display_child_operation_failed_names_first_and_count() {
1027 let failed = vec![
1028 FailedUpload {
1029 input: None,
1030 error: Error::new(ErrorKind::ServiceError, "boom"),
1031 source_path: Some(std::path::PathBuf::from("/data/a.bin")),
1032 },
1033 FailedUpload {
1034 input: None,
1035 error: Error::new(ErrorKind::ServiceError, "boom"),
1036 source_path: Some(std::path::PathBuf::from("/data/b.bin")),
1037 },
1038 ];
1039 let e = Error::new(ErrorKind::ChildOperationFailed, "aborted").with_failed_uploads(failed);
1040 assert_eq!(
1041 e.to_string(),
1042 "child operation failed: /data/a.bin (and 1 more)"
1043 );
1044 }
1045
1046 #[test]
1047 fn display_child_operation_failed_single_no_path() {
1048 let failed = vec![FailedUpload {
1050 input: None,
1051 error: Error::new(ErrorKind::ServiceError, "boom"),
1052 source_path: None,
1053 }];
1054 let e = Error::new(ErrorKind::ChildOperationFailed, "aborted").with_failed_uploads(failed);
1055 assert_eq!(e.to_string(), "child operation failed: 1 object");
1056 }
1057
1058 #[test]
1059 fn display_local_context_names_action_and_path() {
1060 let e = io_at(
1061 "creating temporary file for",
1062 "out/logs/a.log",
1063 std::io::Error::from(std::io::ErrorKind::NotFound),
1064 );
1065 assert_eq!(
1066 e.to_string(),
1067 "I/O error creating temporary file for \"out/logs/a.log\""
1068 );
1069 let source = std::error::Error::source(&e)
1071 .and_then(|s| s.downcast_ref::<std::io::Error>())
1072 .expect("the io::Error is the direct source");
1073 assert_eq!(source.kind(), std::io::ErrorKind::NotFound);
1074
1075 let e = io_at(
1076 "creating directory",
1077 "out/logs",
1078 std::io::Error::from(std::io::ErrorKind::PermissionDenied),
1079 )
1080 .with_local_key("logs/a.log");
1081 assert_eq!(
1082 e.to_string(),
1083 "I/O error creating directory \"out/logs\" (key \"logs/a.log\")"
1084 );
1085 let e = Error::new(ErrorKind::IOError, "x").with_local_key("logs/a.log");
1087 assert_eq!(e.to_string(), "I/O error");
1088 }
1089
1090 #[test]
1091 fn display_walker_error_names_path() {
1092 use crate::io::walk::{WalkError, WalkErrorKind};
1093 let e = Error::from(WalkError::new(
1094 Some(std::path::PathBuf::from("/data/sub")),
1095 WalkErrorKind::PermissionDenied,
1096 Box::new(std::io::Error::from(std::io::ErrorKind::PermissionDenied)),
1097 ));
1098 assert_eq!(e.kind(), &ErrorKind::IOError);
1099 assert_eq!(e.to_string(), "I/O error walking \"/data/sub\"");
1100
1101 let root = Error::from(WalkError::new(
1102 Some(std::path::PathBuf::from("/data/missing")),
1103 WalkErrorKind::SourceUnreadable,
1104 Box::new(std::io::Error::from(std::io::ErrorKind::NotFound)),
1105 ));
1106 assert_eq!(root.kind(), &ErrorKind::InputInvalid);
1107 assert_eq!(root.to_string(), "invalid input walking \"/data/missing\"");
1108 }
1109
1110 #[test]
1111 fn child_operation_failed_source_is_first_failed_child() {
1112 let failed = vec![
1113 FailedUpload {
1114 input: None,
1115 error: Error::new(ErrorKind::ServiceError, "first"),
1116 source_path: None,
1117 },
1118 FailedUpload {
1119 input: None,
1120 error: Error::new(ErrorKind::ServiceError, "second"),
1121 source_path: None,
1122 },
1123 ];
1124 let e = Error::new(ErrorKind::ChildOperationFailed, "aborted").with_failed_uploads(failed);
1125 let source = std::error::Error::source(&e)
1126 .and_then(|s| s.downcast_ref::<Error>())
1127 .expect("the source is a child's error");
1128 assert!(std::ptr::eq(source, e.failed_uploads().unwrap()[0].error()));
1129
1130 let bare = Error::new(ErrorKind::ChildOperationFailed, "aborted");
1132 assert_eq!(
1133 std::error::Error::source(&bare).map(|s| s.to_string()),
1134 Some("aborted".to_owned())
1135 );
1136 }
1137
1138 #[test]
1139 fn upload_id_is_none_unless_attached() {
1140 let e = Error::new(ErrorKind::ServiceError, "x");
1141 assert_eq!(e.upload_id(), None);
1142 let e = e.with_upload_id("abc".to_owned());
1143 assert_eq!(e.upload_id(), Some("abc"));
1144 assert_eq!(e.to_string(), "service error");
1146 }
1147
1148 use aws_sdk_s3::operation::get_object::GetObjectError;
1151
1152 fn dispatch(conn: ConnectorError) -> SdkError<GetObjectError, HttpResponse> {
1153 SdkError::dispatch_failure(conn)
1154 }
1155
1156 #[test]
1157 fn transient_nested_transient_marker_under_other_none_is_transient() {
1158 let inner = ConnectorError::other(
1165 "incomplete message".into(),
1166 Some(RetryErrorKind::TransientError),
1167 );
1168 let inner_sdk: SdkError<GetObjectError, HttpResponse> = SdkError::dispatch_failure(inner);
1169 let outer = ConnectorError::other(Box::new(inner_sdk), None);
1170 assert!(
1171 is_sdk_transient_transport(&dispatch(outer)),
1172 "a TransientError marker nested under an outer Other(None) must classify transient"
1173 );
1174 }
1175
1176 #[test]
1177 fn transient_outer_io_is_transient() {
1178 assert!(is_sdk_transient_transport(&dispatch(ConnectorError::io(
1179 "reset".into()
1180 ))));
1181 }
1182
1183 #[test]
1184 fn transient_outer_timeout_is_transient() {
1185 assert!(is_sdk_transient_transport(&dispatch(
1186 ConnectorError::timeout("connect".into())
1187 )));
1188 }
1189
1190 #[test]
1191 fn transient_bare_other_none_is_not_transient() {
1192 assert!(!is_sdk_transient_transport(&dispatch(
1195 ConnectorError::other("unclassified".into(), None)
1196 )));
1197 }
1198
1199 #[test]
1200 fn transient_user_error_is_not_transient() {
1201 assert!(!is_sdk_transient_transport(&dispatch(
1203 ConnectorError::user("bad request".into())
1204 )));
1205 }
1206
1207 use aws_smithy_runtime_api::http::StatusCode;
1210 use aws_smithy_types::body::SdkBody;
1211
1212 #[test]
1213 fn precondition_failure_surfaces_as_service_error_with_code() {
1214 let sdk: SdkError<PutObjectError, HttpResponse> = SdkError::service_error(
1218 PutObjectError::generic(
1219 aws_smithy_types::error::ErrorMetadata::builder()
1220 .code("PreconditionFailed")
1221 .build(),
1222 ),
1223 HttpResponse::new(StatusCode::try_from(412).unwrap(), SdkBody::empty()),
1224 );
1225 let err = service_error(sdk);
1226 assert!(matches!(err.kind(), ErrorKind::ServiceError));
1227 assert_eq!(err.code(), Some("PreconditionFailed"));
1228 assert_eq!(err.operation_name(), Some("PutObject"));
1229 assert!(std::error::Error::source(&err)
1231 .and_then(|s| s.downcast_ref::<SdkError<PutObjectError, HttpResponse>>())
1232 .is_some());
1233 }
1234}