Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl the standard Error type via thiserror #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ categories = ["database", "network-programming", "asynchronous"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
neo4rs-macros = { version = "0.2.1", path = "../macros" }
futures = { version = "0.3.8" }
tokio = { version = "1.0.1", features = ["full"] }
bytes = "1.0.0"
async-trait = "0.1.42"
deadpool = "0.7.0"
bytes = "1.0.0"
chrono = "0.4.19"
deadpool = "0.7.0"
futures = { version = "0.3.8" }
log = "0.4"
neo4rs-macros = { version = "0.2.1", path = "../macros" }
thiserror = "1.0.26"
tokio = { version = "1.0.1", features = ["full"] }

[dev-dependencies]
uuid = { version = "0.8", features = ["v4"] }
2 changes: 1 addition & 1 deletion lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl ConfigBuilder {
|| self.max_connections.is_none()
|| self.db.is_none()
{
Err(Error::InvalidConfig)
Err(Error::InvalidConfig("a config field was None".into()))
} else {
//The config attributes are validated before unwrapping
Ok(Config {
Expand Down
38 changes: 19 additions & 19 deletions lib/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl TryFrom<BoltType> for f64 {
fn try_from(input: BoltType) -> Result<f64> {
match input {
BoltType::Float(t) => Ok(t.value),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -20,7 +20,7 @@ impl TryFrom<BoltType> for i64 {
fn try_from(input: BoltType) -> Result<i64> {
match input {
BoltType::Integer(t) => Ok(t.value),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -31,7 +31,7 @@ impl TryFrom<BoltType> for bool {
fn try_from(input: BoltType) -> Result<bool> {
match input {
BoltType::Boolean(t) => Ok(t.value),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -42,7 +42,7 @@ impl TryFrom<BoltType> for Point2D {
fn try_from(input: BoltType) -> Result<Point2D> {
match input {
BoltType::Point2D(p) => Ok(Point2D::new(p)),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -53,7 +53,7 @@ impl TryFrom<BoltType> for std::time::Duration {
fn try_from(input: BoltType) -> Result<std::time::Duration> {
match input {
BoltType::Duration(d) => Ok(d.into()),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -64,7 +64,7 @@ impl TryFrom<BoltType> for chrono::NaiveDate {
fn try_from(input: BoltType) -> Result<chrono::NaiveDate> {
match input {
BoltType::Date(d) => d.try_into(),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -75,7 +75,7 @@ impl TryFrom<BoltType> for chrono::DateTime<chrono::FixedOffset> {
fn try_from(input: BoltType) -> Result<chrono::DateTime<chrono::FixedOffset>> {
match input {
BoltType::DateTime(d) => d.try_into(),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -86,7 +86,7 @@ impl TryFrom<BoltType> for chrono::NaiveDateTime {
fn try_from(input: BoltType) -> Result<chrono::NaiveDateTime> {
match input {
BoltType::LocalDateTime(d) => d.try_into(),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -105,7 +105,7 @@ impl TryFrom<BoltType> for (chrono::NaiveTime, Option<chrono::FixedOffset>) {
}
}
BoltType::LocalTime(d) => Ok((d.into(), None)),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -116,7 +116,7 @@ impl TryFrom<BoltType> for (chrono::NaiveDateTime, String) {
fn try_from(input: BoltType) -> Result<(chrono::NaiveDateTime, String)> {
match input {
BoltType::DateTimeZoneId(date_time_zone_id) => date_time_zone_id.try_into(),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -127,7 +127,7 @@ impl TryFrom<BoltType> for Vec<u8> {
fn try_from(input: BoltType) -> Result<Vec<u8>> {
match input {
BoltType::Bytes(b) => Ok(b.value.to_vec()),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -138,7 +138,7 @@ impl TryFrom<BoltType> for Point3D {
fn try_from(input: BoltType) -> Result<Point3D> {
match input {
BoltType::Point3D(p) => Ok(Point3D::new(p)),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -149,7 +149,7 @@ impl TryFrom<BoltType> for Node {
fn try_from(input: BoltType) -> Result<Node> {
match input {
BoltType::Node(n) => Ok(Node::new(n)),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -160,7 +160,7 @@ impl TryFrom<BoltType> for Path {
fn try_from(input: BoltType) -> Result<Path> {
match input {
BoltType::Path(n) => Ok(Path::new(n)),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -171,7 +171,7 @@ impl TryFrom<BoltType> for Relation {
fn try_from(input: BoltType) -> Result<Relation> {
match input {
BoltType::Relation(r) => Ok(Relation::new(r)),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -182,7 +182,7 @@ impl TryFrom<BoltType> for UnboundedRelation {
fn try_from(input: BoltType) -> Result<UnboundedRelation> {
match input {
BoltType::UnboundedRelation(r) => Ok(UnboundedRelation::new(r)),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -192,7 +192,7 @@ impl TryFrom<BoltType> for BoltList {
fn try_from(input: BoltType) -> Result<BoltList> {
match input {
BoltType::List(l) => Ok(l),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -202,7 +202,7 @@ impl TryFrom<BoltType> for BoltString {
fn try_from(input: BoltType) -> Result<BoltString> {
match input {
BoltType::String(s) => Ok(s),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand All @@ -212,7 +212,7 @@ impl TryFrom<BoltType> for String {
fn try_from(input: BoltType) -> Result<String> {
match input {
BoltType::String(t) => Ok(t.value),
_ => Err(Error::ConverstionError),
_ => Err(Error::ConvertError(input)),
}
}
}
Expand Down
81 changes: 57 additions & 24 deletions lib/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,77 @@
use std::{io, string::FromUtf8Error};

use deadpool::managed::PoolError;

use crate::types::{BoltDate, BoltType};

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
IOError { detail: String },
ConnectionError,
#[error("an IO error occurred")]
IoError(#[from] io::Error),

#[error("connection pool error: {0:?}")]
ConnectionError(Box<PoolError<Self>>),

#[error("attempted to serialize excessively long string")]
StringTooLong,

#[error("attempted to serialize excessively large map")]
MapTooBig,

#[error("attempted to serialize excessively large byte array")]
BytesTooBig,

#[error("attempted to serialize excessively long list")]
ListTooLong,
InvalidConfig,
UnsupportedVersion(String),
UnexpectedMessage(String),

#[error("invalid config: {0}")]
InvalidConfig(String),

#[error("version {0} is not supported")]
UnsupportedVersion(u32),

#[error("an unexpected response was received for `{request}`: {response}")]
UnexpectedMessage {
request: String,
response: String
},

#[error("attempted to parse unknown type: `{0}`")]
UnknownType(String),

#[error("received unknown message: {0}")]
UnknownMessage(String),
ConverstionError,

#[error("attempted to convert {0:?} into differing native type")]
ConvertError(BoltType),

#[error("failed to convert `{0:?}` into native type")]
DateConvertError(BoltDate),

#[error("authentication error: {0}")]
AuthenticationError(String),
InvalidTypeMarker(String),
DeserializationError(String),
}

impl std::convert::From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::IOError {
detail: e.to_string(),
}
}
#[error("invalid {type_name} marker: {marker}")]
InvalidTypeMarker {
type_name: &'static str,
marker: u8,
},

#[error("deserialization error")]
DeserializationError(#[from] FromUtf8Error),
}

impl std::convert::From<deadpool::managed::PoolError<Error>> for Error {
fn from(e: deadpool::managed::PoolError<Error>) -> Self {
impl From<PoolError<Error>> for Error {
fn from(e: PoolError<Error>) -> Self {
match e {
deadpool::managed::PoolError::Backend(e) => e,
_ => Error::ConnectionError,
PoolError::Backend(e) => e,
_ => Error::ConnectionError(Box::new(e)),
}
}
}

pub fn unexpected<T: std::fmt::Debug>(response: T, request: &str) -> Error {
Error::UnexpectedMessage(format!(
"unexpected response for {}: {:?}",
request, response
))
Error::UnexpectedMessage { request: request.into(), response: format!("{:?}", response) }
}
2 changes: 1 addition & 1 deletion lib/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl BoltResponse {
input if Record::can_parse(version, input.clone()) => {
Ok(BoltResponse::RecordMessage(Record::parse(version, input)?))
}
msg => Err(Error::UnknownMessage(format!("unknown message {:?}", msg))),
msg => Err(Error::UnknownMessage(format!("{:?}", msg))),
}
}
}
8 changes: 4 additions & 4 deletions lib/src/types/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ impl BoltBytes {
MEDIUM => input.borrow_mut().get_u16() as usize,
LARGE => input.borrow_mut().get_u32() as usize,
_ => {
return Err(Error::InvalidTypeMarker(format!(
"invalid bytes marker {}",
marker
)))
return Err(Error::InvalidTypeMarker {
type_name: "bytes",
marker,
})
}
};

Expand Down
5 changes: 4 additions & 1 deletion lib/src/types/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ impl BoltBoolean {
match value {
TRUE => Ok(BoltBoolean::new(true)),
FALSE => Ok(BoltBoolean::new(false)),
_ => Err(Error::InvalidTypeMarker("invalid boolean marker".into())),
marker => Err(Error::InvalidTypeMarker {
type_name: "boolean",
marker,
}),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/types/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl TryInto<NaiveDate> for BoltDate {
let days = Duration::days(self.days.value);
epoch
.checked_add_signed(days)
.ok_or(Error::ConverstionError)
.ok_or(Error::DateConvertError(self.clone()))
}
}

Expand Down
7 changes: 6 additions & 1 deletion lib/src/types/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ impl BoltInteger {
INT_16 => input.get_i16() as i64,
INT_32 => input.get_i32() as i64,
INT_64 => input.get_i64() as i64,
_ => return Err(Error::InvalidTypeMarker("invalid integer marker".into())),
marker => {
return Err(Error::InvalidTypeMarker {
type_name: "integer",
marker,
})
}
};

Ok(BoltInteger::new(value))
Expand Down
8 changes: 4 additions & 4 deletions lib/src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ impl BoltList {
MEDIUM => input.borrow_mut().get_u16() as usize,
LARGE => input.borrow_mut().get_u32() as usize,
_ => {
return Err(Error::InvalidTypeMarker(format!(
"invalid list marker {}",
marker
)))
return Err(Error::InvalidTypeMarker {
type_name: "list",
marker,
})
}
};

Expand Down
8 changes: 4 additions & 4 deletions lib/src/types/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ impl BoltMap {
MEDIUM => input.borrow_mut().get_u16() as usize,
LARGE => input.borrow_mut().get_u32() as usize,
_ => {
return Err(Error::InvalidTypeMarker(format!(
"invalid map marker {}",
marker
)))
return Err(Error::InvalidTypeMarker {
type_name: "map",
marker,
})
}
};

Expand Down
Loading