-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathtroubleshoot-errors.php
More file actions
113 lines (94 loc) · 3.97 KB
/
Copy pathtroubleshoot-errors.php
File metadata and controls
113 lines (94 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* Trigger the common Cloudinary errors on purpose and handle each one by type.
*
* Every failure here is deliberate. The point is to show which exception a given mistake
* produces, so real error handling can be written against the right type.
*
* SDK logging is disabled so each failure prints once rather than being preceded by
* CRITICAL log lines on stderr.
*
* Prerequisites:
* composer require cloudinary/cloudinary_php
* export CLOUDINARY_URL=cloudinary://<api_key>:<api_secret>@<cloud_name>
*
* Run:
* php examples/troubleshoot-errors.php
*
* In your own project, the import and instantiation are:
* require 'vendor/autoload.php';
* use Cloudinary\Cloudinary;
* $cloudinary = new Cloudinary();
*/
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use Cloudinary\Api\Exception\ApiError;
use Cloudinary\Cloudinary;
use Cloudinary\Configuration\Configuration;
use Cloudinary\Exception\ConfigurationException;
function main(): void
{
// Quiet the SDK's own logging so deliberate failures do not print twice.
//
// Build the configuration from the environment first, then override. Passing a
// partial array to the constructor REPLACES the configuration rather than merging
// with it, so `new Cloudinary(['logging' => ...])` would discard the credentials.
// getenv() returns false when unset, and fromCloudinaryUrl() requires a string, so
// coerce to '' to get the SDK's own ConfigurationException rather than a TypeError.
$configuration = Configuration::fromCloudinaryUrl(getenv('CLOUDINARY_URL') ?: '');
$configuration->logging->enabled = false;
$cloudinary = new Cloudinary($configuration);
echo 'Connectivity: ', $cloudinary->adminApi()->ping()['status'], PHP_EOL, PHP_EOL;
report('Missing credentials', static function (): void {
// ConfigurationException is thrown by the constructor and does NOT extend
// ApiError, so it needs its own catch block.
new Cloudinary('cloudinary://');
});
report('Video uploaded as an image', static function () use ($cloudinary): void {
// Without resource_type => 'video' this fails: upload() defaults to 'image'.
$cloudinary->uploadApi()->upload('https://res.cloudinary.com/demo/video/upload/dog.mp4');
});
report('Asset that does not exist', static function () use ($cloudinary): void {
$cloudinary->adminApi()->asset('examples/definitely-not-here-abcdef0123456789');
});
report('Search with a leading wildcard', static function () use ($cloudinary): void {
// A '*' may end a term but not begin one.
$cloudinary->searchApi()->expression('tags:*bag*')->execute();
});
report('Undeclared metadata field', static function () use ($cloudinary): void {
$cloudinary->uploadApi()->upload('https://res.cloudinary.com/demo/image/upload/sample.jpg', [
'metadata' => ['undeclared_field_abcdef' => 'value'],
]);
});
}
/**
* Runs a deliberately failing operation and reports which exception type came back.
*/
function report(string $label, callable $operation): void
{
echo $label, PHP_EOL;
try {
$operation();
echo ' unexpectedly succeeded', PHP_EOL, PHP_EOL;
return;
} catch (ConfigurationException $e) {
$type = 'ConfigurationException (not an ApiError)';
} catch (ApiError $e) {
$type = basename(str_replace('\\', '/', get_class($e)));
}
echo ' type: ', $type, PHP_EOL;
echo ' message: ', firstLine($e->getMessage()), PHP_EOL, PHP_EOL;
}
function firstLine(string $message): string
{
$line = strtok($message, "\n");
$line = $line === false ? $message : $line;
return strlen($line) > 120 ? substr($line, 0, 117) . '...' : $line;
}
try {
main();
} catch (Throwable $e) {
fwrite(STDERR, 'Troubleshooting walkthrough failed: ' . $e->getMessage() . PHP_EOL);
fwrite(STDERR, 'Check that CLOUDINARY_URL is set (Console > Settings > API Keys).' . PHP_EOL);
exit(1);
}