Skip to content

Commit

Permalink
Merge pull request #7 from Setasign/BetterErrorHandling
Browse files Browse the repository at this point in the history
Better error handling
  • Loading branch information
MaximilianKresse authored Apr 18, 2023
2 parents 6af1635 + 6e50f85 commit d2c81c5
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 83 deletions.
101 changes: 50 additions & 51 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion examples/appearance-demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use setasign\SetaPDF\Signer\Module\CSC\Client;
use setasign\SetaPDF\Signer\Module\CSC\ClientException;
use setasign\SetaPDF\Signer\Module\CSC\Module;

ini_set('display_errors', '1');
Expand Down Expand Up @@ -108,7 +109,19 @@
$appearance = new SetaPDF_Signer_Signature_Appearance_Dynamic($module);
$signer->setAppearance($appearance);

$signer->sign($module);
try {
$signer->sign($module);
} catch (ClientException $e) {
echo 'An error occured:';
echo $e->getMessage() . ': ' . $e->getResponse()->getBody();
echo '<br/><a href="?">restart</a>';
die();
} catch (\Exception $e) {
echo 'An error occured:';
echo $e->getMessage();
echo '<br/><a href="?">restart</a>';
die();
}

echo '<a href="data:application/pdf;base64,' . base64_encode(file_get_contents($resultPath)) . '" ' .
'download="' . basename($resultPath) . '">download</a> | <a href="?">restart</a><br />';
Binary file added examples/assets/SSL.com.dev-ca.cer
Binary file not shown.
22 changes: 20 additions & 2 deletions examples/async-demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use League\OAuth2\Client\OptionProvider\OptionProviderInterface;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Provider\GenericProvider;
use SetaPDF_Signer_Digest as Digest;
use setasign\SetaPDF\Signer\Module\CSC\Client;
Expand All @@ -26,7 +28,7 @@
$settings = require 'settings.php';
$apiUri = $settings['apiUri'];

$fileToSign = __DIR__ . '/Laboratory-Report.pdf';
$fileToSign = __DIR__ . '/assets/Laboratory-Report.pdf';
$resultPath = 'signed.pdf';


Expand Down Expand Up @@ -56,7 +58,23 @@
'urlAuthorize' => $oauth2Urls['urlAuthorize'],
'urlAccessToken' => $oauth2Urls['urlAccessToken'],
'urlResourceOwnerDetails' => $oauth2Urls['urlResourceOwnerDetails'],
]);
] /*
// If your OAuth endpoint requires a JSON document instead of a form-encoded document pass following "optionProvider"
// to the GenericProvider constructor:
, [
'optionProvider' => new class implements OptionProviderInterface {
public function getAccessTokenOptions($method, array $params)
{
$options = ['headers' => ['content-type' => 'application/json']];
if ($method === AbstractProvider::METHOD_POST) {
$options['body'] = json_encode($params);
}
return $options;
}
}
]*/);

$action = $_GET['action'] ?? 'preview';
// if the oauth request was unsuccessful, return to preSign
Expand Down
16 changes: 14 additions & 2 deletions examples/demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use setasign\SetaPDF\Signer\Module\CSC\Client;
use setasign\SetaPDF\Signer\Module\CSC\ClientException;
use setasign\SetaPDF\Signer\Module\CSC\Module;

ini_set('display_errors', '1');
Expand Down Expand Up @@ -93,8 +94,19 @@

// create the signer instance
$signer = new SetaPDF_Signer($document);

$signer->sign($module);
try {
$signer->sign($module);
} catch (ClientException $e) {
echo 'An error occured:';
echo $e->getMessage() . ': ' . $e->getResponse()->getBody();
echo '<br/><a href="?">restart</a>';
die();
} catch (\Exception $e) {
echo 'An error occured:';
echo $e->getMessage();
echo '<br/><a href="?">restart</a>';
die();
}

echo '<a href="data:application/pdf;base64,' . base64_encode(file_get_contents($resultPath)) . '" ' .
'download="' . basename($resultPath) . '">download</a> | <a href="?">restart</a><br />';
Loading

0 comments on commit d2c81c5

Please sign in to comment.