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:
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!
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.
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.