1

I am building a class wrapper around the themoviedb.org api. I'm using guzzle 7 for the requests, but it seems that it is not throwing any exception.

namespace App\Classes;

use App\Models\Movie;
use App\Models\Series;
use GuzzleHttp\Client;

use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Uri;

use Psr\Http\Message\RequestInterface;

class TMDBScraper{

private string $apiKey;
private string $language;
private Client $client;
private const API_URL = "http://api.themoviedb.org/3/";
private const IMAGE_URL = "http://image.tmdb.org/t/p/";
private const POSTER_PATH_SIZE = "w500";
private const BACKDROP_PATH_SIZE = "original";


public function __construct(string $apiKey = "default_api_key") {
    $this->apiKey = $apiKey;
    $this->language = app()->getLocale();

    $handlerStack = new HandlerStack(new CurlHandler());
    $handlerStack->unshift(Middleware::mapRequest(function (RequestInterface $request) {
        return $request->withUri(Uri::withQueryValues($request->getUri(), [
            'api_key' => $this->apiKey,
            'language' => $this->language
        ]));
    }));

    $this->client = new Client([
        'base_uri' => self::API_URL,
        'handler' => $handlerStack
    ]);

}

public function search($screenplayType, $query): ?array {

    try {

        $response = json_decode($this->client->get('search/' . $screenplayType, [
            'query' => compact('query')
        ])->getBody());

        return $this->toModel($response, $screenplayType);

    } catch (GuzzleException $e) {
        echo $e->getMessage();
        return null;
    }

}

... more code }

I tried to use a wrong api key, but the client exception is not thrown. I also tried to set http_errors to true, that should be set by default, but it didn't work too.

Mike
  • 387
  • 1
  • 4
  • 10
  • at first there is no GuzzleException class, change it to see from [here](https://stackoverflow.com/a/64603614/9471283) – bhucho Sep 29 '21 at 13:33
  • @bhucho you mean no GuzzleException class imported?, i added more code for the imports, because it is imported. and as stated in the [guzzle docs](https://docs.guzzlephp.org/en/stable/quickstart.html), the GuzzleException handles also the ClientException – Mike Sep 29 '21 at 14:04
  • 1
    You can't call GuzzleException it is an interface not a class, it is implemented by TransferException, see Exception Tree https://docs.guzzlephp.org/en/latest/quickstart.html#exceptions – bhucho Sep 29 '21 at 14:24

1 Answers1

-1

You can try this code:

$handler = new CurlHandler();
$stack = HandlerStack::create($handler);
Martinaut
  • 1,868
  • 1
  • 17
  • 27
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Apr 26 '22 at 21:58