better error handling (now wrapping user-defined functions with try/catch), better frontend logging (debug replaces info)
This commit is contained in:
parent
8f44f6b508
commit
59d83d7857
2 changed files with 37 additions and 19 deletions
|
@ -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' } })
|
resp = new Response(get_siteroot_html({ page_title: config.page_title }), { status: 200, headers: { 'Content-Type': 'text/html' } })
|
||||||
}
|
}
|
||||||
else if (url.pathname === '/index.js') {
|
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(
|
resp = new Response(
|
||||||
await async_build_js_script(import.meta.dir + '/../frontend/index.js', {
|
await async_build_js_script(import.meta.dir + '/../frontend/index.js', {
|
||||||
define: {
|
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') {
|
else if (url.pathname === '/ws') {
|
||||||
const uid = crypto.randomUUID()
|
const uid = crypto.randomUUID()
|
||||||
if (server.upgrade(req, {
|
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 }])
|
console.info(['ws message', ws.data.uid, { size: m_bytes }])
|
||||||
if (handle_transport_bytes !== undefined) {
|
if (handle_transport_bytes !== undefined) {
|
||||||
const transport_send_bytes = wrap_transport_sendbytes.bind(null, ws)
|
const transport_send_bytes = wrap_transport_sendbytes.bind(null, ws)
|
||||||
|
try {
|
||||||
handle_transport_bytes({ transport_send_bytes }, message)
|
handle_transport_bytes({ transport_send_bytes }, message)
|
||||||
}
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(['error during handle_transport_bytes', err])
|
||||||
|
}
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
console.warn('Unexpected: "handle_transport_bytes" not defined')
|
console.warn('Unexpected: "handle_transport_bytes" not defined')
|
||||||
}
|
}
|
||||||
|
@ -220,7 +238,7 @@ const async_run = async function ({
|
||||||
ws_map: new Map(),
|
ws_map: new Map(),
|
||||||
}
|
}
|
||||||
const server = start_server({ context_meta, config: active_config, jsbuild_app_frontend, handle_transport_bytes })
|
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 () => {
|
process.on('SIGINT', async () => {
|
||||||
console.log('SIGINT intercepted')
|
console.log('SIGINT intercepted')
|
||||||
close_active_ws_sessions({ context_meta })
|
close_active_ws_sessions({ context_meta })
|
||||||
|
|
|
@ -32,24 +32,24 @@ class FramerockUtils {
|
||||||
}
|
}
|
||||||
_setup_transport ({ on_open, on_close, on_message }) {
|
_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`)
|
this._ws = new WebSocket(`ws://${window.location.host}/ws`)
|
||||||
const ws = this._ws
|
const ws = this._ws
|
||||||
ws.binaryType = 'arraybuffer'
|
ws.binaryType = 'arraybuffer'
|
||||||
|
|
||||||
const func_on_open = () => {
|
const func_on_open = () => {
|
||||||
console.info('WS OPEN')
|
console.debug('WS OPEN')
|
||||||
on_open && on_open()
|
on_open && on_open()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const func_on_close = (event) => {
|
const func_on_close = (event) => {
|
||||||
console.info('WS CLOSE')
|
console.debug('WS CLOSE')
|
||||||
on_close && on_close(event)
|
on_close && on_close(event)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const func_on_message = (event) => {
|
const func_on_message = (event) => {
|
||||||
console.info('WS MESSAGE', event)
|
console.debug('WS MESSAGE', event)
|
||||||
if (on_message !== undefined) {
|
if (on_message !== undefined) {
|
||||||
on_message(event.data)
|
on_message(event.data)
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ class FramerockUtils {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const func_on_error = () => {
|
const func_on_error = () => {
|
||||||
console.info('WS ERROR')
|
console.debug('WS ERROR')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ class FramerockUtils {
|
||||||
}
|
}
|
||||||
_teardown_transport () {
|
_teardown_transport () {
|
||||||
|
|
||||||
console.info('TEARDOWN TRANSPORT')
|
console.debug('TEARDOWN TRANSPORT')
|
||||||
|
|
||||||
// NOTE: has no effect if ws already closed
|
// NOTE: has no effect if ws already closed
|
||||||
this._ws.close()
|
this._ws.close()
|
||||||
|
|
Loading…
Add table
Reference in a new issue