Skip to content

Commit

Permalink
Clean up compilation error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
WardBrian committed Aug 13, 2024
1 parent 94d867c commit 576d2c2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ const ConfigureCompilationServerDialog: FunctionComponent<

{serverType === "custom" && (
<div>
<p>
<TextField
variant="standard"
label="Custom server URL"
disabled={serverType !== "custom"}
value={stanWasmServerUrl}
onChange={(e) => setStanWasmServerUrl(e.target.value)}
/>
</p>
<TextField
variant="standard"
label="Custom server URL"
disabled={serverType !== "custom"}
value={stanWasmServerUrl}
onChange={(e) => setStanWasmServerUrl(e.target.value)}
/>
<br />
<br />
</div>
)}
</FormControl>
Expand Down
2 changes: 1 addition & 1 deletion gui/src/app/CompileContext/CompileContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ export const CompileContextProvider: FunctionComponent<
onStatus,
);

setTheStanFileContentThasHasBeenCompiled(data.stanFileContent);
if (!mainJsUrl) {
setCompileStatus("failed");
return;
}
setCompiledMainJsUrl(mainJsUrl);
setCompileStatus("compiled");
setTheStanFileContentThasHasBeenCompiled(data.stanFileContent);
}, [
data.stanFileContent,
setCompiledMainJsUrl,
Expand Down
42 changes: 24 additions & 18 deletions gui/src/app/CompileContext/compileStanProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ const compileStanProgram = async (

onStatus("initiating job");
const initiateJobUrl = `${stanWasmServerUrl}/job/initiate`;
// post
const a = await fetch(initiateJobUrl, {

const initiation = await fetch(initiateJobUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer 1234",
},
});
if (!a.ok) {
onStatus(`failed to initiate job: ${a.statusText}`);
if (!initiation.ok) {
onStatus(`failed to initiate job: ${await messageOrStatus(initiation)}`);
return {};
}
const resp = await a.json();
const resp = await initiation.json();
const job_id = resp.job_id;
if (!job_id) {
onStatus(`failed to initiate job: ${JSON.stringify(resp)}`);
Expand All @@ -39,51 +39,57 @@ const compileStanProgram = async (

onStatus(`job initiated: ${job_id}`);
const uploadFileUrl = `${stanWasmServerUrl}/job/${job_id}/upload/main.stan`;
const b = await fetch(uploadFileUrl, {
const upload = await fetch(uploadFileUrl, {
method: "POST",
headers: {
"Content-Type": "text/plain",
},
body: stanProgram,
});
if (!b.ok) {
onStatus(`failed to upload file: ${b.statusText}`);
if (!upload.ok) {
onStatus(`failed to upload file: ${await messageOrStatus(upload)}`);
return {};
}
onStatus("file uploaded successfully");

onStatus("compiling...");
const runJobUrl = `${stanWasmServerUrl}/job/${job_id}/run`;
const c = await fetch(runJobUrl, {
const runCompile = await fetch(runJobUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
});
if (!c.ok) {
const j = await c.json();
onStatus(`failed to run job: ${j?.message ?? c.statusText}`);
if (!runCompile.ok) {
onStatus(`failed to compile: ${await messageOrStatus(runCompile)}`);
return {};
}

const downloadMainJsUrl = `${stanWasmServerUrl}/job/${job_id}/download/main.js`;
const mainJsUrl = `${stanWasmServerUrl}/job/${job_id}/download/main.js`;

setToMainJsUrlCache(stanProgram, downloadMainJsUrl);
setToMainJsUrlCache(stanProgram, mainJsUrl);

// download to make sure it is there
onStatus("Checking download of main.js");
const d = await fetch(downloadMainJsUrl);
if (!d.ok) {
onStatus(`failed to download main.js: ${d.statusText}`);
const downloadCheck = await fetch(mainJsUrl);
if (!downloadCheck.ok) {
onStatus(
`failed to download main.js: ${await messageOrStatus(downloadCheck)}`,
);
return {};
}

onStatus("compiled");
return { mainJsUrl: downloadMainJsUrl };
return { mainJsUrl };
} catch (e) {
onStatus(`failed to compile: ${e}`);
return {};
}
};

const messageOrStatus = async (response: Response) => {
const j = await response.json();
return j?.message ?? response.statusText;
};

export default compileStanProgram;
2 changes: 1 addition & 1 deletion gui/src/localStyles.css
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ span.EditorToolbarItem {
font-weight: normal;
letter-spacing: normal;
display: inline-block;
white-space: normal;
white-space: nowrap;
padding-left: 5px;
font-size: 14px;
}
Expand Down

0 comments on commit 576d2c2

Please sign in to comment.