Skip to content

Commit

Permalink
chore: replace Err(e)? with return Err(e.into());
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpolaczyk committed Sep 26, 2019
1 parent 3031132 commit 71f59c5
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 103 deletions.
2 changes: 1 addition & 1 deletion crypto/src/key.rs
Expand Up @@ -67,7 +67,7 @@ where
let seed_len = seed_bytes.len();

if seed_len < 16 || seed_len > 64 {
Err(MasterKeyGenError::InvalidSeedLength)?
return Err(MasterKeyGenError::InvalidSeedLength);
}

let key_bytes = self.key;
Expand Down
4 changes: 2 additions & 2 deletions data_structures/src/builders.rs
Expand Up @@ -91,7 +91,7 @@ impl Message {
) -> Result<Message, failure::Error> {
// Check there are some inventory vectors to be added to the message
if inv_entries.is_empty() {
Err(BuildersError::NoInvVectorsAnnouncement)?
return Err(BuildersError::NoInvVectorsAnnouncement.into());
}

// Build the message
Expand All @@ -110,7 +110,7 @@ impl Message {
) -> Result<Message, failure::Error> {
// Check there are some inventory vectors to be added to the message
if inv_entries.is_empty() {
Err(BuildersError::NoInvVectorsRequest)?
return Err(BuildersError::NoInvVectorsRequest.into());
}

// Build the message
Expand Down
16 changes: 8 additions & 8 deletions data_structures/src/chain.rs
Expand Up @@ -1356,7 +1356,7 @@ impl TryFrom<DataRequestInfo> for DataRequestReport {
block_hash_tally_tx,
})
} else {
Err(DataRequestError::UnfinishedDataRequest)?
Err(DataRequestError::UnfinishedDataRequest.into())
}
}
}
Expand Down Expand Up @@ -1437,11 +1437,11 @@ impl DataRequestState {
) -> Result<(), failure::Error> {
if let DataRequestStage::COMMIT = self.stage {
self.info.commits.insert(pkh, commit);

Ok(())
} else {
Err(DataRequestError::NotCommitStage)?
Err(DataRequestError::NotCommitStage.into())
}

Ok(())
}

/// Add reveal
Expand All @@ -1452,11 +1452,11 @@ impl DataRequestState {
) -> Result<(), failure::Error> {
if let DataRequestStage::REVEAL = self.stage {
self.info.reveals.insert(pkh, reveal);

Ok(())
} else {
Err(DataRequestError::NotRevealStage)?
Err(DataRequestError::NotRevealStage.into())
}

Ok(())
}

/// Add tally and return the data request report
Expand All @@ -1474,7 +1474,7 @@ impl DataRequestState {

Ok(data_request_report)
} else {
Err(DataRequestError::NotTallyStage)?
Err(DataRequestError::NotTallyStage.into())
}
}

Expand Down
17 changes: 8 additions & 9 deletions data_structures/src/data_request.rs
Expand Up @@ -78,7 +78,7 @@ impl DataRequestPool {
) -> Result<(), failure::Error> {
let dr_hash = data_request.hash();
if data_request.signatures.is_empty() {
Err(TransactionError::SignatureNotFound)?
return Err(TransactionError::SignatureNotFound.into());
}

let pkh = data_request.signatures[0].public_key.pkh();
Expand All @@ -104,19 +104,19 @@ impl DataRequestPool {
let tx_hash = commit.hash();
// For a commit output, we need to get the corresponding data request input
let dr_pointer = commit.body.dr_pointer;

// The data request must be from a previous block, and must not be timelocked.
// This is not checked here, as it should have made the block invalid.
if let Some(dr) = self.data_request_pool.get_mut(&dr_pointer) {
dr.add_commit(pkh, commit.clone())?
dr.add_commit(pkh, commit.clone())
} else {
Err(DataRequestError::AddCommitFail {
block_hash: *block_hash,
tx_hash,
dr_pointer,
})?
}
.into())
}

Ok(())
}

/// Add a reveal transaction
Expand All @@ -132,16 +132,15 @@ impl DataRequestPool {
// The data request must be from a previous block, and must not be timelocked.
// This is not checked here, as it should have made the block invalid.
if let Some(dr) = self.data_request_pool.get_mut(&dr_pointer) {
dr.add_reveal(pkh, reveal)?
dr.add_reveal(pkh, reveal)
} else {
Err(DataRequestError::AddRevealFail {
block_hash: *block_hash,
tx_hash,
dr_pointer,
})?
}
.into())
}

Ok(())
}

/// Add a tally transaction
Expand Down
2 changes: 1 addition & 1 deletion data_structures/src/vrf.rs
Expand Up @@ -49,7 +49,7 @@ impl VrfProof {
let vrf_proof = VrfProof { proof, public_key };

if proof_hash.len() != 32 {
Err(HashParseError::InvalidLength(proof_hash.len()))?
Err(HashParseError::InvalidLength(proof_hash.len()).into())
} else {
let mut x = [0; 32];
x.copy_from_slice(&proof_hash);
Expand Down
17 changes: 9 additions & 8 deletions node/src/actors/chain_manager/handlers.rs
Expand Up @@ -240,7 +240,8 @@ impl Handler<GetHighestCheckpointBeacon> for ChainManager {
Ok(chain_info.highest_block_checkpoint)
} else {
log::error!("No ChainInfo loaded in ChainManager");
Err(ChainInfoError::ChainInfoNotFound)?

Err(ChainInfoError::ChainInfoNotFound.into())
}
}
}
Expand Down Expand Up @@ -382,7 +383,7 @@ impl Handler<AddTransaction> for ChainManager {
match self.sm_state {
StateMachine::Synced => {}
_ => {
Err(ChainManagerError::NotSynced)?;
return Err(ChainManagerError::NotSynced.into());
}
};

Expand Down Expand Up @@ -903,12 +904,12 @@ impl Handler<GetReputation> for ChainManager {
_ctx: &mut Self::Context,
) -> Self::Result {
if self.sm_state != StateMachine::Synced {
Err(ChainManagerError::NotSynced)?;
return Err(ChainManagerError::NotSynced.into());
}

let rep_eng = match self.chain_state.reputation_engine.as_ref() {
Some(x) => x,
None => Err(ChainManagerError::ChainNotReady)?,
None => return Err(ChainManagerError::ChainNotReady.into()),
};

Ok((rep_eng.trs.get(&pkh), rep_eng.ars.contains(&pkh)))
Expand All @@ -920,12 +921,12 @@ impl Handler<GetReputationAll> for ChainManager {

fn handle(&mut self, _msg: GetReputationAll, _ctx: &mut Self::Context) -> Self::Result {
if self.sm_state != StateMachine::Synced {
Err(ChainManagerError::NotSynced)?;
return Err(ChainManagerError::NotSynced.into());
}

let rep_eng = match self.chain_state.reputation_engine.as_ref() {
Some(x) => x,
None => Err(ChainManagerError::ChainNotReady)?,
None => return Err(ChainManagerError::ChainNotReady.into()),
};

Ok(rep_eng
Expand All @@ -940,12 +941,12 @@ impl Handler<GetReputationStatus> for ChainManager {

fn handle(&mut self, _msg: GetReputationStatus, _ctx: &mut Self::Context) -> Self::Result {
if self.sm_state != StateMachine::Synced {
Err(ChainManagerError::NotSynced)?;
return Err(ChainManagerError::NotSynced.into());
}

let rep_eng = match self.chain_state.reputation_engine.as_ref() {
Some(x) => x,
None => Err(ChainManagerError::ChainNotReady)?,
None => return Err(ChainManagerError::ChainNotReady.into()),
};

let num_active_identities = rep_eng.ars.active_identities_number() as u32;
Expand Down
2 changes: 1 addition & 1 deletion node/src/actors/chain_manager/mod.rs
Expand Up @@ -274,7 +274,7 @@ impl ChainManager {
Err(e) => Err(e),
}
} else {
Err(ChainManagerError::ChainNotReady)?
Err(ChainManagerError::ChainNotReady.into())
}
}

Expand Down
6 changes: 3 additions & 3 deletions p2p/src/sessions/bounded_sessions.rs
Expand Up @@ -46,11 +46,11 @@ impl<T> BoundedSessions<T> {
.map(|limit| self.collection.len() >= limit as usize)
.unwrap_or(false)
{
Err(SessionsError::MaxPeersReached)?
return Err(SessionsError::MaxPeersReached.into());
}
// Check if address is already in sessions collection
if self.collection.contains_key(&address) {
Err(SessionsError::AddressAlreadyRegistered)?
return Err(SessionsError::AddressAlreadyRegistered.into());
}
// Insert session into the right collection
self.collection.insert(address, SessionInfo { reference });
Expand All @@ -66,7 +66,7 @@ impl<T> BoundedSessions<T> {
// Insert session into the right map (if not present)
match self.collection.remove(&address) {
Some(info) => Ok(info),
None => Err(SessionsError::AddressNotFound)?,
None => Err(SessionsError::AddressNotFound.into()),
}
}
}
4 changes: 2 additions & 2 deletions p2p/src/sessions/mod.rs
Expand Up @@ -101,7 +101,7 @@ where
SessionStatus::Unconsolidated => Ok(&mut self.outbound_unconsolidated),
SessionStatus::Consolidated => Ok(&mut self.outbound_consolidated),
},
_ => Err(SessionsError::NotExpectedFeelerPeer)?,
_ => Err(SessionsError::NotExpectedFeelerPeer.into()),
}
}
/// Method to set the server address
Expand Down Expand Up @@ -273,7 +273,7 @@ where
// Register session into consolidated collection
cons_sessions.register_session(address, session_info)
} else {
Err(SessionsError::NotOutboundConsolidatedPeer)?
Err(SessionsError::NotOutboundConsolidatedPeer.into())
}
}
/// Method to mark a session as consensus unsafe
Expand Down
20 changes: 12 additions & 8 deletions rad/src/operators/array.rs
Expand Up @@ -72,9 +72,11 @@ pub fn filter(input: &RadonArray, args: &[Value]) -> Result<RadonTypes, RadError
result.push(item);
}
}
value => Err(RadError::ArrayFilterWrongSubscript {
value: value.to_string(),
})?,
value => {
return Err(RadError::ArrayFilterWrongSubscript {
value: value.to_string(),
})
}
}
}

Expand All @@ -98,9 +100,9 @@ pub fn sort(input: &RadonArray, args: &[Value]) -> Result<RadonArray, RadError>
}
// Sort not applicable if not homogeneous
if !input.is_homogeneous() {
Err(RadError::UnsupportedOpNonHomogeneous {
return Err(RadError::UnsupportedOpNonHomogeneous {
operator: "ArraySort".to_string(),
})?;
});
}

// Distinguish depending the type
Expand All @@ -117,9 +119,11 @@ pub fn sort(input: &RadonArray, args: &[Value]) -> Result<RadonArray, RadError>
_ => unreachable!(),
});
}
_ => Err(RadError::UnsupportedSortOp {
inner_type: mapped_array_value[0].clone().radon_type_name(),
})?,
_ => {
return Err(RadError::UnsupportedSortOp {
inner_type: mapped_array_value[0].clone().radon_type_name(),
})
}
};

let result: Vec<_> = tuple_array.into_iter().map(|(a, _)| a.clone()).collect();
Expand Down
6 changes: 4 additions & 2 deletions reputation/src/ars.rs
Expand Up @@ -156,7 +156,8 @@ where
Err(ReputationError::InvalidUpdateTime {
new_time,
current_time: self.last_update,
})?
}
.into())
}
}

Expand All @@ -181,7 +182,8 @@ where
Err(ReputationError::InvalidUpdateTime {
new_time,
current_time: self.last_update,
})?
}
.into())
}
}
}
Expand Down

0 comments on commit 71f59c5

Please sign in to comment.