-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract crafting of error MQTT message from operation handlers
1. Changed error type from `ConversionError` to `OperationError` which displays its source error in a single line 2. Added context to some operation error cases Signed-off-by: Marcel Guzik <[email protected]>
- Loading branch information
Showing
9 changed files
with
194 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::error::Error; | ||
use std::fmt::Display; | ||
|
||
/// An error type for operation handlers. | ||
/// | ||
/// This error type wraps an [`anyhow::Error`] and provides a display implementation that assumes | ||
/// that source errors print their own causes after `:` character. This is in order to ensure that | ||
/// failures are printed properly in Cumulocity web interface. | ||
#[derive(Debug)] | ||
pub(crate) struct OperationError(anyhow::Error); | ||
|
||
impl Display for OperationError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
self.0.fmt(f)?; | ||
|
||
if let Some(source) = self.0.source() { | ||
write!(f, ": {}", source)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl Error for OperationError { | ||
fn source(&self) -> Option<&(dyn Error + 'static)> { | ||
self.0.source() | ||
} | ||
} | ||
|
||
impl From<anyhow::Error> for OperationError { | ||
fn from(error: anyhow::Error) -> Self { | ||
Self(error) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use anyhow::Context; | ||
|
||
#[test] | ||
fn separates_error_levels_on_a_single_line() { | ||
let example_io_err: Result<(), std::io::Error> = Err(std::io::Error::new( | ||
std::io::ErrorKind::InvalidInput, | ||
"Example io error", | ||
)); | ||
|
||
let operation_error: OperationError = example_io_err | ||
.context("Could not perform io operation") | ||
.unwrap_err() | ||
.into(); | ||
|
||
assert_eq!( | ||
&operation_error.to_string(), | ||
"Could not perform io operation: Example io error" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.