-- Leo's gemini proxy

-- Connecting to typed-hole.org:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini

commit 7cb7f0944116c0da1f62bd19524e3b77c59bbccd

Author: Julien Blanchard <julien@sideburns.eu>

Date: Wed Jun 26 10:53:46 2019 +0200


Add some error handling


diff --git a/src/main.rs b/src/main.rs

index 71e38e5..d45f12c 100644

--- a/src/main.rs

+++ b/src/main.rs

@@ -56,23 +56,44 @@ fn prompt_for_url(s: &mut Cursive) {

fn visit_url(s: &mut Cursive, url: &str) {

let mut main_view = s.find_id::<TextView>("content").unwrap();

s.pop_layer();

- let new_content = get_data(url);


- main_view.set_content(new_content);

- s.focus_id("content").unwrap();

+ let cleaned_url = str::replace(url, "gemini://", "");

+

+ match get_data(&cleaned_url) {

+ Ok(new_content) => {

+ main_view.set_content(new_content);

+ s.focus_id("content").unwrap();

+ },

+ Err(msg) => {

+ // Show the error in a popup

+ s.add_layer(Dialog::info(msg));

+ }

+ }

+

}


-fn get_data(url: &str) -> String {

+fn get_data(url: &str) -> Result<String, String> {

let mut builder = TlsConnector::builder();

builder.danger_accept_invalid_hostnames(true);

builder.danger_accept_invalid_certs(true);

let connector = builder.build().unwrap();


- let stream = TcpStream::connect(format!("{}:1965", url)).unwrap();

- let mut stream = connector.connect(url, stream).unwrap();

+ let stream = TcpStream::connect(format!("{}:1965", url));

+

+ match stream {

+ Ok(stream) => {

+ let mstream = connector.connect(url, stream);


- stream.write_all(b"/\r\n").unwrap();

- let mut res = vec![];

- stream.read_to_end(&mut res).unwrap();

- String::from_utf8_lossy(&res).to_string()

+ match mstream {

+ Ok(mut stream) => {

+ stream.write_all(b"/\r\n").unwrap();

+ let mut res = vec![];

+ stream.read_to_end(&mut res).unwrap();

+ Ok(String::from_utf8_lossy(&res).to_string())

+ },

+ Err(e) => Err(format!("Could not connect to {}\n{}", url, e))

+ }

+ },

+ Err(e) => Err(format!("Could not connect to {}\n{}", url, e))

+ }

}



---

Served by Pollux Gemini Server.

-- Response ended

-- Page fetched on Sun May 19 09:12:32 2024