Skip to content

Commit

Permalink
Merge pull request #157 from abdolence/feature/error-handling-improvm…
Browse files Browse the repository at this point in the history
…ents

Improved error messages and details
  • Loading branch information
abdolence authored Mar 5, 2024
2 parents a8fc257 + b2e929e commit 659a534
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 33 deletions.
21 changes: 11 additions & 10 deletions src/db/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ impl FirestoreGetByIdSupport for FirestoreDb {
Err(err) => {
error!(
%err,
"Error occurred while consuming batch documents as a stream.",
"Error occurred while consuming batch documents as a stream. Document: {}",
doc_id
);
None
}
Expand Down Expand Up @@ -627,24 +628,24 @@ impl FirestoreDb {
}
Err(err) => match err {
FirestoreError::DatabaseError(ref db_err)
if db_err.retry_possible && retries < self.get_options().max_retries =>
{
span.in_scope(|| {
warn!(
if db_err.retry_possible && retries < self.get_options().max_retries =>
{
span.in_scope(|| {
warn!(
err = %db_err,
current_retry = retries + 1,
max_retries = self.get_options().max_retries,
"Failed to get document. Retrying up to the specified number of times.",
);
});
self.get_doc_by_path(collection_id, document_path, None, retries + 1)
.await
}
});
self.get_doc_by_path(collection_id, document_path, None, retries + 1)
.await
}
_ => Err(err),
},
}
}
.boxed()
.boxed()
}

pub(crate) async fn get_docs_by_ids(
Expand Down
29 changes: 15 additions & 14 deletions src/db/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ impl FirestoreListingSupport for FirestoreDb {
Err(err) => {
error!(
%err,
"Error occurred while consuming list document as a stream.",
"Error occurred while deserializing a document inside a stream. Document: {}",
doc.name
);
None
}
Expand Down Expand Up @@ -349,22 +350,22 @@ impl FirestoreDb {
}
Err(err) => match err {
FirestoreError::DatabaseError(ref db_err)
if db_err.retry_possible && retries < db_inner.options.max_retries =>
{
warn!(
if db_err.retry_possible && retries < db_inner.options.max_retries =>
{
warn!(
err = %db_err,
current_retry = retries + 1,
max_retries = db_inner.options.max_retries,
"Failed to list documents. Retrying up to the specified number of times.",
);

Self::list_doc_with_retries_inner(db_inner, list_request, retries + 1, span).await
}
Self::list_doc_with_retries_inner(db_inner, list_request, retries + 1, span).await
}
_ => Err(err),
},
}
}
.boxed()
.boxed()
}

async fn stream_list_doc_with_retries<'b>(
Expand Down Expand Up @@ -503,23 +504,23 @@ impl FirestoreDb {
}
Err(err) => match err {
FirestoreError::DatabaseError(ref db_err)
if db_err.retry_possible && retries < self.inner.options.max_retries =>
{
warn!(
if db_err.retry_possible && retries < self.inner.options.max_retries =>
{
warn!(
err = %db_err,
current_retry = retries + 1,
max_retries = self.inner.options.max_retries,
"Failed to list collection IDs. Retrying up to the specified number of times.",
);

self.list_collection_ids_with_retries(params, retries + 1, span)
.await
}
self.list_collection_ids_with_retries(params, retries + 1, span)
.await
}
_ => Err(err),
},
}
}
.boxed()
.boxed()
}

#[cfg(feature = "caching")]
Expand Down
3 changes: 2 additions & 1 deletion src/db/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ impl FirestoreQuerySupport for FirestoreDb {
Err(err) => {
error!(
%err,
"Error occurred while converting query document in a stream.",
"Error occurred while converting query document in a stream. Document: {}",
doc.name
);
None
}
Expand Down
52 changes: 44 additions & 8 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ pub struct FirestoreErrorPublicGenericDetails {
pub code: String,
}

impl Display for FirestoreErrorPublicGenericDetails {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "Error code: {}", self.code)
}
}

#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct FirestoreSystemError {
pub public: FirestoreErrorPublicGenericDetails,
Expand All @@ -67,7 +73,11 @@ pub struct FirestoreSystemError {

impl Display for FirestoreSystemError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "Firestore system/internal error: {}", self.message)
write!(
f,
"Firestore system/internal error: {}. {}",
self.public, self.message
)
}
}

Expand All @@ -82,7 +92,11 @@ pub struct FirestoreDatabaseError {

impl Display for FirestoreDatabaseError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "Database general error occurred: {}", self.details)
write!(
f,
"Database general error occurred: {}. {}. Retry possibility: {}",
self.public, self.details, self.retry_possible
)
}
}

Expand All @@ -96,7 +110,11 @@ pub struct FirestoreDataConflictError {

impl Display for FirestoreDataConflictError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "Database conflict error occurred: {}", self.details)
write!(
f,
"Database conflict error occurred: {}. {}",
self.public, self.details
)
}
}

Expand All @@ -110,7 +128,11 @@ pub struct FirestoreDataNotFoundError {

impl Display for FirestoreDataNotFoundError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "Data not found error occurred: {:?}", self.public)
write!(
f,
"Data not found error occurred: {}. {}",
self.public, self.data_detail_message
)
}
}

Expand All @@ -122,14 +144,24 @@ pub struct FirestoreInvalidParametersPublicDetails {
pub error: String,
}

impl Display for FirestoreInvalidParametersPublicDetails {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(
f,
"Invalid parameters error: {}. {}",
self.field, self.error
)
}
}

#[derive(Debug, Clone, Builder)]
pub struct FirestoreInvalidParametersError {
pub public: FirestoreInvalidParametersPublicDetails,
}

impl Display for FirestoreInvalidParametersError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "Data not found error occurred: {:?}", self.public)
write!(f, "Data not found error occurred: {}", self.public)
}
}

Expand All @@ -148,7 +180,7 @@ pub struct FirestoreNetworkError {

impl Display for FirestoreNetworkError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "Network error: {}", self.message)
write!(f, "Network error: {}. {}", self.public, self.message)
}
}

Expand Down Expand Up @@ -270,7 +302,11 @@ impl FirestoreSerializationError {

impl Display for FirestoreSerializationError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "Invalid serialization: {:?}", self.public)
write!(
f,
"Invalid serialization: {}. {}",
self.public, self.message
)
}
}

Expand All @@ -284,7 +320,7 @@ pub struct FirestoreCacheError {

impl Display for FirestoreCacheError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "Cache error: {}", self.message)
write!(f, "Cache error: {}. {}", self.public, self.message)
}
}

Expand Down

0 comments on commit 659a534

Please sign in to comment.