refactor: impl Error for FieldViolation (#975)
parent
6ac7e2c1a7
commit
2456bc1b28
|
@ -84,6 +84,18 @@ impl FieldViolation {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for FieldViolation {}
|
||||
|
||||
impl std::fmt::Display for FieldViolation {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"Violation for field \"{}\": {}",
|
||||
self.field, self.description
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn encode_bad_request(violation: Vec<FieldViolation>) -> Result<Any, EncodeError> {
|
||||
let mut buffer = BytesMut::new();
|
||||
|
||||
|
@ -106,7 +118,7 @@ fn encode_bad_request(violation: Vec<FieldViolation>) -> Result<Any, EncodeError
|
|||
|
||||
impl From<FieldViolation> for tonic::Status {
|
||||
fn from(f: FieldViolation) -> Self {
|
||||
let message = format!("Violation for field \"{}\": {}", f.field, f.description);
|
||||
let message = f.to_string();
|
||||
|
||||
match encode_bad_request(vec![f]) {
|
||||
Ok(details) => encode_status(tonic::Code::InvalidArgument, message, details),
|
||||
|
|
|
@ -14,8 +14,8 @@ pub enum Error {
|
|||
#[error("Error listing chunks: {0}")]
|
||||
ListChunkError(#[from] ListChunksError),
|
||||
|
||||
#[error("Error interpreting server response: {:?}", .0)]
|
||||
ConvertingResponse(FieldViolation),
|
||||
#[error("Error interpreting server response: {0}")]
|
||||
ConvertingResponse(#[from] FieldViolation),
|
||||
|
||||
#[error("Error rendering response as JSON: {0}")]
|
||||
WritingJson(#[from] serde_json::Error),
|
||||
|
@ -62,8 +62,8 @@ pub async fn command(url: String, config: Config) -> Result<()> {
|
|||
|
||||
let chunks = chunks
|
||||
.into_iter()
|
||||
.map(|c| ChunkSummary::try_from(c).map_err(Error::ConvertingResponse))
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
.map(ChunkSummary::try_from)
|
||||
.collect::<Result<Vec<_>, FieldViolation>>()?;
|
||||
|
||||
serde_json::to_writer_pretty(std::io::stdout(), &chunks).map_err(Error::WritingJson)?;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue