Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Better curl error messages for ^6.1 (#319)
Browse files Browse the repository at this point in the history
Better curl error messages for ^6.1
Proposal for issue #316
  • Loading branch information
Spomky authored Feb 19, 2018
1 parent cdc769b commit 108bde9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
25 changes: 22 additions & 3 deletions src/Object/DownloadedJWKSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ protected function getContent()
}

/**
* @throws \InvalidArgumentException
* @throws \InvalidArgumentException Has CURL error message and the CURL error
* number as exception code.
* Throws exception with error number 0
* if the error is not related to CURL.
*
* @return string
*/
Expand All @@ -137,9 +140,25 @@ private function downloadContent()
$ch = curl_init();
curl_setopt_array($ch, $params);
$content = curl_exec($ch);
curl_close($ch);

Assertion::false(false === $content, 'Unable to get content.');
try {
Assertion::false(false === $content, 'Failed to load JWK contents: ');
} catch (\Assert\AssertionFailedException $e) {
$curlError = curl_error($ch);
$curlErrorNumber = curl_errno($ch);

throw new \InvalidArgumentException(
$e->getMessage().$curlError,
$curlErrorNumber
);
} catch (\Exception $e) {
throw new \InvalidArgumentException(
'Failed to load JWK contents: '.$e->getMessage(),
0
);
} finally {
curl_close($ch);
}

return $content;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Object/JKUJWKSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ final class JKUJWKSet extends DownloadedJWKSet
public function getKeys()
{
$content = json_decode($this->getContent(), true);
Assertion::isArray($content, 'Invalid content.');
Assertion::keyExists($content, 'keys', 'Invalid content.');
Assertion::isArray($content, 'Invalid JWK content.');
Assertion::keyExists($content, 'keys', 'Invalid JWKSet content.');

return (new JWKSet($content))->getKeys();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Object/X5UJWKSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ final class X5UJWKSet extends DownloadedJWKSet
public function getKeys()
{
$content = json_decode($this->getContent(), true);
Assertion::isArray($content, 'Invalid content.');
Assertion::isArray($content, 'Invalid JWK content.');
$jwkset = new JWKSet();
foreach ($content as $kid => $cert) {
$jwk = KeyConverter::loadKeyFromCertificate($cert);
Assertion::notEmpty($jwk, 'Invalid content.');
Assertion::notEmpty($jwk, 'Invalid JWKSet content.');
if (is_string($kid)) {
$jwk['kid'] = $kid;
}
Expand Down

0 comments on commit 108bde9

Please sign in to comment.