php-tmdb / api

PHP 7.3+ API Wrapper for The Movie Database
MIT License
402 stars 112 forks source link

Crash when using an id that doesn't exist in the repository #103

Closed Jip-Hop closed 8 years ago

Jip-Hop commented 9 years ago

$repository = new \Tmdb\Repository\TvRepository($tmdb_client); $entry = $repository->load(298312);

Fatal error: Uncaught exception 'Tmdb\Exception\TmdbApiException' with message 'The resource you requested could not be found.' in /vendor/php-tmdb/api/lib/Tmdb/HttpClient/

Same for the movie repository with a high id:

$repository = new \Tmdb\Repository\MovieRepository($tmdb_client); $entry = $repository->load(298312000);

Fatal error: Uncaught exception 'Tmdb\Exception\TmdbApiException' with message 'The resource you requested could not be found.' in /vendor/php-tmdb/api/lib/Tmdb/HttpClient/

Is this the way it should be? I'm currently using a try/catch block to prevent crashes.

ghost commented 8 years ago

yes that's the way it is, when a ID does not exist it returns a api status code 34 https://www.themoviedb.org/documentation/api/status-codes

Jip-Hop commented 8 years ago

Maybe nicer to just return an empty object instead of throwing an error which crashes the program if not handled accordingly. It's not obvious or documented that php-tmdb throws an error in this scenario.

On Thu, Oct 8, 2015 at 3:25 AM, DeeJaVu notifications@github.com wrote:

yes that's the way it is, when a ID does not exist it returns a api status code 34 https://www.themoviedb.org/documentation/api/status-codes

Reply to this email directly or view it on GitHub: https://github.com/php-tmdb/api/issues/103#issuecomment-146388249

wtfzdotnet commented 8 years ago

I strongly disagree returning an empty object. This would make detecting errors and handling them appropriately nearly impossible.

However more specific errors would be welcome, that all extend the same api exception or an other logical parent of the error, so backwards compatibility is kept. These errors are thrown because of the response that tmdb returns, which is an actual error response.

wtfzdotnet commented 8 years ago

I rather think the way you are trying to catch the error is faulty, probably because you are not importing the namespace for the exceptions, and thus it isn't catched.

Jip-Hop commented 8 years ago

All right so it is a well considered design choice then. Is there an example on github on how to gracefully handle errors in case I load a movie by the tmdb id which no longer exists (which is a scenario in which this error is thrown)?

On Sun, Oct 11, 2015 at 5:53 PM, Michael Roterman notifications@github.com wrote:

I rather think the way you are trying to catch the error is faulty, probably because you are not importing the namespace for the exceptions, and thus it isn't catched.

Reply to this email directly or view it on GitHub: https://github.com/php-tmdb/api/issues/103#issuecomment-147212764

wtfzdotnet commented 8 years ago

See https://github.com/php-tmdb/api/blob/2.0/examples/exception_handling.php

AdhaarSharma commented 2 years ago

We can use Try and Exception to get an empty object which will work as a placeholder. I was facing the same issue during data fetch for a machine learning project and it fixed it for me.