0

I am currently exploring my first third party lib and everything is working fine except for that it should be throwing me an error when something is wrong which it doesn't..

So this is my code, and I've tried causing an error and it should be causing an error but its not, the app still runs

When I remove a letter or number from the consumer key for example, it doesnt connect and it should throw me an error.

I was looking over the docs and I was trying to follow this

Destroying a Friendship

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tweetinvi;
using Tweetinvi.Core.Authentication;
using Tweetinvi.Core;
using Tweetinvi.Credentials;
using Tweetinvi.WebLogic;

namespace Tweetinvi
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string accessToken;
        string accessTokenSecret;
        string consumerKey;
        string consumerSecret;
        string hashTag;
        string userName;



        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)

        {
            try
            {
                Auth.SetUserCredentials(consumerKeyBox.Text, consumerSecretBox.Text, accessTokenBox.Text, accessTokenSecretBox.Text);
                Tweet.PublishTweet("Hello World :)");
                //User.UnFollowUser("");
            }
            catch(Exception ex)
            {
                MessageBox.Show("Something went wrong");
            }



        }
    }
}
Linvi
  • 2,077
  • 1
  • 14
  • 28
  • What is supposed to go wrong? – Shon May 21 '16 at 17:57
  • you are running your program in `Release` mode or `Debug` mode? – Usman lqbal May 21 '16 at 17:58
  • 3
    When using third-party libraries, you must read the [documentation](https://github.com/linvi/tweetinvi/wiki/Exception-Handling) to confirm your expectations with the reality. By default, TweetInvi swallows exceptions. This should be indicated by `Tweet.PublishTweet()` returning `null` and `ExceptionHandler.GetLastException()` containing the exception. – CodeCaster May 21 '16 at 17:58

3 Answers3

1

It's not throwing an exception because there isn't one to throw.

When you publish a tweet from twitter it should return an object that you can inspect. If the publishTweet() method fails, it might not throw an exception, instead it will return a response object.

inspect that object and handle accordingly.

Ryan Ternier
  • 8,714
  • 4
  • 46
  • 69
1

I am the developer of Tweetinvi and just for the sake of clarity here is how Tweetinvi works.

  • WebException thrown by Twitter are by default handled by ExceptionHandler. The idea of such a feature is that most developers do not care why it failed but care only to know if it failed. In this case you will want to use the ExceptionHandler which is an Exception Logger. When an exception fails, Tweetinvi will return a null object or false depending of the Twitter endpoint.

  • You can disable the ExceptionHandler by using the following line of code : ExceptionHandler.SwallowWebExceptions = false;. When doing so you will have to use a try/catch on the Tweetinvi class Tweetinvi.Core.Exceptions.TwitterException. You might also want to handle the following exceptions :

    • TwitterTimeoutException : The exception has timed out as specified by TweetinviConfig.ApplicationSettings.HttpRequestTimeout.
    • TwitterNullCredentialsException and TwitterInvalidCredentialsException as set by Auth.SetUserCredentials().
  • ArgumentException and ArgumentNullException will ALWAYS be thrown if one of the parameter you specified is not correct. In the next version 0.9.13.0 we will improve the details of such exceptions Find more.

You can find a wiki for Exception handling in Tweetinvi here :

https://github.com/linvi/tweetinvi/wiki/Exception-Handling

Linvi
  • 2,077
  • 1
  • 14
  • 28
0

Try this code and see if your user is authenticated. If not, then must be permissions or your keys are wrong:

ITwitterCredentials creds = new TwitterCredentials(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
Auth.SetCredentials(creds);
var authenticatedUser = User.GetAuthenticatedUser(creds);
oleksii
  • 35,458
  • 16
  • 93
  • 163