Skip to content

Commit

Permalink
refactor: delete error.rs (#226)
Browse files Browse the repository at this point in the history
* refactor: delete error.rs

* fix: BdkError semantics

---------

Co-authored-by: bodymindarts <justin@galoy.io>
  • Loading branch information
thevaibhav-dixit and bodymindarts authored Jun 9, 2023
1 parent ab459fa commit a18ecfb
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 151 deletions.
4 changes: 2 additions & 2 deletions src/account/keys/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl AccountApiKeys {
tx: &mut sqlx::Transaction<'_, Postgres>,
name: String,
account_id: AccountId,
) -> Result<AccountApiKey, BriaError> {
) -> Result<AccountApiKey, AccountError> {
let code = Alphanumeric.sample_string(&mut rand::thread_rng(), 64);
let key = format!("bria_{code}");
let record = sqlx::query!(
Expand All @@ -39,7 +39,7 @@ impl AccountApiKeys {
})
}

pub async fn find_by_key(&self, key: &str) -> Result<AccountApiKey, BriaError> {
pub async fn find_by_key(&self, key: &str) -> Result<AccountApiKey, AccountError> {
let record = sqlx::query!(
r#"SELECT id, account_id, name FROM bria_account_api_keys WHERE encrypted_key = crypt($1, encrypted_key)"#,
key
Expand Down
20 changes: 11 additions & 9 deletions src/admin/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use sqlx_ledger::SqlxLedgerError;
use thiserror::Error;

use crate::{account::error::AccountError, error::*, profile::error::ProfileError};
use crate::{
account::error::AccountError, app::error::ApplicationError, ledger::error::LedgerError,
profile::error::ProfileError,
};

#[allow(clippy::large_enum_variant)]
#[derive(Error, Debug)]
Expand All @@ -10,23 +12,23 @@ pub enum AdminApiError {
TonicError(#[from] tonic::transport::Error),
#[error("AdminApiError - SqlxError: {0}")]
SqlxError(#[from] sqlx::Error),
#[error("AdminApiError - SqlxLedgerError: {0}")]
SqlxLedgerError(#[from] SqlxLedgerError),
#[error("AdminApiError - BriaError: {0}")]
BriaError(BriaError),
BriaError(ApplicationError),
#[error("AdminApiError - BadNetworkForDev")]
BadNetworkForDev,
#[error("{0}")]
AccountError(#[from] AccountError),
#[error("{0}")]
ProfileError(#[from] ProfileError),
#[error("{0}")]
LedgerError(#[from] LedgerError),
}

impl From<BriaError> for AdminApiError {
fn from(err: BriaError) -> Self {
impl From<ApplicationError> for AdminApiError {
fn from(err: ApplicationError) -> Self {
match err {
BriaError::Sqlx(e) => AdminApiError::SqlxError(e),
BriaError::Tonic(e) => AdminApiError::TonicError(e),
ApplicationError::Sqlx(e) => AdminApiError::SqlxError(e),
ApplicationError::ServerError(e) => AdminApiError::TonicError(e),
e => AdminApiError::BriaError(e),
}
}
Expand Down
21 changes: 6 additions & 15 deletions src/api/server/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::{
account::balance::AccountBalanceSummary,
address::*,
app::error::*,
error::BriaError,
outbox::*,
payout::*,
payout_queue::*,
Expand All @@ -19,20 +18,6 @@ use crate::{
xpub::*,
};

impl From<BriaError> for tonic::Status {
fn from(err: BriaError) -> Self {
match err {
BriaError::CouldNotParseIncomingMetadata(err) => {
tonic::Status::invalid_argument(err.to_string())
}
BriaError::CouldNotParseIncomingUuid(err) => {
tonic::Status::invalid_argument(err.to_string())
}
_ => tonic::Status::new(tonic::Code::Unknown, format!("{err}")),
}
}
}

impl From<Profile> for proto::Profile {
fn from(p: Profile) -> Self {
Self {
Expand Down Expand Up @@ -525,6 +510,12 @@ impl From<ApplicationError> for tonic::Status {
ApplicationError::PayoutError(PayoutError::PayoutIdNotFound(_)) => {
tonic::Status::not_found(err.to_string())
}
ApplicationError::CouldNotParseIncomingMetadata(err) => {
tonic::Status::invalid_argument(err.to_string())
}
ApplicationError::CouldNotParseIncomingUuid(err) => {
tonic::Status::invalid_argument(err.to_string())
}
_ => tonic::Status::internal(err.to_string()),
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/api/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use proto::{bria_service_server::BriaService, *};
use super::config::*;
use crate::{
app::{error::ApplicationError, *},
error::*,
payout_queue,
primitives::*,
};
Expand Down Expand Up @@ -280,7 +279,7 @@ impl BriaService for Bria {
metadata
.map(serde_json::to_value)
.transpose()
.map_err(BriaError::CouldNotParseIncomingMetadata)?,
.map_err(ApplicationError::CouldNotParseIncomingMetadata)?,
)
.await?;
Ok(Response::new(NewAddressResponse { address }))
Expand Down Expand Up @@ -313,7 +312,7 @@ impl BriaService for Bria {
new_metadata
.map(serde_json::to_value)
.transpose()
.map_err(BriaError::CouldNotParseIncomingMetadata)?,
.map_err(ApplicationError::CouldNotParseIncomingMetadata)?,
)
.await?;
Ok(Response::new(UpdateAddressResponse {}))
Expand Down Expand Up @@ -496,7 +495,7 @@ impl BriaService for Bria {
metadata
.map(serde_json::to_value)
.transpose()
.map_err(BriaError::CouldNotParseIncomingMetadata)?,
.map_err(ApplicationError::CouldNotParseIncomingMetadata)?,
)
.await?;
Ok(Response::new(SubmitPayoutResponse { id: id.to_string() }))
Expand Down Expand Up @@ -593,7 +592,8 @@ impl BriaService for Bria {
self.app
.update_payout_queue(
profile,
id.parse().map_err(BriaError::CouldNotParseIncomingUuid)?,
id.parse()
.map_err(ApplicationError::CouldNotParseIncomingUuid)?,
new_description,
new_config.map(payout_queue::PayoutQueueConfig::from),
)
Expand All @@ -620,7 +620,7 @@ impl BriaService for Bria {
profile,
batch_id
.parse()
.map_err(BriaError::CouldNotParseIncomingUuid)?,
.map_err(ApplicationError::CouldNotParseIncomingUuid)?,
)
.await?;

Expand Down
4 changes: 4 additions & 0 deletions src/app/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ pub enum ApplicationError {
ServerError(#[from] tonic::transport::Error),
#[error("ApplicationError - UnsupportedPubKeyType")]
UnsupportedPubKeyType,
#[error("ApplicationError - CouldNotParseIncomingMetadata: {0}")]
CouldNotParseIncomingMetadata(serde_json::Error),
#[error("ApplicationError - CouldNotParseIncomingUuid: {0}")]
CouldNotParseIncomingUuid(uuid::Error),
}
2 changes: 2 additions & 0 deletions src/bdk/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ pub enum BdkError {
ElectrumClient(#[from] electrum_client::Error),
#[error("BdkError - Sqlx: {0}")]
Sqlx(#[from] sqlx::Error),
#[error("BdkError - Serde: {0}")]
Serde(#[from] serde_json::Error),
}
6 changes: 3 additions & 3 deletions src/bdk/pg/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use sqlx::{PgPool, Postgres, QueryBuilder, Transaction};
use tracing::instrument;
use uuid::Uuid;

use crate::{bdk::error::BdkError, error::*, primitives::*};
use crate::{bdk::error::BdkError, primitives::*};

#[derive(Debug)]
pub struct UnsyncedTransaction {
Expand Down Expand Up @@ -96,7 +96,7 @@ impl Transactions {
pub async fn find_unsynced_tx(
&self,
excluded_tx_ids: &[String],
) -> Result<Option<UnsyncedTransaction>, BriaError> {
) -> Result<Option<UnsyncedTransaction>, BdkError> {
let rows = sqlx::query!(
r#"WITH tx_to_sync AS (
SELECT tx_id, details_json, height
Expand Down Expand Up @@ -169,7 +169,7 @@ impl Transactions {
&self,
tx: &mut Transaction<'_, Postgres>,
min_height: u32,
) -> Result<Option<ConfirmedSpendTransaction>, BriaError> {
) -> Result<Option<ConfirmedSpendTransaction>, BdkError> {
let rows = sqlx::query!(r#"
WITH tx_to_sync AS (
UPDATE bdk_transactions SET confirmation_synced_to_bria = true, modified_at = NOW()
Expand Down
4 changes: 2 additions & 2 deletions src/bdk/pg/utxos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use sqlx::{PgPool, Postgres, QueryBuilder, Transaction};
use tracing::instrument;
use uuid::Uuid;

use crate::{bdk::error::BdkError, error::*, primitives::*};
use crate::{bdk::error::BdkError, primitives::*};

pub struct ConfirmedIncomeUtxo {
pub outpoint: bitcoin::OutPoint,
Expand Down Expand Up @@ -106,7 +106,7 @@ impl Utxos {
&self,
tx: &mut Transaction<'_, Postgres>,
min_height: u32,
) -> Result<Option<ConfirmedIncomeUtxo>, BriaError> {
) -> Result<Option<ConfirmedIncomeUtxo>, BdkError> {
let row = sqlx::query!(
r#"WITH updated_utxo AS (
UPDATE bdk_utxos SET confirmation_synced_to_bria = true, modified_at = NOW()
Expand Down
97 changes: 0 additions & 97 deletions src/error.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/job/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub enum JobError {
Sqlx(#[from] sqlx::Error),
#[error("JobError - PsbtMissingInSigningSessions")]
PsbtMissingInSigningSessions,
#[error("BriaError - psbt::Error: {0}")]
#[error("JobError - psbt::Error: {0}")]
PsbtError(#[from] psbt::Error),
}

Expand Down
8 changes: 4 additions & 4 deletions src/job/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use tracing::instrument;
use uuid::{uuid, Uuid};

use crate::{
account::*, address::Addresses, app::BlockchainConfig, batch::*, error::*,
fees::MempoolSpaceClient, ledger::Ledger, outbox::*, payout::*, payout_queue::*, primitives::*,
signing_session::*, utxo::Utxos, wallet::*, xpub::*,
account::*, address::Addresses, app::BlockchainConfig, batch::*, fees::MempoolSpaceClient,
ledger::Ledger, outbox::*, payout::*, payout_queue::*, primitives::*, signing_session::*,
utxo::Utxos, wallet::*, xpub::*,
};
use batch_broadcasting::BatchBroadcastingData;
use batch_signing::BatchSigningData;
Expand Down Expand Up @@ -442,7 +442,7 @@ async fn spawn_schedule_process_payout_queue(
pool: &sqlx::PgPool,
data: impl Into<ProcessPayoutQueueData>,
delay: std::time::Duration,
) -> Result<(), BriaError> {
) -> Result<(), JobError> {
let data = data.into();
match JobBuilder::new_with_id(
Uuid::from(data.payout_queue_id),
Expand Down
4 changes: 2 additions & 2 deletions src/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tracing::instrument;

use std::collections::HashMap;

use crate::{account::balance::*, error::*, primitives::*};
use crate::{account::balance::*, primitives::*};
use constants::*;
use error::LedgerError;
pub use event::*;
Expand Down Expand Up @@ -413,7 +413,7 @@ impl Ledger {
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
id: AccountId,
account_name: String,
) -> Result<JournalId, BriaError> {
) -> Result<JournalId, LedgerError> {
let new_journal = NewJournal::builder()
.id(id)
.description(format!("Journal for account '{account_name}'"))
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub mod cli;
pub mod descriptor;
mod dev_constants;
mod entity;
mod error;
pub mod fees;
mod job;
pub mod ledger;
Expand Down
2 changes: 2 additions & 0 deletions src/xpub/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ pub enum XPubError {
XPubParseError(bdk::bitcoin::util::base58::Error),
#[error("XPubError - Bip32: {0}")]
Bip32(#[from] crate::primitives::bitcoin::bip32::Error),
#[error("XPubError - UnsupportedPubKeyType")]
UnsupportedPubKeyType,
}
2 changes: 1 addition & 1 deletion src/xpub/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub enum XPubRef {
}

impl std::str::FromStr for XPubRef {
type Err = crate::error::BriaError;
type Err = super::error::XPubError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Ok(id) = XPubId::from_str(s) {
Expand Down
Loading

0 comments on commit a18ecfb

Please sign in to comment.