From 51e51662338affa177d5e6b6043da7401cfb3caa Mon Sep 17 00:00:00 2001 From: Stuart Boston Date: Wed, 2 Oct 2013 09:15:50 +0100 Subject: [PATCH] Throw MovieDbException if movie ID not found Rather than pass a seemingly valid object back to the caller, this change will throw a "MOVIE_ID_NOT_FOUND" exception to the caller. Affects getMovieInfo and getMovieInfoImdb methods Fixes #8 --- .../omertron/themoviedbapi/TheMovieDbApi.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java index f644b6584..88bf2c788 100644 --- a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java +++ b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java @@ -477,6 +477,8 @@ public class TheMovieDbApi { * * It will return the single highest rated poster and backdrop. * + * MovieDbExceptionType.MOVIE_ID_NOT_FOUND will be thrown if there are no movies found. + * * @param movieId * @param language * @throws MovieDbException @@ -495,7 +497,12 @@ public class TheMovieDbApi { URL url = apiUrl.buildUrl(); String webpage = requestWebPage(url); try { - return mapper.readValue(webpage, MovieDb.class); + MovieDb movie = mapper.readValue(webpage, MovieDb.class); + if (movie == null || movie.getId() == 0) { + LOG.warn("No movie foind for ID '{}'", movieId); + throw new MovieDbException(MovieDbExceptionType.MOVIE_ID_NOT_FOUND, "No movie foind for ID: " + movieId); + } + return movie; } catch (IOException ex) { LOG.warn("Failed to get movie info: {}", ex.getMessage()); throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); @@ -507,6 +514,8 @@ public class TheMovieDbApi { * * It will return the single highest rated poster and backdrop. * + * MovieDbExceptionType.MOVIE_ID_NOT_FOUND will be thrown if there are no movies found. + * * @param imdbId * @param language * @throws MovieDbException @@ -525,7 +534,12 @@ public class TheMovieDbApi { URL url = apiUrl.buildUrl(); String webpage = requestWebPage(url); try { - return mapper.readValue(webpage, MovieDb.class); + MovieDb movie = mapper.readValue(webpage, MovieDb.class); + if (movie == null || movie.getId() == 0) { + LOG.warn("No movie foind for IMDB ID: '{}'", imdbId); + throw new MovieDbException(MovieDbExceptionType.MOVIE_ID_NOT_FOUND, "No movie foind for IMDB ID: " + imdbId); + } + return movie; } catch (IOException ex) { LOG.warn("Failed to get movie info: {}", ex.getMessage()); throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex);