core: small internal refactor

This commit is contained in:
anna 2022-12-11 20:53:57 +01:00
parent ead1a4fc62
commit 1947632ecd
Signed by: fef
GPG key ID: EC22E476DC2D3D84
2 changed files with 14 additions and 13 deletions

View file

@ -29,13 +29,9 @@ pub fn utc_now() -> NaiveDateTime {
impl ResponseError for Error {
fn status_code(&self) -> StatusCode {
match self {
Error::Database(sqlx_error) => match sqlx_error {
sqlx::Error::RowNotFound => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR,
},
Error::Io(_) => StatusCode::INTERNAL_SERVER_ERROR,
Error::NotFound => StatusCode::NOT_FOUND,
Error::Invalid(_) => StatusCode::UNPROCESSABLE_ENTITY,
_ => StatusCode::INTERNAL_SERVER_ERROR,
}
}
@ -57,7 +53,10 @@ impl fmt::Display for Error {
impl From<sqlx::Error> for Error {
fn from(e: sqlx::Error) -> Error {
Error::Database(e)
match e {
sqlx::Error::RowNotFound => Error::NotFound,
_ => Error::Database(e),
}
}
}
@ -67,6 +66,12 @@ impl From<io::Error> for Error {
}
}
impl From<validate::Error> for Error {
fn from(e: validate::Error) -> Error {
Error::Invalid(e)
}
}
impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where

View file

@ -56,7 +56,9 @@ where
type Error = crate::core::Error;
fn try_from(t: Insane<T>) -> Result<Sane<T>> {
Ok(Sane((&t.0).validate(ResultBuilder::new()).result(t.inner())?))
Ok(Sane(
(&t.0).validate(ResultBuilder::new()).result(t.inner())?,
))
}
}
@ -126,12 +128,6 @@ impl<'a, T> FieldResultBuilder<'a, T> {
}
}
impl From<Error> for crate::core::Error {
fn from(e: Error) -> crate::core::Error {
crate::core::Error::Invalid(e)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (field, msg) in &self.errors {