Commit 2484a717 authored by fooker's avatar fooker 👽

More verbose errors

parent a171ba6e
Pipeline #382 passed with stage
in 11 minutes and 1 second
......@@ -51,7 +51,7 @@ impl Repository {
.arg("pull")
.current_dir(&self.0)
.status()
.expect("Failed to git-pull resolution repository");
.expect(&format!("Failed to git-pull resolution repository: {:?}", &self.0));
}
pub fn append(&self, resolution: &Resolution) {
......@@ -59,14 +59,12 @@ impl Repository {
let path = self.0.join("resolutions").join(format!("{}", resolution.date().year()));
debug!("Appending to resolution file: {:?}", path);
let mut file = OpenOptions::new()
.create(true)
.write(true)
.append(true)
.open(&path)
.expect("Failed to open resolution file");
.expect(&format!("Failed to open resolution file: {:?}", &path));
writeln!(file, "[{:08x}] {:04}-{:02}-{:02}: {} ({})",
resolution.id(),
......@@ -75,24 +73,24 @@ impl Repository {
resolution.date().day(),
resolution.text(),
resolution.opinions())
.expect("Failed to append to resolution file");
.expect(&format!("Failed to append to resolution file: {:?}", &path));
Command::new("git")
.arg("add").arg(path)
.arg("add").arg(&path)
.current_dir(&self.0)
.status()
.expect("Failed to git-add resolution repository");
.expect(&format!("Failed to git-add resolution repository: {:?}: {:?}", &self.0, &path));
Command::new("git")
.arg("commit")
.arg("-m").arg(format!("[bot] Added resolution {:08x}: {}", resolution.id(), resolution.text()))
.current_dir(&self.0)
.status()
.expect("Failed to git-commit resolution repository");
.expect(&format!("Failed to git-commit resolution repository: {:?}", &self.0));
Command::new("git")
.arg("push")
.current_dir(&self.0)
.status()
.expect("Failed to git-push resolution repository");
.expect(&format!("Failed to git-push resolution repository: {:?}", &self.0));
}
pub fn search(&self, pattern: &Regex) -> Vec<String> {
......
......@@ -16,13 +16,13 @@ pub struct Bot<M>
#[derive(Clone)]
pub struct Request<'a> {
/// The message text (with prefix removed)
message: &'a str,
pub message: &'a str,
/// The nickname who sent the message
source: &'a str,
pub source: &'a str,
/// The target nickname or channel of the message
target: &'a str,
pub target: &'a str,
}
#[derive(Clone)]
......@@ -63,20 +63,6 @@ pub trait Module where Self: std::marker::Sized {
fn handle(&mut self, request: Request, response: Response) -> Result<(), Error>;
}
impl<'a> Request<'a> {
pub fn message(&self) -> &'a str {
return self.message;
}
pub fn source(&self) -> &'a str {
return self.source;
}
pub fn target(&self) -> &'a str {
return self.target;
}
}
impl<'a> Response<'a> {
pub fn respond_message(&self, message: &str) -> Result<(), Error> {
return self.message(self.respondee, message);
......@@ -111,6 +97,10 @@ impl Client {
return Ok(());
}
pub fn nick(&self) -> &str {
return self.connection.current_nickname();
}
}
impl<M> Bot<M>
......
......@@ -55,18 +55,16 @@ impl<M> Module for M
}
fn handle(&mut self, request: Request, response: Response) -> Result<(), failure::Error> {
let source = request.source();
let (message, direct) = if request.target() == source {
(request.message().trim(), true)
} else if let Some(message) = prefixed(&request.message(), &format!("{}:", source)) {
let (message, direct) = if request.target == response.client.nick() {
(request.message.trim(), true)
} else if let Some(message) = prefixed(&request.message, &format!("{}:", response.client.nick())) {
(message.trim(), false)
} else {
// Message not intended for bot
return Ok(());
};
if let Ok(args) = shellwords::split(request.message()) { // FIXME: Error handling of mismatched quotes
if let Ok(args) = shellwords::split(&request.message) { // FIXME: Error handling of mismatched quotes
let app = App::new("")
.bin_name("FIXME") // FIXME: Use right name and set width to max line length
.setting(AppSettings::ArgRequiredElseHelp)
......@@ -83,8 +81,8 @@ impl<M> Module for M
Ok(matches) => {
let result = self.exec(Command {
message,
source,
target: request.target(),
source: &request.source,
target: &request.target,
direct,
matches,
}, &response);
......@@ -102,7 +100,7 @@ impl<M> Module for M
match err.kind {
// TODO: Show help only for query - send a one-line answer to channels
AppErrorKind::HelpDisplayed => response.message(
request.source(),
&request.source,
&err.message
)?,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment