quant-ux icon indicating copy to clipboard operation
quant-ux copied to clipboard

Retrieve worker JS code syntax|runtime errors

Open Equilibrier opened this issue 2 years ago • 1 comments

Hi Klaus,

Managed to successfully grab the errors from the JS worker code, and here is how I've done it:

(pasting a section from my ScriptWorker.js, from the beginning, after the imports)

` //...

self.addEventListener('error', (errorEvent) => { const { lineno, colno, message } = errorEvent; console.log(Error thrown at: ${lineno}:${colno}: ${message}); // Don't pollute the console with additional info: errorEvent.preventDefault();

    Logger.error(1, 'ScriptWorker.message() > Error', errorEvent)
    console.error(errorEvent)
    //const evEvent = JSON.parse(JSON.stringify(errorEvent))
    self.postMessage({
        status: 'error',
        //console: console.messages,
        error: errorEvent.message,
        stack: errorEvent.stack
    })
});

const js = e.data.code
const model = e.data.model
const viewModel = e.data.viewModel
const scalingFactor = e.data.scaleFactor

const qux = new ScriptAPI(model, scalingFactor)
const code = new Function('qux', 'data', 'console', js);
const console = new ScriptConsole()
let result = undefined
try {
    setTimeout(() => {
        result = code(qux, viewModel, console)

        self.postMessage({
            to: result !== undefined ? (typeof result === 'string' ? result : result.to) : undefined,
            delayedBackMs: result !== undefined ? result.backTimeout : undefined,
            loop: result !== undefined ? result.loop : undefined,
            immediateTransition: result !== undefined && result.immediateTransition !== undefined ? result.immediateTransition : false,
            viewModel: viewModel,
            appDeltas: qux.getAppDeltas(),
            console: console.messages,
            status : 'ok'
        })
    });
} catch (error) {

//... `

So, what I added is this: image

And the results are promissing: a syntax error as an example: image image

and a runtime error: image image and we also have a nice stack we can print if we access errorEv.error.stack: image

So, two actual mini-problems, (1) could not pass the console.messages object as it complains something about not being able to clone the object and (2) it shows me the error (don't know why) on two lines bellow the correct one, but this is an error I can live with, and if it is a constant, we can correct it in the code, of course. But nonetheless, it works perfectly for me, and very useful, as until now I had to solve my issue by other external means, and these were not the best approaches for syntax/runtime issues.

Hope it is as useful to you as it was for me ! All the best and good luck !

Equilibrier avatar Aug 09 '22 11:08 Equilibrier

Have you tried in the compiled version ( so after npm run build)? This messes quite a lot with the JS...

KlausSchaefers avatar Aug 28 '22 17:08 KlausSchaefers