Skip to content

Commit

Permalink
error messages improved
Browse files Browse the repository at this point in the history
  • Loading branch information
124C41p committed Apr 1, 2024
1 parent 298b97a commit ff20949
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
10 changes: 1 addition & 9 deletions src/betterproto_interop/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use pyo3::{exceptions::PyRuntimeError, PyErr};
use pyo3::PyErr;
use thiserror::Error;

#[derive(Error, Debug)]
Expand All @@ -13,14 +13,6 @@ pub enum InteropError {
UnsupportedWrappedType(String),
#[error("Given object is not a valid betterproto message.")]
IncompleteMetadata,
#[error("Offset-naive datetime {0} is invalid for the current local timezone.")]
OffsetNaiveDateTimeDoesNotMap(chrono::NaiveDateTime),
}

pub type InteropResult<T> = Result<T, InteropError>;

impl From<InteropError> for PyErr {
fn from(value: InteropError) -> Self {
PyRuntimeError::new_err(value.to_string())
}
}
12 changes: 9 additions & 3 deletions src/encode/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ use thiserror::Error;

#[derive(Error, Debug)]
pub enum EncodeError {
#[error("Given object is not a valid betterproto message.")]
NoBetterprotoMessage(#[from] PyErr),
#[error(transparent)]
PythonInteropFailed(#[from] PyErr),
#[error("Given object is not a valid betterproto message.")]
DowncastFailed,
#[error(transparent)]
Interop(#[from] InteropError),
#[error("Given object is not a valid betterproto message.")]
ProstEncode(#[from] prost::EncodeError),
#[error("Offset-naive datetime {0} is invalid for the current local timezone.")]
OffsetNaiveDateTimeDoesNotMap(chrono::NaiveDateTime),
}

pub type EncodeResult<T> = Result<T, EncodeError>;
Expand All @@ -24,6 +26,10 @@ impl From<DowncastError<'_, '_>> for EncodeError {

impl From<EncodeError> for PyErr {
fn from(value: EncodeError) -> Self {
PyRuntimeError::new_err(value.to_string())
if let EncodeError::PythonInteropFailed(pyerr) = value {
pyerr
} else {
PyRuntimeError::new_err(value.to_string())
}
}
}
2 changes: 1 addition & 1 deletion src/encode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod chunk;
mod error;
mod message;

pub use self::{error::EncodeResult, message::MessageEncoder};
pub use self::{error::EncodeError, error::EncodeResult, message::MessageEncoder};
6 changes: 3 additions & 3 deletions src/well_known_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use pyo3::{
};

use crate::{
betterproto_interop::{InteropError, InteropResult},
decode::{DecodeError, DecodeResult},
encode::{EncodeError, EncodeResult},
};

const NANOS_PER_SEC: u32 = 1_000_000_000;
Expand Down Expand Up @@ -200,7 +200,7 @@ impl Duration {
}

impl Timestamp {
fn try_from_any(ob: &Bound<PyAny>) -> InteropResult<Self> {
fn try_from_any(ob: &Bound<PyAny>) -> EncodeResult<Self> {
// try to extract an offset-aware datetime object
if let Ok(dt) = ob.extract::<chrono::DateTime<chrono::FixedOffset>>() {
return Ok(dt.to_utc().into());
Expand All @@ -211,7 +211,7 @@ impl Timestamp {
Ok(dt
.and_local_timezone(chrono::Local)
.single()
.ok_or(InteropError::OffsetNaiveDateTimeDoesNotMap(dt))?
.ok_or(EncodeError::OffsetNaiveDateTimeDoesNotMap(dt))?
.to_utc()
.into())
}
Expand Down

0 comments on commit ff20949

Please sign in to comment.