Skip to content

Commit

Permalink
sqs: Print error chain for aws SDK errors (#67)
Browse files Browse the repository at this point in the history
As recommended in the documentation for SdkError:
https://docs.rs/aws-smithy-runtime-api/1.3.0/aws_smithy_runtime_api/client/result/enum.SdkError.html

Note: Not using `DisplayErrorContext` as it also debug-prints the whole
error after showing the error chain, which includes the raw response.
  • Loading branch information
svix-jplatte committed Apr 3, 2024
1 parent 975574b commit 6445fdb
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions omniqueue/src/backends/sqs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::time::Duration;
use std::{
fmt::{self, Write},
time::Duration,
};

use async_trait::async_trait;
use aws_sdk_sqs::{
Expand Down Expand Up @@ -190,7 +193,7 @@ impl Acker for SqsAcker {
.receipt_handle(receipt_handle)
.send()
.await
.map_err(QueueError::generic)?;
.map_err(aws_to_queue_error)?;

self.has_been_acked_or_nacked = true;

Expand Down Expand Up @@ -226,7 +229,7 @@ impl SqsProducer {
.message_body(payload)
.send()
.await
.map_err(QueueError::generic)?;
.map_err(aws_to_queue_error)?;

Ok(())
}
Expand All @@ -244,7 +247,7 @@ impl SqsProducer {
.delay_seconds(delay.as_secs().try_into().map_err(QueueError::generic)?)
.send()
.await
.map_err(QueueError::generic)?;
.map_err(aws_to_queue_error)?;

Ok(())
}
Expand Down Expand Up @@ -288,7 +291,7 @@ impl SqsConsumer {
.queue_url(&self.queue_dsn)
.send()
.await
.map_err(QueueError::generic)?;
.map_err(aws_to_queue_error)?;

out.messages()
.iter()
Expand All @@ -312,7 +315,7 @@ impl SqsConsumer {
.queue_url(&self.queue_dsn)
.send()
.await
.map_err(QueueError::generic)?;
.map_err(aws_to_queue_error)?;

out.messages()
.iter()
Expand All @@ -322,3 +325,22 @@ impl SqsConsumer {
}

impl_queue_consumer!(SqsConsumer, String);

fn aws_to_queue_error<E>(err: aws_sdk_sqs::error::SdkError<E>) -> QueueError
where
E: std::error::Error + 'static,
{
let mut message = String::new();
write_err(&mut message, &err).expect("Write to string never fails");
QueueError::Generic(message.into())
}

fn write_err(s: &mut String, err: &dyn std::error::Error) -> fmt::Result {
write!(s, "{err}")?;
if let Some(source) = err.source() {
write!(s, ": ")?;
write_err(s, source)?;
}

Ok(())
}

0 comments on commit 6445fdb

Please sign in to comment.