Repository navigation
Expand file tree
/
Copy patherror_reporting.php
More file actions
175 lines (162 loc) · 5.87 KB
/
Copy patherror_reporting.php
File metadata and controls
175 lines (162 loc) · 5.87 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Samples\ErrorReporting;
use Exception;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputDefinition;
# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';
$application = new Application('Stackdriver Error Reporting');
$inputDefinition = new InputDefinition([
new InputArgument('project_id', InputArgument::REQUIRED, 'The project id'),
new InputArgument('message', InputArgument::OPTIONAL, 'The error message', 'My Error Message'),
]);
$application->add(new Command('report-simple'))
->setDefinition(clone $inputDefinition)
->setDescription('Reports a simple error message.')
->setCode(function ($input, $output) {
$projectId = $input->getArgument('project_id');
$message = $input->getArgument('message');
require_once __DIR__ . '/src/report_error_simple.php';
});
$application->add(new Command('report'))
->setDefinition(clone $inputDefinition)
->addOption(
'user',
'',
InputOption::VALUE_REQUIRED,
'The user attributed to the error.',
'test@user.com'
)
->addOption(
'service',
'',
InputOption::VALUE_REQUIRED,
'The service where the error occurred.',
'service'
)
->addOption(
'app-version',
'',
InputOption::VALUE_REQUIRED,
'The version for which the error occurred.',
'version'
)
->setDescription('Reports an error message with context and user data.')
->setCode(function ($input, $output) {
$projectId = $input->getArgument('project_id');
$message = $input->getArgument('message');
$user = $input->getOption('user');
$service = $input->getOption('service');
$version = $input->getOption('app-version');
require_once __DIR__ . '/src/report_error_manually.php';
});
$application->add(new Command('report-grpc'))
->setDefinition(clone $inputDefinition)
->setDescription('Reports a custom error object using gRPC.')
->addOption(
'user',
'',
InputOption::VALUE_REQUIRED,
'The user attributed to the error.',
'test@user.com'
)
->addOption(
'with-stacktrace',
'',
InputOption::VALUE_NONE,
'Include a stack trace in the error.'
)
->setCode(function ($input, $output) {
$projectId = $input->getArgument('project_id');
$message = $input->getArgument('message');
$user = $input->getOption('user');
if ($input->getOption('with-stacktrace')) {
# [START message-to-exception]
$message = (string) new Exception($message);
# [END message-to-exception]
}
require_once __DIR__ . '/src/report_error_grpc.php';
});
$application->add(new Command('test-exception-handler'))
->setDescription('Reports an exception using an exception handler.')
->addArgument(
'project_id',
InputArgument::REQUIRED,
'Type of error to test: "exception", "error", or "fatal".'
)
->addArgument(
'message',
InputArgument::OPTIONAL,
'The error message.'
)
->addOption(
'type',
'',
InputOption::VALUE_REQUIRED,
'Type of error to test: "exception", "error", or "fatal".',
'exception'
)
->addOption(
'service',
'',
InputOption::VALUE_REQUIRED,
'The service where the error occurred.',
'service'
)
->addOption(
'app-version',
'',
InputOption::VALUE_REQUIRED,
'The version for which the error occurred.',
'version'
)
->setCode(function ($input, $output) use ($application) {
$errorType = $input->getOption('type');
if (!in_array($errorType, ['exception', 'error', 'fatal'])) {
throw new \InvalidArgumentException('Invalid error type provided, '
. 'must be one one of "exception", "error", or "fatal".');
}
$projectId = $input->getArgument('project_id');
$service = $input->getOption('service');
$version = $input->getOption('app-version');
require_once __DIR__ . '/src/register_exception_handler.php';
// disable Console Application exception handlers
$application->setCatchExceptions(false);
// throw a test exception to trigger our exception handler
$message = $input->getArgument('message');
switch ($errorType) {
case 'exception':
print('Throwing a PHP Exception...' . PHP_EOL);
throw new \Exception($message ?: 'This is from "throw new Exception()"');
case 'fatal':
print('Triggering a PHP Fatal Error by eval-ing a syntax error...' . PHP_EOL);
eval('syntax-error');
break;
case 'error':
print('Triggering a PHP Error' . PHP_EOL);
trigger_error($message ?: 'This is from "trigger_error()"', E_USER_ERROR);
}
});
// for testing
if (getenv('PHPUNIT_TESTS') === '1') {
return $application;
}
$application->run();