Skip to content

Commit 68f35d2

Browse files
authored
chore: upgrade error_reporting samples to new format (GoogleCloudPlatform#1726)
1 parent a81cf3e commit 68f35d2

File tree

2 files changed

+27
-34
lines changed

2 files changed

+27
-34
lines changed

error_reporting/src/report_error.php

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,7 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/error_reporting/README.md
2222
*/
2323

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;
3225

3326
# [START report_error]
3427
use Google\Cloud\ErrorReporting\V1beta1\ReportErrorsServiceClient;
@@ -41,26 +34,31 @@
4134
* The ReportedErrorEvent object gives you more control over how the error
4235
* appears and the details associated with it.
4336
*
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
4540
*/
46-
// $projectId = 'YOUR_PROJECT_ID';
47-
// $message = 'This is the error message to report!';
48-
// $user = '[email protected]';
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);
5245

53-
$location = (new SourceLocation())
54-
->setFunctionName('global');
46+
$location = (new SourceLocation())
47+
->setFunctionName('global');
5548

56-
$context = (new ErrorContext())
57-
->setReportLocation($location)
58-
->setUser($user);
49+
$context = (new ErrorContext())
50+
->setReportLocation($location)
51+
->setUser($user);
5952

60-
$event = (new ReportedErrorEvent())
61-
->setMessage($message)
62-
->setContext($context);
53+
$event = (new ReportedErrorEvent())
54+
->setMessage($message)
55+
->setContext($context);
6356

64-
$errors->reportErrorEvent($projectName, $event);
57+
$errors->reportErrorEvent($projectName, $event);
58+
print('Reported an exception to Stackdriver' . PHP_EOL);
59+
}
6560
# [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);

error_reporting/test/report_errorTest.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,22 @@
1717

1818
namespace Google\Cloud\Samples\ErrorReporting;
1919

20+
use Google\Cloud\TestUtils\TestTrait;
2021
use PHPUnit\Framework\TestCase;
2122

2223
// Load the testing trait
2324
require_once __DIR__ . '/VerifyReportedErrorTrait.php';
2425

2526
class report_errorTest extends TestCase
2627
{
28+
use TestTrait;
2729
use VerifyReportedErrorTrait;
2830

2931
public function testReportError()
3032
{
3133
$message = sprintf('Test Report Error (%s)', date('Y-m-d H:i:s'));
32-
$output = $this->runSnippet('report_error', [
34+
$output = $this->runFunctionSnippet('report_error', [
35+
self::$projectId,
3336
$message,
3437
3538
]);
@@ -40,12 +43,4 @@ public function testReportError()
4043

4144
$this->verifyReportedError(self::$projectId, $message);
4245
}
43-
44-
private function runSnippet($sampleName, $params = [])
45-
{
46-
$argv = array_merge([0, self::$projectId], $params);
47-
ob_start();
48-
require __DIR__ . "/../src/$sampleName.php";
49-
return ob_get_clean();
50-
}
5146
}

0 commit comments

Comments
 (0)