Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check handler type on construction #1745

Merged
merged 2 commits into from
Feb 19, 2017

Conversation

DQNEO
Copy link
Contributor

@DQNEO DQNEO commented Feb 18, 2017

Background

handler option must be an callable, but there is no type check

Current behavior

So Fatal Error occurs on request time.

$client = new GuzzleHttp\Client(['handler' => 'x']);
$res = $client->request('get', 'https://example.com/');
PHP Fatal error:  Uncaught Error: Call to undefined function x() in /Users/DQNEO/src/github.com/DQNEO/guzzle/src/Client.php:275
Stack trace:
#0 /Users/DQNEO/src/github.com/DQNEO/guzzle/src/Client.php(123): GuzzleHttp\Client->transfer(Object(GuzzleHttp\Psr7\Request), Array)
#1 /Users/DQNEO/src/github.com/DQNEO/guzzle/src/Client.php(129): GuzzleHttp\Client->requestAsync('get', Object(GuzzleHttp\Psr7\Uri), Array)
#2 /Users/DQNEO/src/github.com/DQNEO/guzzle/,.php(9): GuzzleHttp\Client->request('get', 'https://httpbin...')
#3 {main}
  thrown in /Users/DQNEO/src/github.com/DQNEO/guzzle/src/Client.php on line 275

New behavior

by this PR, users can see a exception on construction , which is more easier to find the cause.

PHP Fatal error:  Uncaught InvalidArgumentException: handler must be a callable in /Users/DQNEO/src/github.com/DQNEO/guzzle/src/Client.php:66
Stack trace:
#0 /Users/DQNEO/src/github.com/DQNEO/guzzle/,.php(7): GuzzleHttp\Client->__construct(Array)
#1 {main}
  thrown in /Users/DQNEO/src/github.com/DQNEO/guzzle/src/Client.php on line 66

src/Client.php Outdated
@@ -61,7 +61,11 @@ class Client implements ClientInterface
*/
public function __construct(array $config = [])
{
if (!isset($config['handler'])) {
if (isset($config['handler'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR looks good. This if statement could be simplified though:

if (!isset($config['handler'])) {
    $config['handler'] = HandlerStack::create();
} elseif (!is_callable($config['handler'])) {
    throw new \InvalidArgumentException('handler must be a callable');
}

@DQNEO
Copy link
Contributor Author

DQNEO commented Feb 19, 2017

I fixed it!

@sagikazarmark sagikazarmark added this to the 6.3.0 milestone Feb 19, 2017
@sagikazarmark
Copy link
Member

Thanks for your contribution

@sagikazarmark sagikazarmark merged commit 6a99df9 into guzzle:master Feb 19, 2017
@DQNEO DQNEO deleted the check-type-for-handler branch February 19, 2017 18:06
thinsoldier pushed a commit to thinsoldier/guzzle that referenced this pull request Mar 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants