Skip to content

Commit

Permalink
refactor(wallet): move validation errors logic
Browse files Browse the repository at this point in the history
Co-authored-by: Luis Rubio <[email protected]>
  • Loading branch information
mariocao and lrubiorod committed Oct 29, 2020
1 parent ee050f9 commit a378cb4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 36 deletions.
20 changes: 18 additions & 2 deletions wallet/src/actors/app/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -99,6 +99,22 @@ impl From<actors::worker::Error> 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),
}
}
Expand Down
39 changes: 5 additions & 34 deletions wallet/src/actors/app/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::actors::{
worker::{HandleBlockRequest, HandleSuperBlockRequest, NotifyStatus},
*,
};
use crate::{crypto, model, repository};
use crate::model;

use super::*;

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
});

Expand All @@ -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)
});

Expand Down

0 comments on commit a378cb4

Please sign in to comment.