Skip to content

Commit d2c81c5

Browse files
Merge pull request #7 from Setasign/BetterErrorHandling
Better error handling
2 parents 6af1635 + 6e50f85 commit d2c81c5

10 files changed

Lines changed: 179 additions & 83 deletions

File tree

composer.lock

Lines changed: 50 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/appearance-demo.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use setasign\SetaPDF\Signer\Module\CSC\Client;
6+
use setasign\SetaPDF\Signer\Module\CSC\ClientException;
67
use setasign\SetaPDF\Signer\Module\CSC\Module;
78

89
ini_set('display_errors', '1');
@@ -108,7 +109,19 @@
108109
$appearance = new SetaPDF_Signer_Signature_Appearance_Dynamic($module);
109110
$signer->setAppearance($appearance);
110111

111-
$signer->sign($module);
112+
try {
113+
$signer->sign($module);
114+
} catch (ClientException $e) {
115+
echo 'An error occured:';
116+
echo $e->getMessage() . ': ' . $e->getResponse()->getBody();
117+
echo '<br/><a href="?">restart</a>';
118+
die();
119+
} catch (\Exception $e) {
120+
echo 'An error occured:';
121+
echo $e->getMessage();
122+
echo '<br/><a href="?">restart</a>';
123+
die();
124+
}
112125

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

examples/assets/SSL.com.dev-ca.cer

1.5 KB
Binary file not shown.

examples/async-demo.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
declare(strict_types=1);
44

5+
use League\OAuth2\Client\OptionProvider\OptionProviderInterface;
6+
use League\OAuth2\Client\Provider\AbstractProvider;
57
use League\OAuth2\Client\Provider\GenericProvider;
68
use SetaPDF_Signer_Digest as Digest;
79
use setasign\SetaPDF\Signer\Module\CSC\Client;
@@ -26,7 +28,7 @@
2628
$settings = require 'settings.php';
2729
$apiUri = $settings['apiUri'];
2830

29-
$fileToSign = __DIR__ . '/Laboratory-Report.pdf';
31+
$fileToSign = __DIR__ . '/assets/Laboratory-Report.pdf';
3032
$resultPath = 'signed.pdf';
3133

3234

@@ -56,7 +58,23 @@
5658
'urlAuthorize' => $oauth2Urls['urlAuthorize'],
5759
'urlAccessToken' => $oauth2Urls['urlAccessToken'],
5860
'urlResourceOwnerDetails' => $oauth2Urls['urlResourceOwnerDetails'],
59-
]);
61+
] /*
62+
// If your OAuth endpoint requires a JSON document instead of a form-encoded document pass following "optionProvider"
63+
// to the GenericProvider constructor:
64+
, [
65+
'optionProvider' => new class implements OptionProviderInterface {
66+
public function getAccessTokenOptions($method, array $params)
67+
{
68+
$options = ['headers' => ['content-type' => 'application/json']];
69+
70+
if ($method === AbstractProvider::METHOD_POST) {
71+
$options['body'] = json_encode($params);
72+
}
73+
74+
return $options;
75+
}
76+
}
77+
]*/);
6078

6179
$action = $_GET['action'] ?? 'preview';
6280
// if the oauth request was unsuccessful, return to preSign

examples/demo.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use setasign\SetaPDF\Signer\Module\CSC\Client;
6+
use setasign\SetaPDF\Signer\Module\CSC\ClientException;
67
use setasign\SetaPDF\Signer\Module\CSC\Module;
78

89
ini_set('display_errors', '1');
@@ -93,8 +94,19 @@
9394

9495
// create the signer instance
9596
$signer = new SetaPDF_Signer($document);
96-
97-
$signer->sign($module);
97+
try {
98+
$signer->sign($module);
99+
} catch (ClientException $e) {
100+
echo 'An error occured:';
101+
echo $e->getMessage() . ': ' . $e->getResponse()->getBody();
102+
echo '<br/><a href="?">restart</a>';
103+
die();
104+
} catch (\Exception $e) {
105+
echo 'An error occured:';
106+
echo $e->getMessage();
107+
echo '<br/><a href="?">restart</a>';
108+
die();
109+
}
98110

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

0 commit comments

Comments
 (0)