Skip to content

Commit

Permalink
add logging, improved error handling, make init JS more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
ranile committed Sep 1, 2023
1 parent ec03c4b commit 08a2a94
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 124 deletions.
181 changes: 66 additions & 115 deletions app/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 29 additions & 9 deletions services/backend/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::net::SocketAddr;

use anyhow::Error;
use anyhow::{anyhow, Error};
use axum::extract::Query;
use axum::response::Html;
use axum::routing::get;
Expand All @@ -11,7 +11,7 @@ use reqwest::Client;
use response::Bson;
use serde::{Deserialize, Serialize};
use tower_http::trace::TraceLayer;
use tracing::info;
use tracing::{debug, error, info};

use common::response;
use common::{errors, init_tracing};
Expand Down Expand Up @@ -66,22 +66,42 @@ async fn run(Query(body): Query<RunPayload>) -> Result<Html<String>, ApiError> {
.await
.map_err(Error::from)?;

debug!(status = ?res.status(), "got response from compiler");

let run_response: common::Response = {
let bytes = res.bytes().await.unwrap();
bson::from_slice(&bytes).unwrap()
let bytes = res.bytes().await.map_err(|e| {
error!(?e, "failed to get bytes from compiler response");
ApiError::Unknown(e.into())
})?;
bson::from_slice(&bytes).map_err(|e| {
error!(?e, "failed to deserialize compiler response");
ApiError::BsonDeserializeError(e)
})?
};


match run_response {
common::Response::Output {
index_html: _,
js,
wasm,
} => {
let index_html = INDEX_HTML.replace("/*JS_GOES_HERE*/", &js);
println!("{}", index_html);
let init = format!("init((new Int8Array({:?})).buffer)", wasm);
let index_html = index_html.replace("/*INIT_GOES_HERE*/", &init);
Ok(Html(index_html))
debug!(wasm_bytes = wasm.len(), "compilation successful");
let init_fn = js.split("export default").nth(1).and_then(|it| it.trim().strip_suffix(";"));
match init_fn {
Some(init_fn) => {
let index_html = INDEX_HTML.replace("/*JS_GOES_HERE*/", &js);
let init = format!("{}((new Int8Array({:?})).buffer)", init_fn, wasm);
let index_html = index_html.replace("/*INIT_GOES_HERE*/", &init);

tokio::fs::write("/home/hamza/code/yew-playground/app/dist/tmp.html", &index_html).await.unwrap();

Ok(Html(index_html))
}
None => {
return Err(ApiError::Unknown(anyhow!("failed to find init function as default export in js")))
}
}
}
common::Response::CompileError(e) => Ok(Html(e)),
}
Expand Down
3 changes: 3 additions & 0 deletions services/common/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub enum ApiError {
BuildFailed(Output),
#[error(transparent)]
Unknown(#[from] anyhow::Error),
#[error("failed to deserialize bson: {0}")]
BsonDeserializeError(#[from] bson::de::Error),
}

impl IntoResponse for ApiError {
Expand All @@ -27,6 +29,7 @@ impl IntoResponse for ApiError {
ApiError::NoBody => StatusCode::BAD_REQUEST,
ApiError::BuildFailed(_) => StatusCode::BAD_REQUEST,
ApiError::Unknown(_) => StatusCode::INTERNAL_SERVER_ERROR,
ApiError::BsonDeserializeError(_) => StatusCode::INTERNAL_SERVER_ERROR,
};
Response::builder()
.status(status)
Expand Down

0 comments on commit 08a2a94

Please sign in to comment.