From a639aed0bbeb91032c29c0f0ae7b83ab58dad3fd Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 22 Sep 2026 07:41:11 +0000 Subject: [PATCH 1/2] Refactor `stdlib/src/web.rs` to replace unwraps with error handling Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> --- stdlib/src/web.rs | 34 +++++++++++++++++---- stdlib/tests/stdlib_tests.rs | 57 +++++++++++++----------------------- 2 files changed, 48 insertions(+), 43 deletions(-) diff --git a/stdlib/src/web.rs b/stdlib/src/web.rs index 2c2040db..54bbd2c1 100644 --- a/stdlib/src/web.rs +++ b/stdlib/src/web.rs @@ -402,8 +402,19 @@ impl StdlibRegistry { return Ok(RuntimeValue::Str("Server already running".to_string())); } SERVER_RUNNING.store(true, Ordering::SeqCst); - let server = - Mutex::new(tiny_http::Server::http(format!("0.0.0.0:{}", port)).unwrap()); + let server = match tiny_http::Server::http(format!("0.0.0.0:{}", port)) { + Ok(s) => Mutex::new(s), + Err(e) => { + SERVER_RUNNING.store(false, Ordering::SeqCst); + return Err(RuntimeError::new( + techscript_runtime::error::RuntimeErrorKind::InvalidOperation( + format!("Failed to start server: {}", e), + ), + None, + None, + )); + } + }; thread::spawn(move || { while SERVER_RUNNING.load(Ordering::SeqCst) { if let Ok(mut req) = server.lock().unwrap().recv() { @@ -413,7 +424,7 @@ impl StdlibRegistry { &b"Content-Type"[..], &b"text/html; charset=utf-8"[..], ) - .unwrap(), + .expect("valid header"), ); let _ = req.respond(r); } @@ -470,8 +481,19 @@ impl StdlibRegistry { return Ok(RuntimeValue::Str("Server already running".to_string())); } SERVER_RUNNING.store(true, Ordering::SeqCst); - let server = - Mutex::new(tiny_http::Server::http(format!("0.0.0.0:{}", port)).unwrap()); + let server = match tiny_http::Server::http(format!("0.0.0.0:{}", port)) { + Ok(s) => Mutex::new(s), + Err(e) => { + SERVER_RUNNING.store(false, Ordering::SeqCst); + return Err(RuntimeError::new( + techscript_runtime::error::RuntimeErrorKind::InvalidOperation( + format!("Failed to start server: {}", e), + ), + None, + None, + )); + } + }; thread::spawn(move || { while SERVER_RUNNING.load(Ordering::SeqCst) { if let Ok(mut req) = server.lock().unwrap().recv() { @@ -481,7 +503,7 @@ impl StdlibRegistry { &b"Content-Type"[..], &b"text/html; charset=utf-8"[..], ) - .unwrap(), + .expect("valid header"), ); let _ = req.respond(r); } diff --git a/stdlib/tests/stdlib_tests.rs b/stdlib/tests/stdlib_tests.rs index 8fe9cdf7..a9362482 100644 --- a/stdlib/tests/stdlib_tests.rs +++ b/stdlib/tests/stdlib_tests.rs @@ -410,7 +410,6 @@ fn test_regex_operations() { #[test] fn test_web_module() { use std::net::TcpListener; - use std::panic; let registry = StdlibRegistry::new(); let web = registry.get_module("std.web").unwrap(); @@ -433,50 +432,34 @@ fn test_web_module() { let listener = TcpListener::bind("0.0.0.0:0").unwrap(); let port = listener.local_addr().unwrap().port(); - // Test panic on `start` when port is in use - let start_result = panic::catch_unwind(panic::AssertUnwindSafe(|| { - let mut caps = HashSet::new(); - caps.insert(Capability::Network); - let mut ctx2 = RuntimeContext::new(RuntimeConfig { - strict_mode: false, - max_recursion_depth: 1000, - enable_assertions: true, - capabilities: caps, - }); - let _ = start.call( - &mut ctx2, - vec![ - RuntimeValue::Int(port as i64), - RuntimeValue::Str("

Test

".to_string()), - ], - ); - })); + // Test that `start` returns an error when port is in use + let start_result = start.call( + &mut ctx, + vec![ + RuntimeValue::Int(port as i64), + RuntimeValue::Str("

Test

".to_string()), + ], + ); assert!( start_result.is_err(), - "Expected `start` to panic due to port already in use" + "Expected `start` to return an error due to port already in use" + ); + assert!( + start_result.unwrap_err().message.contains("Failed to start server"), + "Expected error message to mention failure to start server" ); - // Make sure we stop the server and cleanup the global bool in case of weirdness, - // although the panic meant it wasn't started fully, but `SERVER_RUNNING` is true. - let _ = stop.call(&mut ctx, vec![]).unwrap(); - - // Test panic on `serve` when port is in use - let serve_result = panic::catch_unwind(panic::AssertUnwindSafe(|| { - let mut caps = HashSet::new(); - caps.insert(Capability::Network); - let mut ctx3 = RuntimeContext::new(RuntimeConfig { - strict_mode: false, - max_recursion_depth: 1000, - enable_assertions: true, - capabilities: caps, - }); - let _ = serve.call(&mut ctx3, vec![RuntimeValue::Int(port as i64)]); - })); + // Test that `serve` returns an error when port is in use + let serve_result = serve.call(&mut ctx, vec![RuntimeValue::Int(port as i64)]); assert!( serve_result.is_err(), - "Expected `serve` to panic due to port already in use" + "Expected `serve` to return an error due to port already in use" + ); + assert!( + serve_result.unwrap_err().message.contains("Failed to start server"), + "Expected error message to mention failure to start server" ); // Reset `SERVER_RUNNING` so other tests aren't affected From 76e2ebce5e9c58fa1a18b06d9564f29461744b7f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 22 Sep 2026 07:44:30 +0000 Subject: [PATCH 2/2] Refactor `stdlib/src/web.rs` to replace unwraps with error handling Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> --- stdlib/tests/stdlib_tests.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/stdlib/tests/stdlib_tests.rs b/stdlib/tests/stdlib_tests.rs index a9362482..0572b5d1 100644 --- a/stdlib/tests/stdlib_tests.rs +++ b/stdlib/tests/stdlib_tests.rs @@ -446,7 +446,10 @@ fn test_web_module() { "Expected `start` to return an error due to port already in use" ); assert!( - start_result.unwrap_err().message.contains("Failed to start server"), + start_result + .unwrap_err() + .message + .contains("Failed to start server"), "Expected error message to mention failure to start server" ); @@ -458,7 +461,10 @@ fn test_web_module() { "Expected `serve` to return an error due to port already in use" ); assert!( - serve_result.unwrap_err().message.contains("Failed to start server"), + serve_result + .unwrap_err() + .message + .contains("Failed to start server"), "Expected error message to mention failure to start server" );