Skip to content

Commit 108bde9

Browse files
authored
Better curl error messages for ^6.1 (Spomky-Labs#319)
Better curl error messages for ^6.1 Proposal for issue #316
1 parent cdc769b commit 108bde9

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/Object/DownloadedJWKSet.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ protected function getContent()
119119
}
120120

121121
/**
122-
* @throws \InvalidArgumentException
122+
* @throws \InvalidArgumentException Has CURL error message and the CURL error
123+
* number as exception code.
124+
* Throws exception with error number 0
125+
* if the error is not related to CURL.
123126
*
124127
* @return string
125128
*/
@@ -137,9 +140,25 @@ private function downloadContent()
137140
$ch = curl_init();
138141
curl_setopt_array($ch, $params);
139142
$content = curl_exec($ch);
140-
curl_close($ch);
141143

142-
Assertion::false(false === $content, 'Unable to get content.');
144+
try {
145+
Assertion::false(false === $content, 'Failed to load JWK contents: ');
146+
} catch (\Assert\AssertionFailedException $e) {
147+
$curlError = curl_error($ch);
148+
$curlErrorNumber = curl_errno($ch);
149+
150+
throw new \InvalidArgumentException(
151+
$e->getMessage().$curlError,
152+
$curlErrorNumber
153+
);
154+
} catch (\Exception $e) {
155+
throw new \InvalidArgumentException(
156+
'Failed to load JWK contents: '.$e->getMessage(),
157+
0
158+
);
159+
} finally {
160+
curl_close($ch);
161+
}
143162

144163
return $content;
145164
}

src/Object/JKUJWKSet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ final class JKUJWKSet extends DownloadedJWKSet
2424
public function getKeys()
2525
{
2626
$content = json_decode($this->getContent(), true);
27-
Assertion::isArray($content, 'Invalid content.');
28-
Assertion::keyExists($content, 'keys', 'Invalid content.');
27+
Assertion::isArray($content, 'Invalid JWK content.');
28+
Assertion::keyExists($content, 'keys', 'Invalid JWKSet content.');
2929

3030
return (new JWKSet($content))->getKeys();
3131
}

src/Object/X5UJWKSet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ final class X5UJWKSet extends DownloadedJWKSet
2525
public function getKeys()
2626
{
2727
$content = json_decode($this->getContent(), true);
28-
Assertion::isArray($content, 'Invalid content.');
28+
Assertion::isArray($content, 'Invalid JWK content.');
2929
$jwkset = new JWKSet();
3030
foreach ($content as $kid => $cert) {
3131
$jwk = KeyConverter::loadKeyFromCertificate($cert);
32-
Assertion::notEmpty($jwk, 'Invalid content.');
32+
Assertion::notEmpty($jwk, 'Invalid JWKSet content.');
3333
if (is_string($kid)) {
3434
$jwk['kid'] = $kid;
3535
}

0 commit comments

Comments
 (0)