Skip to content

Instantly share code, notes, and snippets.

@hskrasek
Created August 15, 2017 02:59
Show Gist options
  • Save hskrasek/511b910b8556c574b97ee41b7c6ea198 to your computer and use it in GitHub Desktop.
Save hskrasek/511b910b8556c574b97ee41b7c6ea198 to your computer and use it in GitHub Desktop.
How to generate guzzle exceptions when calling Slack API and getting errors
<?php
$stack = new \GuzzleHttp\HandlerStack(\GuzzleHttp\choose_handler());
$stack->push(function (callable $handler) {
return function (
\Psr\Http\Message\RequestInterface $request,
array $options
) use ($handler) {
$promise = $handler($request, $options);
return $promise->then(
function (\Psr\Http\Message\ResponseInterface $response) use ($request) {
$data = \GuzzleHttp\json_decode((string) $response->getBody(), true);
if (!$data['ok']) {
throw new \GuzzleHttp\Exception\RequestException($data['error'], $request, $response);
}
return $response;
}
);
};
});
$client = new \GuzzleHttp\Client(['handler' => $stack]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment