Skip to content

Commit

Permalink
Merge pull request #112 from LibreSign/handle_error
Browse files Browse the repository at this point in the history
Catch error
  • Loading branch information
vitormattos authored Mar 11, 2021
2 parents 9694787 + 316cca0 commit 182a385
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions lib/Handler/CfsslHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OCA\Libresign\Handler;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\TransferException;
use OCA\Libresign\Exception\LibresignException;

class CfsslHandler {
Expand Down Expand Up @@ -40,33 +41,41 @@ private function newCert(
string $organizationUnit,
string $cfsslUri
) {
$response = (new Client(['base_uri' => $cfsslUri]))
->request(
'POST',
'newcert',
[
'json' => [
'profile' => 'CA',
'request' => [
'hosts' => $hosts,
$json = [
'json' => [
'profile' => 'CA',
'request' => [
'hosts' => $hosts,
'CN' => $commonName,
'key' => [
'algo' => 'rsa',
'size' => 2048,
],
'names' => [
[
'C' => $country,
'O' => $organization,
'OU' => $organizationUnit,
'CN' => $commonName,
'key' => [
'algo' => 'rsa',
'size' => 2048,
],
'names' => [
[
'C' => $country,
'O' => $organization,
'OU' => $organizationUnit,
'CN' => $commonName,
],
],
],
],
]
)
;
],
],
];
try {
$response = (new Client(['base_uri' => $cfsslUri]))
->request(
'POST',
'newcert',
$json
)
;
} catch (TransferException $th) {
if ($th->getHandlerContext() && $th->getHandlerContext()['error']) {
throw new \Exception($th->getHandlerContext()['error'], 1);
}
throw new LibresignException($th->getMessage(), 500);
}

$responseDecoded = json_decode($response->getBody(), true);
if (!$responseDecoded['success']) {
Expand Down

0 comments on commit 182a385

Please sign in to comment.