diff --git a/wallet/src/actors/app/error.rs b/wallet/src/actors/app/error.rs index d358ca209..62786f107 100644 --- a/wallet/src/actors/app/error.rs +++ b/wallet/src/actors/app/error.rs @@ -4,7 +4,7 @@ use serde_json::json; use witnet_net::client::tcp; -use crate::actors; +use crate::{actors, crypto, repository}; #[derive(Debug, Fail)] pub enum Error { @@ -38,7 +38,7 @@ impl Error { Error::WalletNotFound => (402, "Forbidden", None), Error::WalletAlreadyExists(wallet_id) => ( 409, - "Conflict", + "Wallet Conflict", Some(json!({ "cause": self.to_string(), "wallet_id": wallet_id })), ), Error::Node(e) => { @@ -99,6 +99,22 @@ impl From for Error { fn from(err: actors::worker::Error) -> Self { match err { actors::worker::Error::WalletAlreadyExists(e) => Error::WalletAlreadyExists(e), + actors::worker::Error::WrongPassword => { + validation_error(field_error("password", "Wrong password")) + } + actors::worker::Error::WalletNotFound => { + validation_error(field_error("wallet_id", "Wallet not found")) + } + actors::worker::Error::Node(e) => Error::Node(e), + actors::worker::Error::KeyGen(e @ crypto::Error::InvalidKeyPath(_)) => { + validation_error(field_error("seedData", e.to_string())) + } + actors::worker::Error::Repository(_e @ repository::Error::InsufficientBalance) => { + validation_error(field_error( + "balance", + "Wallet account has not enough balance", + )) + } _ => internal_error(err), } } diff --git a/wallet/src/actors/app/methods.rs b/wallet/src/actors/app/methods.rs index c0d797e48..12c770836 100644 --- a/wallet/src/actors/app/methods.rs +++ b/wallet/src/actors/app/methods.rs @@ -9,7 +9,7 @@ use crate::actors::{ worker::{HandleBlockRequest, HandleSuperBlockRequest, NotifyStatus}, *, }; -use crate::{crypto, model, repository}; +use crate::model; use super::*; @@ -261,12 +261,7 @@ impl App { overwrite, )) .flatten() - .map_err(|err| match err { - worker::Error::KeyGen(e @ crypto::Error::InvalidKeyPath(_)) => { - validation_error(field_error("seedData", format!("{}", e))) - } - err => From::from(err), - }); + .map_err(From::from); Box::new(f) } @@ -328,15 +323,7 @@ impl App { .worker .send(worker::UnlockWallet(wallet_id.clone(), password)) .flatten() - .map_err(|err| match err { - worker::Error::WalletNotFound => { - validation_error(field_error("wallet_id", "Wallet not found")) - } - worker::Error::WrongPassword => { - validation_error(field_error("password", "Wrong password")) - } - err => From::from(err), - }) + .map_err(From::from) .into_actor(self) .and_then(move |res, slf: &mut Self, _ctx| { let types::UnlockedSessionWallet { @@ -377,15 +364,7 @@ impl App { .worker .send(worker::CreateVtt(wallet, vtt_params)) .flatten() - .map_err(|err| match err { - worker::Error::Repository(repository::Error::InsufficientBalance) => { - validation_error(field_error( - "balance", - "Wallet account has not enough balance", - )) - } - err => From::from(err), - }) + .map_err(From::from) .into_actor(slf) }); @@ -407,15 +386,7 @@ impl App { .worker .send(worker::CreateDataReq(wallet, params)) .flatten() - .map_err(|err| match err { - worker::Error::Repository(repository::Error::InsufficientBalance) => { - validation_error(field_error( - "balance", - "Wallet account has not enough balance", - )) - } - err => From::from(err), - }) + .map_err(From::from) .into_actor(slf) });