-
Notifications
You must be signed in to change notification settings - Fork 218
Exception Handling
Error handling is an important topic for Twitter developers. The reason is that an operation can be invalid, unauthorized, rate limited... and this will result in the REST API to throw an exception (the WebRequest
returns a status code that indicates that an error occurred).
It is therefore essential that each time you execute an operation on Twitter you make sure that nothing goes wrong.
Tweetinvi provides the ExceptionHandler
which goal is to make the life of developers easier when it comes to exception handling.
Important note : by default the exception handler is swallowing exceptions and any request that would have thrown an exception will instead return null or false.
Tweetinvi gives developers 2 ways to handle exceptions.
- Global Application Handling Mechanism (DEFAULT BEHAVIOR) : You will be able to tell if the operation has failed because the result will either be null or false. But you will not be able to easily check which error caused the exception.
-
Per request exception handling : Every method will throw an
ArgumentException
orTwitterException
. This is a great way to identify what went wrong during the flow of method execution query. But it means that you will need to have a try/catch around each of your query.
Please read the following parts so that you can compare the code and to understand when to use each of the solution.
Quick Example
var authenticatedUser = User.GetAuthenticatedUser();
if (authenticatedUser == null) // Something went wrong but we don't know what
{
// We can get the latest exception received by Tweetinvi
var latestException = ExceptionHandler.GetLastException();
Console.WriteLine("The following error occured : '{0}'", latestException.TwitterDescription);
}
This solution will give you more control over the exception handling. But it will require that you add a try/catch around every method from the Tweetinvi library that actually execute a HttpRequest
. 2 types of exceptions can be thrown :
-
ArgumentException
: It will be thrown if the parameters are missing or invalid. It is thrown before the request is sent to twitter API and indicates that something wrong with parameters. -
TwitterException
: It indicates that request has been created successfully but failed to execute.
Quick Example
// Disable the exception swallowing to allow exception to be thrown by Tweetinvi
ExceptionHandler.SwallowWebExceptions = false;
// Execute an operation
try
{
var authenticatedUser = User.GetAuthenticatedUser(null, new GetAuthenticatedUserParameters());
}
catch (ArgumentException ex)
{
//Something went wrong with the arguments and request was not performed
Console.WriteLine("Request parameters are invalid: '{0}'", ex.Message);
}
catch (TwitterException ex)
{
// Twitter API Request has been failed; Bad request, network failure or unauthorized request
Console.WriteLine("Something went wrong when we tried to execute the http request : '{0}'", ex.TwitterDescription);
}
Tweetinvi will throw exception if a parameter is invalid. It will throw an exception of type ArgumentException
and it is end user's responsibility to manage all the exceptions for their application. Parameter exceptions are turned off by-default and can be turned on using following settings
// Disable the exception swallowing to allow exception to be thrown by Tweetinvi
ExceptionHandler.SwallowWebExceptions = false;