TTN javascript decoder error handling?

Clearly the javascript payload decoder does not support the javascript error handling as try{}, catch{} and finally{}. Any idea about alternatives?

Works for me:

function Decoder(bytes, port) {
  var error;
  try {
    console.log("Here we go!");
    // should throw: 'myvar' is not defined
    console.log(myvar);
  }
  catch (e) {
    console.log(e);
    error = e;
  }
  finally {
    if (error) {
      return {worksForMe: error};
    }
    return {oops: "exception not caught"};
  }
}

This shows:

TTN Console

When not using try-catch then see also the MQTT error events as mentioned in Is there any documentation on payload functions?

Thanks Arjan. Solved:
catch {} as empty was not accepted (My excuse: Firefox JS scratchbook did accept it however and did not complain).
Solution is simple: do not use catch as empty statement.

1 Like

Probably not important at all, but for future reference: leaving out the identifier (e) (or whatever name is used like, like (error)) from catch (e) is indeed invalid. But an empty statement work just fine as well:

function Decoder(bytes, port) {
  try {
    console.log("Here we go!");
    // should throw: 'myvar' is not defined
    console.log(myvar);
    console.log("Oops, we should not get here");
  }
  catch (e) {
  }
  finally {
    return {worksForMe: "yes"};
  }
  return {oops: "finally not executed"};
}

image

Same results without any catch at all; at least one of catch or finally must be present.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.