From 59d83d7857ad57f9e25d6cbf76349c956560bf51 Mon Sep 17 00:00:00 2001 From: dab Date: Fri, 15 Aug 2025 14:39:24 +0000 Subject: [PATCH] better error handling (now wrapping user-defined functions with try/catch), better frontend logging (debug replaces info) --- backend/index.js | 44 +++++++++++++++++++++++++++++++------------- frontend/index.js | 12 ++++++------ 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/backend/index.js b/backend/index.js index 8cedaa6..a48cfac 100644 --- a/backend/index.js +++ b/backend/index.js @@ -91,18 +91,31 @@ const start_server = function ({ context_meta, config, jsbuild_app_frontend, han resp = new Response(get_siteroot_html({ page_title: config.page_title }), { status: 200, headers: { 'Content-Type': 'text/html' } }) } else if (url.pathname === '/index.js') { - const str_js = await jsbuild_app_frontend() - resp = new Response( - await async_build_js_script(import.meta.dir + '/../frontend/index.js', { - define: { - JS_APP_FRONTEND: JSON.stringify(str_js), + let str_js + // wrap provided function in case it throws error + try { + str_js = await jsbuild_app_frontend() + } + catch (err) { + console.error(['error during JS build', err]) + } + if (str_js !== undefined) { + resp = new Response( + await async_build_js_script(import.meta.dir + '/../frontend/index.js', { + define: { + JS_APP_FRONTEND: JSON.stringify(str_js), + } + }), + { + status: 200, + headers: { 'Content-Type': 'text/javascript' } } - }), - { - status: 200, - headers: { 'Content-Type': 'text/javascript' } - } - ) + ) + } + else { + // respond with generic HTTP error if JS build unsuccessful + resp = new Response(null, { status: 500 }) + } } else if (url.pathname === '/ws') { const uid = crypto.randomUUID() @@ -159,7 +172,12 @@ const start_server = function ({ context_meta, config, jsbuild_app_frontend, han console.info(['ws message', ws.data.uid, { size: m_bytes }]) if (handle_transport_bytes !== undefined) { const transport_send_bytes = wrap_transport_sendbytes.bind(null, ws) - handle_transport_bytes({ transport_send_bytes }, message) + try { + handle_transport_bytes({ transport_send_bytes }, message) + } + catch (err) { + console.error(['error during handle_transport_bytes', err]) + } } else { console.warn('Unexpected: "handle_transport_bytes" not defined') @@ -220,7 +238,7 @@ const async_run = async function ({ ws_map: new Map(), } const server = start_server({ context_meta, config: active_config, jsbuild_app_frontend, handle_transport_bytes }) - console.info(server) + console.info(`framerock app now running at ${active_config.hostname}:${active_config.port}`) process.on('SIGINT', async () => { console.log('SIGINT intercepted') close_active_ws_sessions({ context_meta }) diff --git a/frontend/index.js b/frontend/index.js index a99b100..354482c 100644 --- a/frontend/index.js +++ b/frontend/index.js @@ -32,24 +32,24 @@ class FramerockUtils { } _setup_transport ({ on_open, on_close, on_message }) { - console.info('SETUP TRANSPORT') + console.debug('SETUP TRANSPORT') this._ws = new WebSocket(`ws://${window.location.host}/ws`) const ws = this._ws ws.binaryType = 'arraybuffer' const func_on_open = () => { - console.info('WS OPEN') + console.debug('WS OPEN') on_open && on_open() return } const func_on_close = (event) => { - console.info('WS CLOSE') + console.debug('WS CLOSE') on_close && on_close(event) return } const func_on_message = (event) => { - console.info('WS MESSAGE', event) + console.debug('WS MESSAGE', event) if (on_message !== undefined) { on_message(event.data) } @@ -59,7 +59,7 @@ class FramerockUtils { return } const func_on_error = () => { - console.info('WS ERROR') + console.debug('WS ERROR') return } @@ -73,7 +73,7 @@ class FramerockUtils { } _teardown_transport () { - console.info('TEARDOWN TRANSPORT') + console.debug('TEARDOWN TRANSPORT') // NOTE: has no effect if ws already closed this._ws.close()