Error Handling
Why should I handle these errors?
Not having any logic in place to handle errors is very bad practice, as data integrity may be compromised leading to your script possibly breaking. Error handling will ensure your script works as intended, so it is vital to add some basic error handling logic to your script when using Voyager.
Basic error handling
All of the webhook's methods excluding some private ones return a second item, a table of type RequestStatus. With this table you can easily handle any error that may occur.
First we have to put the RequestStatus table into a variable.
The reason we put a _ before requestStatus is because SendMessage can return a Message instance but since the waitForMessage paramater is false by defualt, it wont. And since it wont, the first item will be nil so we name it _ to show it'll not be used.
Now we can check if the request was successfuly sent to the proxy.
And just like that we've set up basic error handling! The same thing can be done with EditMessage, EditMessageInThread, DeleteMessage, DeleteMessageInThread as well.
Advanced error handling
Lets say we want to send a message to Discord and print that message's id. The code would look something like this.
But this code is actually very unsafe as the request isn't guaranteed to be sent successfuly 100% of the time. And since the request isn't guaranteed to be sent the message variable may be nil, causing the script to error.
Now we can set up some logic to prevent that from happening. First we should put the RequestStatus table into a variable.
Now that we have the RequestStatus table we can check to make sure the message was sent successfully.
Now we can take things a step further by handling specific HTTP errors like 429, 400, 500, 503, etc differently.
And just like that the script is now ready to handle any error it may come across.