Catch an exception

Options
I've converted an API end point with a precondition set to enforce that data is returned.  Now that this is a function, is it possible that the calling function can catch exception from the precondition in the function and do something with it?

sequence:
1.  main function calls the function
2. function fails based on the precondition and generates an exception
3. currently, the main function exits with the exception from step 2

I'd like to be able to catch the exception from step 2 and continue down the main function.

Comments

  • In general, if there's a way to do some kind of try/catch, it would be extremely useful.
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
    Xano doesn't do try/catch but you should be able to accomplish the same thing with conditionals and preconditions
  • Thank you, Michael.
  • Ambroise
    Ambroise Member
    Options

    Wow, that's a big difference from traditional development.

    Just experienced this after doing some basic check:

    const { DateTime, Duration } = luxon; // See https://docs.xano.com/working-with-data/lambdas-javascript/libraries-and-functions#luxon
    
    const { sunday, employeeId } = $input;
    const date = DateTime.fromISO(sunday);
    
    // If date isn't a sunday, stop the script
    if (date.weekday !== 7) {
      throw new Error(`The provided date is not a Sunday "${sunday}". The provided date must be a Sunday but is a "${date.weekdayLong}", because the script will generate a Weekly Timesheet (7 days), starting by the week's Sunday.`);
    }
    
    

    But even when this script was failing, the rest of the actions was executed, not quite what I had expected!