From 576d2c2a083e6ebb924a7a8a688f3b4eb14c76e4 Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Tue, 13 Aug 2024 13:43:40 +0000 Subject: [PATCH 1/2] Clean up compilation error handling --- .../ConfigureCompilationServerDialog.tsx | 18 ++++---- .../CompileContext/CompileContextProvider.tsx | 2 +- .../app/CompileContext/compileStanProgram.ts | 42 +++++++++++-------- gui/src/localStyles.css | 2 +- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/gui/src/app/CompilationServerConnectionControl/ConfigureCompilationServerDialog.tsx b/gui/src/app/CompilationServerConnectionControl/ConfigureCompilationServerDialog.tsx index 01fd7b8..a2dc2c5 100644 --- a/gui/src/app/CompilationServerConnectionControl/ConfigureCompilationServerDialog.tsx +++ b/gui/src/app/CompilationServerConnectionControl/ConfigureCompilationServerDialog.tsx @@ -87,15 +87,15 @@ const ConfigureCompilationServerDialog: FunctionComponent< {serverType === "custom" && (
-

- setStanWasmServerUrl(e.target.value)} - /> -

+ setStanWasmServerUrl(e.target.value)} + /> +
+
)} diff --git a/gui/src/app/CompileContext/CompileContextProvider.tsx b/gui/src/app/CompileContext/CompileContextProvider.tsx index 65f1668..9ac5271 100644 --- a/gui/src/app/CompileContext/CompileContextProvider.tsx +++ b/gui/src/app/CompileContext/CompileContextProvider.tsx @@ -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, diff --git a/gui/src/app/CompileContext/compileStanProgram.ts b/gui/src/app/CompileContext/compileStanProgram.ts index 19ede7e..a5decd0 100644 --- a/gui/src/app/CompileContext/compileStanProgram.ts +++ b/gui/src/app/CompileContext/compileStanProgram.ts @@ -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)}`); @@ -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; diff --git a/gui/src/localStyles.css b/gui/src/localStyles.css index 101deb6..cf6879d 100644 --- a/gui/src/localStyles.css +++ b/gui/src/localStyles.css @@ -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; } From a76954aa00dbd1e744acb6e18b494c156f540a5a Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Tue, 13 Aug 2024 15:10:14 +0000 Subject: [PATCH 2/2] Also warn to console --- .../app/CompileContext/compileStanProgram.ts | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/gui/src/app/CompileContext/compileStanProgram.ts b/gui/src/app/CompileContext/compileStanProgram.ts index a5decd0..ac032c5 100644 --- a/gui/src/app/CompileContext/compileStanProgram.ts +++ b/gui/src/app/CompileContext/compileStanProgram.ts @@ -5,6 +5,11 @@ const compileStanProgram = async ( stanProgram: string, onStatus: (s: string) => void, ): Promise<{ mainJsUrl?: string }> => { + const setStatusAndWarn = (msg: string) => { + onStatus(msg); + console.warn(msg); + }; + try { onStatus("checking cache"); const downloadMainJsUrlFromCache = await checkMainJsUrlCache( @@ -27,13 +32,15 @@ const compileStanProgram = async ( }, }); if (!initiation.ok) { - onStatus(`failed to initiate job: ${await messageOrStatus(initiation)}`); + setStatusAndWarn( + `failed to initiate job: ${await messageOrStatus(initiation)}`, + ); return {}; } const resp = await initiation.json(); const job_id = resp.job_id; if (!job_id) { - onStatus(`failed to initiate job: ${JSON.stringify(resp)}`); + setStatusAndWarn(`failed to initiate job: ${JSON.stringify(resp)}`); return {}; } @@ -47,7 +54,9 @@ const compileStanProgram = async ( body: stanProgram, }); if (!upload.ok) { - onStatus(`failed to upload file: ${await messageOrStatus(upload)}`); + setStatusAndWarn( + `failed to upload file: ${await messageOrStatus(upload)}`, + ); return {}; } onStatus("file uploaded successfully"); @@ -61,7 +70,9 @@ const compileStanProgram = async ( }, }); if (!runCompile.ok) { - onStatus(`failed to compile: ${await messageOrStatus(runCompile)}`); + setStatusAndWarn( + `failed to compile: ${await messageOrStatus(runCompile)}`, + ); return {}; } @@ -73,7 +84,7 @@ const compileStanProgram = async ( onStatus("Checking download of main.js"); const downloadCheck = await fetch(mainJsUrl); if (!downloadCheck.ok) { - onStatus( + setStatusAndWarn( `failed to download main.js: ${await messageOrStatus(downloadCheck)}`, ); return {}; @@ -82,7 +93,7 @@ const compileStanProgram = async ( onStatus("compiled"); return { mainJsUrl }; } catch (e) { - onStatus(`failed to compile: ${e}`); + setStatusAndWarn(`failed to compile: ${e}`); return {}; } };