Skip to content

Commit

Permalink
failure logging in app-compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
ranile committed Sep 5, 2023
1 parent b331d7e commit e28b4f6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
46 changes: 39 additions & 7 deletions services/app-compiler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use tokio::process::Command;
use tower::limit::GlobalConcurrencyLimitLayer;
use tower::ServiceBuilder;
use tower_http::trace::TraceLayer;
use tracing::{debug, info};
use tracing::{debug, info, error};

use common::errors::{timeout_or_500, ApiError};
use common::init_tracing;
Expand Down Expand Up @@ -46,25 +46,57 @@ async fn run(RawBody(body): RawBody) -> Result<Bson<Response>, ApiError> {
return Err(ApiError::NoBody);
}
let body = String::from_utf8_lossy(&body);
let app_dir = fs::canonicalize(&*APP_DIR).await?;
fs::write(app_dir.join("src/main.rs"), &*body).await?;
let app_dir = match fs::canonicalize(&*APP_DIR).await {
Ok(v) => v,
Err(e) => {
error!(?e, "failed to canonicalize app_dir path");
return Err(ApiError::IoError(e))
},
};

match fs::write(app_dir.join("src/main.rs"), &*body).await {
Ok(_) => {},
Err(e) => {
error!(?e, "failed to write main.rs");
return Err(ApiError::IoError(e))
},
};

let mut cmd = Command::new(&*TRUNK_BIN);
let cmd = cmd
.arg("--config")
.arg(app_dir.join("Trunk.toml"))
.arg("build");
debug!(?cmd, "running command");
let output = cmd.output().await?;

let output = match cmd.output().await {
Ok(o) => o,
Err(e) => {
error!(?e, "running trunk failed");
return Err(ApiError::IoError(e))
},
};

if !output.status.success() {
return Ok(Bson(Response::CompileError(
String::from_utf8_lossy(&output.stderr).to_string(),
)));
}

let dist = app_dir.join("dist");
let index_html = fs::read_to_string(dist.join("index.html")).await?;
let js = fs::read_to_string(dist.join("app.js")).await?;
let wasm = fs::read(dist.join("app_bg.wasm")).await?;
let index_html = fs::read_to_string(dist.join("index.html")).await.map_err(|e| {
error!(?e, "failed to read index.html");
ApiError::IoError(e)
})?;
let js = fs::read_to_string(dist.join("app.js")).await.map_err(|e| {
error!(?e, "failed to read app.js");
ApiError::IoError(e)
})?;
let wasm = fs::read(dist.join("app_bg.wasm")).await.map_err(|e| {
error!(?e, "failed to read app_bg.wasm");
ApiError::IoError(e)
})?;


Ok(Bson(Response::Output {
index_html,
Expand Down
2 changes: 1 addition & 1 deletion services/common/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use axum::{body, BoxError};
#[derive(Debug, thiserror::Error)]
pub enum ApiError {
#[error("{0}")]
IoError(#[from] std::io::Error),
IoError(std::io::Error),
#[error("{0} should be present after trunk build but is not")]
BuildFileNotFound(&'static str),
#[error("request must have a body but none was found")]
Expand Down

0 comments on commit e28b4f6

Please sign in to comment.