Search code examples
javathemoviedb-api

Exception handling with Response Status codes on TMDB API using 'themoviedbapi' wrapper


I got some trouble using the wrapper 'themoviedbapi' to crawl the data of TMDB.

I wrote a test class to see, what may occur when I want to put those json responses into a db.

So, when hitting an ID, which is not taken, I got following messages:

Exception in thread "main" ResponseStatus{code=34, message=The resource you requested could not be found.}
at info.movito.themoviedbapi.AbstractTmdbApi.mapJsonResult(AbstractTmdbApi.java:78)
at info.movito.themoviedbapi.AbstractTmdbApi.mapJsonResult(AbstractTmdbApi.java:45)
at info.movito.themoviedbapi.AbstractTmdbApi.mapJsonResult(AbstractTmdbApi.java:40)
at info.movito.themoviedbapi.TmdbMovies.getMovie(TmdbMovies.java:60)
at tmdb_api_to_db.Test.main(Test.java:21)

As my Java is rusty as hell, would you please tell me, how to handle this? I was thinking about try&catch, but I would appreciate any good advice!

Here's my code so far:

package tmdb_api_to_db;

import org.apache.commons.httpclient.HttpException;
import info.movito.themoviedbapi.TmdbApi;
import info.movito.themoviedbapi.TmdbMovies;
import info.movito.themoviedbapi.model.Credits;
import info.movito.themoviedbapi.model.MovieDb;

public class Test {


public static void main(String[]args){

    int movieID = 1;

    try{

    TmdbMovies movies = new TmdbApi("###########").getMovies();
    MovieDb movie_name = movies.getMovie(movieID, "en");
    Credits movie_credits = movies.getCredits(movieID);

    System.out.println(movie_name);
    System.out.println(movie_credits.getCast());
    System.out.println(movie_credits.getCrew());
    System.out.println(movie_credits.getGuestStars());
    System.out.println(prettyJsonString);
    }catch(XX){
        }

    }

}

Solution

  • Using a try catch is a good approach. I don't know about this API but you can check the error code in the catch to decide what to do next.

    If you really want to be sure the error comes from a specific line I would suggest you to modify your try/catch like this :

    // ..
    
    TmdbMovies movies = new TmdbApi("###########").getMovies();
    MovieDb movie_name;
    try{
        movie_name = movies.getMovie(movieID, "en");
    }catch(Throwable e){
        System.err.println("Erreur getting movie #"+movieID);
        return;
    }
    
    // ...