|
21 | 21 | * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/error_reporting/README.md
|
22 | 22 | */
|
23 | 23 |
|
24 |
| -// Include Google Cloud dependendencies using Composer |
25 |
| -require_once __DIR__ . '/../vendor/autoload.php'; |
26 |
| - |
27 |
| -if (count($argv) < 3 || count($argv) > 4) { |
28 |
| - return printf("Usage: php %s PROJECT_ID ERROR_MESSAGE [USER]\n", basename(__FILE__)); |
29 |
| -} |
30 |
| -list($_, $projectId, $message) = $argv; |
31 |
| -$user = isset($argv[3]) ? $argv[3] : ''; |
| 24 | +namespace Google\Cloud\Samples\ErrorReporting; |
32 | 25 |
|
33 | 26 | # [START report_error]
|
34 | 27 | use Google\Cloud\ErrorReporting\V1beta1\ReportErrorsServiceClient;
|
|
41 | 34 | * The ReportedErrorEvent object gives you more control over how the error
|
42 | 35 | * appears and the details associated with it.
|
43 | 36 | *
|
44 |
| - * Uncomment these line and replace with your project ID, message, and optionally your user. |
| 37 | + * @param string $projectId Your Google Cloud Project ID. |
| 38 | + * @param string $message The error message to report. |
| 39 | + * @param string $user Optional user email address |
45 | 40 | */
|
46 |
| -// $projectId = 'YOUR_PROJECT_ID'; |
47 |
| -// $message = 'This is the error message to report!'; |
48 |
| - |
49 |
| - |
50 |
| -$errors = new ReportErrorsServiceClient(); |
51 |
| -$projectName = $errors->projectName($projectId); |
| 41 | +function report_error(string $projectId, string $message, string $user = '') |
| 42 | +{ |
| 43 | + $errors = new ReportErrorsServiceClient(); |
| 44 | + $projectName = $errors->projectName($projectId); |
52 | 45 |
|
53 |
| -$location = (new SourceLocation()) |
54 |
| - ->setFunctionName('global'); |
| 46 | + $location = (new SourceLocation()) |
| 47 | + ->setFunctionName('global'); |
55 | 48 |
|
56 |
| -$context = (new ErrorContext()) |
57 |
| - ->setReportLocation($location) |
58 |
| - ->setUser($user); |
| 49 | + $context = (new ErrorContext()) |
| 50 | + ->setReportLocation($location) |
| 51 | + ->setUser($user); |
59 | 52 |
|
60 |
| -$event = (new ReportedErrorEvent()) |
61 |
| - ->setMessage($message) |
62 |
| - ->setContext($context); |
| 53 | + $event = (new ReportedErrorEvent()) |
| 54 | + ->setMessage($message) |
| 55 | + ->setContext($context); |
63 | 56 |
|
64 |
| -$errors->reportErrorEvent($projectName, $event); |
| 57 | + $errors->reportErrorEvent($projectName, $event); |
| 58 | + print('Reported an exception to Stackdriver' . PHP_EOL); |
| 59 | +} |
65 | 60 | # [END report_error]
|
66 |
| -print('Reported an exception to Stackdriver' . PHP_EOL); |
| 61 | + |
| 62 | +// The following 2 lines are only needed to run the samples |
| 63 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 64 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments