Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use non-static strings errors #16

Merged
merged 2 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/src/crypto/key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl Ed25519PublicKey {
pub fn from_bytes(pk: &[u8]) -> Result<Self> {
let pk_bytes: [u8; 32] = pk
.try_into()
.map_err(|_| Error::TryInto("Failed to convert slice to [u8; 32]"))?;
.map_err(|_| Error::TryInto("Failed to convert slice to [u8; 32]".to_string()))?;

Ok(Self(ed25519_dalek::VerifyingKey::from_bytes(&pk_bytes)?))
}
Expand All @@ -136,7 +136,7 @@ impl PublicKeyExt for Ed25519PublicKey {
fn verify(&self, msg: &[u8], signature: &[u8]) -> Result<()> {
let sig_bytes: [u8; 64] = signature
.try_into()
.map_err(|_| Error::TryInto("Failed to convert slice to [u8; 64]"))?;
.map_err(|_| Error::TryInto("Failed to convert slice to [u8; 64]".to_string()))?;
self.0
.verify(msg, &ed25519_dalek::Signature::from_bytes(&sig_bytes))?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ pub enum Error {
IO(#[from] std::io::Error),

#[error("TryInto Error: {0}")]
TryInto(&'static str),
TryInto(String),

#[error("Timeout Error")]
Timeout,

#[error("Path Not Found Error: {0}")]
PathNotFound(&'static str),
PathNotFound(String),

#[error("Event Emit Error: {0}")]
EventEmitError(String),
Expand Down
2 changes: 1 addition & 1 deletion core/src/util/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{error::Error, Result};
/// Returns the user's home directory as a `PathBuf`.
#[allow(dead_code)]
pub fn home_dir() -> Result<PathBuf> {
dirs::home_dir().ok_or(Error::PathNotFound("Home dir not found"))
dirs::home_dir().ok_or(Error::PathNotFound("Home dir not found".to_string()))
}

/// Expands a tilde (~) in a path and returns the expanded `PathBuf`.
Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/src/client/message_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ impl MessageDispatcher {
let res_id = match res.id {
Some(ref rid) => rid.clone(),
None => {
return Err(Error::InvalidMsg("Response id is none"));
return Err(Error::InvalidMsg("Response id is none".to_string()));
}
};
let id: RequestID = serde_json::from_value(res_id)?;
let val = self.chans.lock().await.remove(&id);
match val {
Some(tx) => tx.send(res).await.map_err(Error::from),
None => Err(Error::InvalidMsg("Receive unknown message")),
None => Err(Error::InvalidMsg("Receive unknown message".to_string())),
}
}
}
8 changes: 4 additions & 4 deletions jsonrpc/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ where

match response.result {
Some(result) => Ok(serde_json::from_value::<V>(result)?),
None => Err(Error::InvalidMsg("Invalid response result")),
None => Err(Error::InvalidMsg("Invalid response result".to_string())),
}
}

Expand All @@ -108,7 +108,7 @@ where

let sub_id = match response.result {
Some(result) => serde_json::from_value::<SubscriptionID>(result)?,
None => return Err(Error::InvalidMsg("Invalid subscription id")),
None => return Err(Error::InvalidMsg("Invalid subscription id".to_string())),
};

let sub = self.subscriptions.subscribe(sub_id).await;
Expand Down Expand Up @@ -172,7 +172,7 @@ where
// It should be OK to unwrap here, as the message dispatcher checks
// for the response id.
if *response.id.as_ref().expect("Get response id") != id {
return Err(Error::InvalidMsg("Invalid response id"));
return Err(Error::InvalidMsg("Invalid response id".to_string()));
}

Ok(response)
Expand Down Expand Up @@ -325,7 +325,7 @@ where
},
Err(err) => {
error!("Receive unexpected msg {msg}: {err}");
Err(Error::InvalidMsg("Unexpected msg"))
Err(Error::InvalidMsg("Unexpected msg".to_string()))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/src/client/subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ impl Subscriptions {
pub(super) async fn notify(&self, nt: Notification) -> Result<()> {
let nt_res: NotificationResult = match nt.params {
Some(ref p) => serde_json::from_value(p.clone())?,
None => return Err(Error::InvalidMsg("Invalid notification msg")),
None => return Err(Error::InvalidMsg("Invalid notification msg".to_string())),
};

match self.subs.lock().await.get(&nt_res.subscription) {
Some(s) => s.notify(nt_res.result.unwrap_or(json!(""))).await?,
None => {
return Err(Error::InvalidMsg("Unknown notification"));
return Err(Error::InvalidMsg("Unknown notification".to_string()));
}
}

Expand Down
10 changes: 5 additions & 5 deletions jsonrpc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub enum Error {
Decode(String),

#[error("Invalid Message Error: {0}")]
InvalidMsg(&'static str),
InvalidMsg(String),

#[error(transparent)]
ParseJSON(#[from] serde_json::Error),
Expand Down Expand Up @@ -58,7 +58,7 @@ pub enum Error {
WebSocket(#[from] async_tungstenite::tungstenite::Error),

#[error("Unexpected Error: {0}")]
General(&'static str),
General(String),

#[error(transparent)]
KaryonCore(#[from] karyon_core::error::Error),
Expand All @@ -79,13 +79,13 @@ pub type RPCResult<T> = std::result::Result<T, RPCError>;
#[derive(ThisError, Debug)]
pub enum RPCError {
#[error("Custom Error: code: {0} msg: {1}")]
CustomError(i32, &'static str),
CustomError(i32, String),

#[error("Invalid Params: {0}")]
InvalidParams(&'static str),
InvalidParams(String),

#[error("Invalid Request: {0}")]
InvalidRequest(&'static str),
InvalidRequest(String),

#[error("Parse Error: {0}")]
ParseError(String),
Expand Down
4 changes: 3 additions & 1 deletion p2p/src/discovery/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ impl LookupService {
NetMsgCmd::Peers => {
peers = decode::<PeersMsg>(&msg.payload)?.0.peers;
if peers.len() >= MAX_PEERS_IN_PEERSMSG {
return Err(Error::Lookup("Received too many peers in PeersMsg"));
return Err(Error::Lookup(
"Received too many peers in PeersMsg".to_string(),
));
}
break;
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ pub enum Error {
InvalidPongMsg,

#[error("Discovery error: {0}")]
Discovery(&'static str),
Discovery(String),

#[error("Lookup error: {0}")]
Lookup(&'static str),
Lookup(String),

#[error("Peer Already Connected")]
PeerAlreadyConnected,
Expand Down
Loading