better error handling (now wrapping user-defined functions with try/catch), better frontend logging (debug replaces info)

This commit is contained in:
dab 2025-08-15 14:39:24 +00:00
parent 8f44f6b508
commit 59d83d7857
2 changed files with 37 additions and 19 deletions

View file

@ -91,7 +91,15 @@ 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()
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: {
@ -104,6 +112,11 @@ const start_server = function ({ context_meta, config, jsbuild_app_frontend, han
}
)
}
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()
if (server.upgrade(req, {
@ -159,8 +172,13 @@ 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)
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 })

View file

@ -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()