Add custom error type
This commit is contained in:
parent
c62bb41cd4
commit
d59ff11799
@ -3,6 +3,8 @@ pub mod rows;
|
||||
pub mod part;
|
||||
pub mod types;
|
||||
|
||||
use std::error::Error;
|
||||
use std::fmt::{Debug, Display, Formatter};
|
||||
use anyhow::Context;
|
||||
use async_trait::async_trait;
|
||||
use sqlx::{PgConnection, PgPool, Postgres};
|
||||
@ -10,6 +12,51 @@ use sqlx::migrate::Migrator;
|
||||
use sqlx::postgres::{PgArguments, PgConnectOptions, PgPoolOptions, PgQueryResult};
|
||||
use sqlx::query::Query;
|
||||
|
||||
pub enum CoveDbError {
|
||||
SqlxError(sqlx::error::Error),
|
||||
AnyhowError(anyhow::Error),
|
||||
}
|
||||
|
||||
impl From<sqlx::Error> for CoveDbError {
|
||||
fn from(err: sqlx::Error) -> CoveDbError {
|
||||
CoveDbError::SqlxError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<anyhow::Error> for CoveDbError {
|
||||
fn from(err: anyhow::Error) -> CoveDbError {
|
||||
CoveDbError::AnyhowError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for CoveDbError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
CoveDbError::SqlxError(err) => {
|
||||
std::fmt::Debug::fmt(&err, f)
|
||||
}
|
||||
CoveDbError::AnyhowError(err) => {
|
||||
std::fmt::Debug::fmt(&err, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for CoveDbError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
CoveDbError::SqlxError(err) => {
|
||||
std::fmt::Display::fmt(&err, f)
|
||||
}
|
||||
CoveDbError::AnyhowError(err) => {
|
||||
std::fmt::Display::fmt(&err, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for CoveDbError {}
|
||||
|
||||
pub struct CoveDB {
|
||||
pg_connect_options: PgConnectOptions,
|
||||
db_pool: PgPool,
|
||||
@ -70,7 +117,7 @@ impl CoveDBImpl for CoveDB {
|
||||
pub trait CoveDBImpl {
|
||||
fn get_pool(&self) -> &PgPool;
|
||||
|
||||
async fn run_query<T: From<PgQueryResult>>(&self, query: Query<'_, Postgres, PgArguments>) -> Result<T, anyhow::Error> {
|
||||
async fn run_query<T: From<PgQueryResult>>(&self, query: Query<'_, Postgres, PgArguments>) -> Result<T, CoveDbError> {
|
||||
let result = query.execute(self.get_pool())
|
||||
.await
|
||||
.context("Failed to execute query")?;
|
||||
|
||||
@ -2,7 +2,7 @@ use async_trait::async_trait;
|
||||
use sqlx::Executor;
|
||||
use sqlx::postgres::PgQueryResult;
|
||||
use cove_net_common::id::SnowflakeID;
|
||||
use crate::{CoveDB, CoveDBImpl};
|
||||
use crate::{CoveDB, CoveDBImpl, CoveDbError};
|
||||
use crate::part::{BindQueryBuilder, SqlPart};
|
||||
use crate::query::{PartialQueryResult, QueryResult};
|
||||
use crate::rows::{DeletableRow, InsertableRow, SelectableRow, TableRow, WhereRow};
|
||||
@ -10,7 +10,8 @@ use crate::rows::user::{PartialUserRow, UserRow};
|
||||
|
||||
#[async_trait]
|
||||
pub trait UserQueries: CoveDBImpl {
|
||||
async fn create_user(&self, user_row: UserRow) -> Result<QueryResult<UserRow>, anyhow::Error> {
|
||||
|
||||
async fn create_user(&self, user_row: UserRow) -> Result<QueryResult<UserRow>, CoveDbError> {
|
||||
let mut query_builder = BindQueryBuilder::new();
|
||||
user_row.insert().encode(&mut query_builder)?;
|
||||
|
||||
@ -22,7 +23,7 @@ pub trait UserQueries: CoveDBImpl {
|
||||
Ok(QueryResult::new(user_row, Some(res)))
|
||||
}
|
||||
|
||||
async fn get_user_by_name_and_discrim(&self, name: impl Into<String> + Send, discriminator: impl Into<String> + Send) -> Result<PartialQueryResult<PartialUserRow>, anyhow::Error> {
|
||||
async fn get_user_by_name_and_discrim(&self, name: impl Into<String> + Send, discriminator: impl Into<String> + Send) -> Result<PartialQueryResult<PartialUserRow>, CoveDbError> {
|
||||
let user_request_row = PartialUserRow {
|
||||
username: Some(name.into()),
|
||||
discriminator: Some(discriminator.into()),
|
||||
@ -41,7 +42,7 @@ pub trait UserQueries: CoveDBImpl {
|
||||
Ok(PartialQueryResult::new(out_partial_row, None))
|
||||
}
|
||||
|
||||
async fn get_user_by_id(&self, user_id: SnowflakeID) -> Result<PartialQueryResult<PartialUserRow>, anyhow::Error> {
|
||||
async fn get_user_by_id(&self, user_id: SnowflakeID) -> Result<PartialQueryResult<PartialUserRow>, CoveDbError> {
|
||||
let user_request_row = PartialUserRow {
|
||||
id: Some(user_id),
|
||||
..Default::default()
|
||||
@ -59,7 +60,7 @@ pub trait UserQueries: CoveDBImpl {
|
||||
Ok(PartialQueryResult::new(out_partial_row, None))
|
||||
}
|
||||
|
||||
async fn delete_user_by_id(&self, user_id: SnowflakeID) -> Result<QueryResult<UserRow>, anyhow::Error> {
|
||||
async fn delete_user_by_id(&self, user_id: SnowflakeID) -> Result<QueryResult<UserRow>, CoveDbError> {
|
||||
let user_delete_row = PartialUserRow {
|
||||
id: Some(user_id),
|
||||
..Default::default()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user