Skip to content

Commit

Permalink
Move ConversionError from restate_common to restate_storage_api (
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper authored Jun 27, 2023
1 parent a635086 commit 66599dd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 30 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 0 additions & 20 deletions src/common/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
use crate::utils::GenericError;
use std::borrow::Cow;
use std::convert::Into;
use std::fmt;
use std::fmt::Formatter;

/// Error type for conversion related problems (e.g. Rust <-> Protobuf)
#[derive(Debug, thiserror::Error)]
pub enum ConversionError {
#[error("missing field '{0}'")]
MissingField(&'static str),
#[error("invalid data: {0}")]
InvalidData(GenericError),
}

impl ConversionError {
pub fn invalid_data(source: impl Into<GenericError>) -> Self {
ConversionError::InvalidData(source.into())
}

pub fn missing_field(field: &'static str) -> Self {
ConversionError::MissingField(field)
}
}

/// This error code set matches the [gRPC error code set](https://github.com/grpc/grpc/blob/master/doc/statuscodes.md#status-codes-and-their-use-in-grpc),
/// representing all the error codes visible to the user code.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand Down
10 changes: 3 additions & 7 deletions src/storage_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use futures_util::future::BoxFuture;
use futures_util::stream::BoxStream;
use restate_common::errors::ConversionError;
pub use restate_common::utils::GenericError;

//
// A Generic storage Error
//
use restate_common::utils::GenericError;

/// Storage error
#[derive(Debug, thiserror::Error)]
pub enum StorageError {
#[error("generic storage error: {0}")]
Generic(#[from] GenericError),
#[error("failed to convert Rust objects to/from protobuf: {0}")]
Conversion(#[from] ConversionError),
Conversion(GenericError),
#[error("Integrity constrained is violated")]
DataIntegrityError,
}
Expand Down
4 changes: 3 additions & 1 deletion src/storage_proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ publish = false

[features]
default = []
conversion = ["dep:restate_common", "dep:bytes", "dep:bytestring", "dep:opentelemetry_api"]
conversion = ["dep:restate_common", "dep:restate_storage_api", "dep:thiserror", "dep:bytes", "dep:bytestring", "dep:opentelemetry_api"]

[dependencies]
bytes = { workspace = true, optional = true }
bytestring = { workspace = true, optional = true }
restate_common = { workspace = true, optional = true }
restate_storage_api = { workspace = true, optional = true }
opentelemetry_api = { workspace = true, optional = true }
prost = { workspace = true }
prost-types = { workspace = true }
thiserror = { workspace = true, optional = true }

[build-dependencies]
prost-build = { workspace = true }
28 changes: 27 additions & 1 deletion src/storage_proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,38 @@ pub mod storage {
use bytes::{Buf, Bytes};
use bytestring::ByteString;
use opentelemetry_api::trace::TraceState;
use restate_common::errors::ConversionError;
use restate_common::types::MillisSinceEpoch;
use restate_common::utils::GenericError;
use restate_storage_api::StorageError;
use std::collections::{HashSet, VecDeque};
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::str::FromStr;

/// Error type for conversion related problems (e.g. Rust <-> Protobuf)
#[derive(Debug, thiserror::Error)]
pub enum ConversionError {
#[error("missing field '{0}'")]
MissingField(&'static str),
#[error("invalid data: {0}")]
InvalidData(GenericError),
}

impl ConversionError {
pub fn invalid_data(source: impl Into<GenericError>) -> Self {
ConversionError::InvalidData(source.into())
}

pub fn missing_field(field: &'static str) -> Self {
ConversionError::MissingField(field)
}
}

impl From<ConversionError> for StorageError {
fn from(value: ConversionError) -> Self {
StorageError::Conversion(value.into())
}
}

impl TryFrom<InvocationStatus> for restate_common::types::InvocationStatus {
type Error = ConversionError;

Expand Down
3 changes: 2 additions & 1 deletion src/storage_rocksdb/src/outbox_table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ fn decode_key_value(k: &[u8], v: &[u8]) -> crate::Result<(u64, OutboxMessage)> {
// decode value
let decoded = storage::v1::OutboxMessage::decode(v)
.map_err(|error| StorageError::Generic(error.into()))?;
let outbox_message = OutboxMessage::try_from(decoded).map_err(StorageError::Conversion)?;
let outbox_message =
OutboxMessage::try_from(decoded).map_err(|e| StorageError::Conversion(e.into()))?;

Ok((sequence_number, outbox_message))
}

0 comments on commit 66599dd

Please sign in to comment.