Skip to content

Commit

Permalink
Merge branch 'aleksanderkrauze-feature/implement-std-error-for-birdc-…
Browse files Browse the repository at this point in the history
…error'. See PR #1
  • Loading branch information
amodm committed Jun 12, 2023
2 parents 7c65b03 + 6028be1 commit e565912
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl Connection {
// process only if we find the first entry to be a 1001
if let Some(msg_1001) = Interface::from_enum(first_msg) {
// get the position of the next 1001
let next_1001_idx = (&messages[idx..])
let next_1001_idx = messages[idx..]
.iter()
.position(|x| matches!(x, Message::InterfaceList(_)))
.unwrap_or(messages.len() - idx)
Expand Down Expand Up @@ -619,7 +619,7 @@ fn parse_message(code: u32, buffer: &[u8], start_pos: usize, msg_size: usize) ->
}
idx += 1;

if let Some(nl_pos) = (&buffer[pos..]).iter().position(|it| *it == b'\n') {
if let Some(nl_pos) = buffer[pos..].iter().position(|it| *it == b'\n') {
let src = &buffer[pos..(pos + nl_pos)];
v.extend_from_slice(src);
pos += src.len() + 1;
Expand Down
39 changes: 38 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{num::ParseIntError, str::Utf8Error};
use std::{fmt, num::ParseIntError, str::Utf8Error};

use super::Message;

Expand All @@ -22,6 +22,43 @@ pub enum Error {
ParseError(Vec<Message>),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::IoError(_) => write!(f, "IO operation failed"),
Error::ProtocolError(msg) => {
write!(f, "received an error message from server")?;
match msg {
Message::ReplyTooLong(_) => write!(f, ": reply too long"),
Message::RouteNotFound(_) => write!(f, ": route not found"),
Message::ConfigurationFileError(_) => write!(f, ": configuration file error"),
Message::NoProtocolsMatch(_) => write!(f, ": no protocols match"),
Message::StoppedDueToReconfiguration(_) => {
write!(f, ": stopped due to reconfiguration")
}
Message::ProtocolDown(_) => write!(f, ": protocol is down => connot dump"),
Message::ReloadFailed(_) => write!(f, ": reload failed"),
Message::AccessDenied(_) => write!(f, ": access denied"),
Message::RuntimeError(..) => write!(f, ": evaluation runtime error"),
_ => Ok(()),
}
}
Error::OperationInProgress => write!(f, "another request is already in progress"),
Error::InvalidToken(_) => write!(f, "received invalid token"),
Error::ParseError(_) => write!(f, "failed to parse server response"),
}
}
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::IoError(err) => Some(err),
_ => None,
}
}
}

impl Error {
pub fn eof(err: &str) -> Self {
Self::IoError(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, err))
Expand Down

0 comments on commit e565912

Please sign in to comment.