Skip to content

Commit

Permalink
better io errors
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Aug 29, 2024
1 parent d046101 commit 542d61d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
25 changes: 22 additions & 3 deletions src/server/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ use async_tempfile::TempFile;
use tokio::fs::read;
use tokio::io::AsyncWriteExt;
use tokio::process::Command;
use tracing::instrument;

#[instrument(skip(data))]
pub async fn pack(map: &str, data: &[u8]) -> Result<Vec<u8>> {
let mut input = TempFile::new_with_name(map.to_string()).await?;
let output = TempFile::new_with_name(format!("out_{map}")).await?;

input.write_all(data).await?;
input
.write_all(data)
.await
.map_err(|err| ServerError::File {
path: input.file_path().into(),
err,
})?;

let pack_cmd = option_env!("GLTFPACK").unwrap_or("gltfpack");

Expand All @@ -21,13 +29,24 @@ pub async fn pack(map: &str, data: &[u8]) -> Result<Vec<u8>> {
.arg("-o")
.arg(output.file_path())
.output()
.await?;
.await
.map_err(|err| ServerError::Pack {
err,
input: input.file_path().into(),
output: output.file_path().into(),
binary: pack_cmd.into(),
})?;

if !out.status.success() {
return Err(ServerError::GltfPack(
String::from_utf8_lossy(&out.stderr).into(),
));
}

Ok(read(output.file_path()).await?)
read(output.file_path())
.await
.map_err(|err| ServerError::File {
path: output.file_path().into(),
err,
})
}
30 changes: 23 additions & 7 deletions src/server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ pub enum ServerError {
Toml(#[from] toml::de::Error),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("IO error {err:#} on {}", path.display())]
File { err: std::io::Error, path: PathBuf },
#[error("Error {err:#} while packing {} to {} with {}", input.display(), output.display(), binary)]
Pack {
err: std::io::Error,
input: PathBuf,
output: PathBuf,
binary: String,
},
#[error(transparent)]
Loader(#[from] LoaderError),
#[error(transparent)]
Expand Down Expand Up @@ -111,7 +120,10 @@ fn setup() -> Result<Config> {
.init();

let args = Args::parse();
let toml = read_to_string(&args.config)?;
let toml = read_to_string(&args.config).map_err(|err| ServerError::File {
path: args.config.into(),
err,
})?;
Ok(from_str(&toml)?)
}

Expand All @@ -137,10 +149,8 @@ async fn main() -> Result<()> {
.with_state(Arc::new(app));

// Run our app with hyper
let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, config.port))
.await
.unwrap();
tracing::info!("listening on {}", listener.local_addr()?);
let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, config.port)).await?;
info!("listening on {}", listener.local_addr()?);
let serve = async {
if let Err(e) = axum::serve(listener, app).await {
eprintln!("{e:#?}");
Expand Down Expand Up @@ -170,15 +180,21 @@ impl App {
fn cached(&self, map: &str, options_key: u64) -> Result<Option<Vec<u8>>> {
let path = self.cache_path(map, options_key);
if path.exists() {
Ok(Some(read(path)?))
Ok(Some(read(&path).map_err(|err| ServerError::File {
path: path.clone(),
err,
})?))
} else {
Ok(None)
}
}

fn cache(&self, map: &str, data: &[u8], options_key: u64) -> Result<()> {
let path = self.cache_path(map, options_key);
Ok(write(path, data)?)
Ok(write(&path, data).map_err(|err| ServerError::File {
path: path.clone(),
err,
})?)
}

async fn download(&self, map: &str) -> Result<Vec<u8>> {
Expand Down

0 comments on commit 542d61d

Please sign in to comment.