Created
August 15, 2017 02:59
-
-
Save hskrasek/511b910b8556c574b97ee41b7c6ea198 to your computer and use it in GitHub Desktop.
How to generate guzzle exceptions when calling Slack API and getting errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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