Nightbot try/catch not printing

I’m curious about Nightbot exception handling, and I for the life of me cannot find an answer in these forums.

I have a series of eval commands that include a try/catch block that should return an error if there is a problem. However, no matter how many different ways I’ve tried, I just can’t get the error to print.

As an example, here’s an eval command I have for printing out my socials:

$(eval try { const query = decodeURIComponent("$(querystring)"); const response = $(urlfetch json https://raw.githubusercontent.com/NotSwayzeStream/nightbot/v3.0.0/socials.json); response[query] } catch (e) {e})

My expectation would be to have my function print the social account referenced by the query key in the urlfetch response, and if it doesn’t exist, to return the error instead. However, when I run this command and use a key that doesn’t exist, it fails silently.

Am I missing something? Happy to also be pointed to another post if I’m doing something silly!

Something like that?

$(eval try { const query = decodeURIComponent("$(querystring)"); const response = $(urlfetch json https://raw.githubusercontent.com/NotSwayzeStream/nightbot/v3.0.0/socials.json); response[query] ? response[query] : 'Error: Command "' + query + '" does not exist.' } catch (e) { 'Error: A problem occurred while processing the command.' })

Fascinating, so this does work, but not in the way I expected – I’m really curious what the catch would actually be doing because I’ve never seen it trigger.

Thanks for this suggestion! Taking this ternary operator approach is probably the way to go. I’m also going to test a short circuit version as well to reduce the number of lines.

So here’s where I ultimately landed:

  • Commands with simple syntax will use an OR short circuit
    try { response[query] || "Hey $(channel)! '$(command)' doesn't support input '$(query)'" } catch (e) {"Hey $(channel)! We caught this error: '$(e)'"}

  • Commands with more complicated syntax will use a ternary operator
    try { response[query] ? 'SWAYZE RAID ${response[query]}'.repeat(10).trimEnd() : "Hey $(channel)! '$(command)' doesn't support input '$(query)'" } catch (e) {"Hey $(channel)! We caught this error: '$(e)"}

Reason being is not using ternary operators for some commands actually managed to raise an error message, which I was not seeing before.

Thank you for your help!

1 Like

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