From a5cd13771b2f4b122abfc0b3629eecb1f7f38685 Mon Sep 17 00:00:00 2001 From: Gwen Lg Date: Thu, 13 Jun 2024 22:16:45 +0200 Subject: [PATCH] add err output when server failed to start --- src/server.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/server.rs b/src/server.rs index 6d4dc7a..4156649 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,7 +1,7 @@ use crate::server::utils::{get_free_port, wait_for_port}; use mktemp::Temp; use std::error::Error; -use std::io::{Cursor, Read}; +use std::io::{self, Cursor, Read}; use std::process::{Child, Stdio}; use zip::ZipArchive; @@ -53,7 +53,7 @@ impl EmbeddedServer { .arg("-p") .arg(port.to_string()) .stdout(Stdio::null()) - .stderr(Stdio::null()) + .stderr(Stdio::piped()) .spawn()?; wait_for_port(&mut child, port)?; @@ -78,7 +78,8 @@ impl Drop for EmbeddedServer { } mod utils { - use std::io::ErrorKind; + use std::fmt::Write; + use std::io::{BufRead, BufReader, ErrorKind}; use std::process::Child; use std::time::Duration; @@ -115,10 +116,24 @@ mod utils { fn check_server(child: &mut Child) -> Result<(), std::io::Error> { match child.try_wait() { Ok(None) => Ok(()), // Continue - Ok(Some(status)) => Err(std::io::Error::new( - ErrorKind::Other, - format!("grammalecte-server exit with {status}"), - )), + Ok(Some(status)) => { + let mut err_msg = format!("grammalecte-server exit with `{status}`"); + + if let Some(err) = child.stderr.take() { + writeln!(&mut err_msg, " :").unwrap(); + let err = BufReader::new(err); + err.lines().for_each(|line| match line { + Ok(line) => { + writeln!(&mut err_msg, "\t{}", line).unwrap(); + } + Err(err) => { + writeln!(&mut err_msg, "__{err:?}").unwrap(); + } + }); + }; + + Err(std::io::Error::new(ErrorKind::Other, err_msg)) + } Err(err) => Err(std::io::Error::new( ErrorKind::Other, format!("Error append during check grammalecte-server status {err}"),