Skip to content

Commit e565912

Browse files
committed
Merge branch 'aleksanderkrauze-feature/implement-std-error-for-birdc-error'. See PR #1
2 parents 7c65b03 + 6028be1 commit e565912

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/connection.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl Connection {
137137
// process only if we find the first entry to be a 1001
138138
if let Some(msg_1001) = Interface::from_enum(first_msg) {
139139
// get the position of the next 1001
140-
let next_1001_idx = (&messages[idx..])
140+
let next_1001_idx = messages[idx..]
141141
.iter()
142142
.position(|x| matches!(x, Message::InterfaceList(_)))
143143
.unwrap_or(messages.len() - idx)
@@ -619,7 +619,7 @@ fn parse_message(code: u32, buffer: &[u8], start_pos: usize, msg_size: usize) ->
619619
}
620620
idx += 1;
621621

622-
if let Some(nl_pos) = (&buffer[pos..]).iter().position(|it| *it == b'\n') {
622+
if let Some(nl_pos) = buffer[pos..].iter().position(|it| *it == b'\n') {
623623
let src = &buffer[pos..(pos + nl_pos)];
624624
v.extend_from_slice(src);
625625
pos += src.len() + 1;

src/error.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{num::ParseIntError, str::Utf8Error};
1+
use std::{fmt, num::ParseIntError, str::Utf8Error};
22

33
use super::Message;
44

@@ -22,6 +22,43 @@ pub enum Error {
2222
ParseError(Vec<Message>),
2323
}
2424

25+
impl fmt::Display for Error {
26+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27+
match self {
28+
Error::IoError(_) => write!(f, "IO operation failed"),
29+
Error::ProtocolError(msg) => {
30+
write!(f, "received an error message from server")?;
31+
match msg {
32+
Message::ReplyTooLong(_) => write!(f, ": reply too long"),
33+
Message::RouteNotFound(_) => write!(f, ": route not found"),
34+
Message::ConfigurationFileError(_) => write!(f, ": configuration file error"),
35+
Message::NoProtocolsMatch(_) => write!(f, ": no protocols match"),
36+
Message::StoppedDueToReconfiguration(_) => {
37+
write!(f, ": stopped due to reconfiguration")
38+
}
39+
Message::ProtocolDown(_) => write!(f, ": protocol is down => connot dump"),
40+
Message::ReloadFailed(_) => write!(f, ": reload failed"),
41+
Message::AccessDenied(_) => write!(f, ": access denied"),
42+
Message::RuntimeError(..) => write!(f, ": evaluation runtime error"),
43+
_ => Ok(()),
44+
}
45+
}
46+
Error::OperationInProgress => write!(f, "another request is already in progress"),
47+
Error::InvalidToken(_) => write!(f, "received invalid token"),
48+
Error::ParseError(_) => write!(f, "failed to parse server response"),
49+
}
50+
}
51+
}
52+
53+
impl std::error::Error for Error {
54+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
55+
match self {
56+
Self::IoError(err) => Some(err),
57+
_ => None,
58+
}
59+
}
60+
}
61+
2562
impl Error {
2663
pub fn eof(err: &str) -> Self {
2764
Self::IoError(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, err))

0 commit comments

Comments
 (0)