load errors

pull/6/head
dvkt 5 years ago
parent 0c95554da4
commit 10022a7c30

@ -1,3 +1,4 @@
use std::error::Error;
use std::io;
use std::io::{Read, Write};
use std::net::TcpStream;
@ -37,18 +38,19 @@ impl Page {
}
}
pub fn load(host: &str, port: &str, selector: &str) -> Option<Page> {
pub fn load(host: &str, port: &str, selector: &str) -> io::Result<Page> {
let mut page = Self::new();
if let Some(res) = Self::fetch(host, port, selector) {
page.raw = res;
Some(page)
} else {
None
match Self::fetch(host, port, selector) {
Ok(res) => {
page.raw = res;
Ok(page)
}
Err(e) => Err(e),
}
}
// Fetches a URL and returns a raw Gopher response.
fn fetch(host: &str, port: &str, selector: &str) -> Option<String> {
fn fetch(host: &str, port: &str, selector: &str) -> io::Result<String> {
let mut body = String::new();
let stream = TcpStream::connect(format!("{}:{}", host, port))
.and_then(|mut stream| {
@ -60,10 +62,9 @@ impl Page {
Ok(())
});
if let Ok(()) = stream {
Some(body)
} else {
None
match stream {
Ok(_) => Ok(body),
Err(e) => Err(e),
}
}
}

@ -15,11 +15,12 @@ impl UI {
}
pub fn load(&mut self, host: &str, port: &str, selector: &str) {
if let Some(page) = Page::load(host, port, selector) {
self.pages.push(page);
} else {
eprintln!("error loading {}:{}{}", host, port, selector);
std::process::exit(1);
match Page::load(host, port, selector) {
Ok(page) => self.pages.push(page),
Err(e) => {
eprintln!("error loading {}:{}{}: {}", host, port, selector, e);
std::process::exit(1);
}
}
}

Loading…
Cancel
Save