Catching errors in Espruino Modules (SX127x)

Posted on
  • Hi 👋,

    I'm getting this error from time to time when using the "SX127x" module in a couple of projects (custom HW, and ESP32).

    Uncaught Error: Radio not found!
     at line 1 col 65
    ...ow Error("Radio not found!");this.setOpMode(0);this.mask(1,1­...
    

    I'm not so concerned with why it fails right now (probably bad SPI communication), but I'd like to catch it so I can handle it properly.

    I've tried the code below, but I still get Uncatched Error: ... in the WEB IDE.

        try {
            sx = require('SX127x').connect({
                spi: SPI1,
                cs: PIN_CS,
                rst: PIN_RESET
            });
        } catch (e) {
            console.log('Error catched: ', e);
        }
    

    Q: Can anyone advise me on how I can catch this error--or is there a more general way to catch errors that happen internally in the Espruino modules 🙂?

  • Sat 2020.09.26

    Hello @user114366,

    I haven't tested this and am not at my development PC but one item of clarification. Is the above snippet being uploaded from the R-Hand editor side of the WebIDE?

    http://www.espruino.com/Modules#-a-name-­repl-a-why-don-t-modules-work-when-typin­g-require-on-the-left-hand-side-of-the-w­eb-ide-or-from-a-terminal-window-

    link from:

    http://www.espruino.com/Modules


    ref: 'Uncatched Error:'

    Should this be taken literally as actual error text?


    Persusing the source will reveal what var objects are created as the module is fetched/parsed. Maybe a conditional test against 'undefined' or what might be expected as those are initialized, and/or throw your own user defined error to catch?

    The module source: (link from third line: http://www.espruino.com/SX127x)

    http://www.espruino.com/modules/SX127x.j­s

  • Hi Robin 🙂!

    It's uploaded from the right-hand side, then uploaded to Flash using the "Send to Espruino" button in the IDE.

    I have boiled down the code to this (right-hand side):

    // ****************************************­*********************
    // *** CONST / VARS
    // ****************************************­*********************
    
    // Pins
    const PIN_RESET = D17;
    const PIN_CS = D4;
    const PIN_SCK = D5;
    const PIN_MISO = D19;
    const PIN_MOSI = D23;
    
    let sx;
    
    // ****************************************­*********************
    // *** MAIN
    // ****************************************­*********************
    
    /*
     * Called on boot
     */
    function onInit() {
    
        try {
            SPI1.setup({ sck: PIN_SCK, miso: PIN_MISO, mosi: PIN_MOSI });
    
            sx = require('SX127x').connect({
                spi: SPI1,
                cs: PIN_CS,
                rst: PIN_RESET
            });
    
        } catch (e) {
            console.log('e: ', e);
        }
    
    }
    

    Should this be taken literally as actual error text?

    No, the error is always as shown below:

    Uncaught Error: Radio not found!
     at line 1 col 65
    ...ow Error("Radio not found!");this.setOpMode(0);this.mask(1,1­...
    

    Persusing the source will reveal what var objects are created as the module is fetched/parsed. Maybe a conditional test against 'undefined' or what might be expected as those are initialized, and/or throw your own user defined error to catch?

    I don't quite understand what you are suggesting 🙂. Which properties could I test against undefined? I mean the error is happening inside the Espruino module, and apparently I'm unable to catch it externally. Modules are not my strongest side, but should't I be able to do that?--I mean, why throw an error if you are prevented from handling it 🤔..

    Thanks for taking the time to help 😁!

  • Sun 2020.09.27

    Good Afternoon @user114366!

    Thank you for clarifying the literal 'uncaught' exception error, the full snippet and the exact error as shown.

    Still not at a development PC to test, so going from memory here and wanted to respond timely as I know your time is limited to the weekends.


    'I don't quite understand what you are suggesting. Which properties could I test against undefined?'

    I had hoped that the review of the var's value and use of dump() from last week would have triggered the answer.



    Opening module SX127x.js I searched for the error that is displayed.

    L134     SX.prototype.init
    
    L304    this.init.bind(this)
    
    L619    exports.connect
    


    'why throw an error if you are prevented from handling it'

    Agreed, and as the statement

    'I'm not so concerned with why it fails right now'

    led me to believe it is comm that is causing the error to be thrown, and the #1 snippet is what I actually do when loading an unknown module. It is quite possible that as we have a module SX.init() called from within E.on() own init function, and there may be some reason the bubble up error is not able to be trapped.

    I perform my test circa 1V95 in it's own function outside of init().

    'Which properties could I test against undefined?'

    My conception was to test the value of sx L27 as from L619 the returned 'SX' object will overwrite the sx value of 'undefined' in your code.

    Another trick I use is to test the var value of assigned constants in the called module. In this case:

    L5    var REG = {
    

    Should the module not be fetched, var 'REG' won't exist in the namespace and may be checked using dump(); and will contain the module constant data, and not 'undefined' when Espruino successfully loads/parses the module.


    'why throw an error if you are prevented from handling it?'

    I'm with you here, but in defense of the author, from line 3 within:

    http://www.espruino.com/SX127x

    In bold text: 'however it is extremely beta'

  • you can use this statement to include the module into you code

    https://www.espruino.com/Reference#l_Mod­ules_addCached

    and than some console.log() statements to nail down what’s going wrong.

  • You won't be able to catch that exception, since it's called in a setTimeout
    But you can take a look at the code, and that exception is thrown when reading the VERSION register returns 0 or 255, most likely a communication / wiring error / the radio not present or not powered. https://github.com/espruino/EspruinoDocs­/blob/master/devices/SX127x.js#L337

  • Hi AkosLukacs,

    You won't be able to catch that exception

    Although regrettable, thanks for confirming that--that basically settles it 😕👍 .

  • Hi MaBe,

    While AkosLukacs has confirmed that I will never be able to catch the exception, I'll see that come in handy next time I'm debugging Espruino modules--thanks 👍.

  • Hey Robin,

    Thanks for taking the time to write such a detailed answer 🙂!

    I had hoped that the review of the var's value and use of dump() from last week would have triggered the answer.

    I still havn't learnt how the output from dump() can help me out--I'm still just a novice in all this Espruino business 😁. I'll have to read up on that sometime.

    Opening module SX127x.js I searched for the error that is displayed.

    Yes, I got the same result regarding the source of the error.

    and there may be some reason the bubble up error is not able to be trapped.

    As AkosLukacs suggests, the reason seems to be that it's eventually thrown inside a setTimeout ?

    I'm with you here, but in defense of the author .... In bold text: 'however it is extremely beta'

    Sure, I'm not blaming anyone for making code for free, and I'm extremely satisfied with the library--I was just explaining that my assertion on JavaScript is that errors should be catchable, but apparently in some cases are not 😊.

    So I guess the case is closed--I'll never be able to catch it 🤔. Catching the error was my only concern of this thread, fixing the communication is not going to be a problem.

    Thank you so much for helping out again, you are always very thorough 😁!

  • Although the code could be refactored so the connect call returns a promise, and you could do Promise.catch.

  • So I guess the case is closed--I'll never be able to catch it

    http://www.espruino.com/Reference#l_proc­ess_uncaughtException

    So you can catch it - just define a global handler for process.uncaughtException. It's a bit of a hack but it's possible

  • Yeah, this is what someone suggested on SO as well.

    If I've understood correctly, callbacks seem to be the general convention used across the project, so I think it would be more correct to have connect provide a callback-function with an error object/string, but perhaps I'm missing something 🙂.

    I'll leave messing with the modules to the pros 😊.

  • It's a bit of a hack but it's possible

    I'll take it--thanks 😎!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Catching errors in Espruino Modules (SX127x)

Posted by Avatar for user114366 @user114366

Actions