-
Notifications
You must be signed in to change notification settings - Fork 0
/
SentryClient.php
284 lines (255 loc) · 7 KB
/
SentryClient.php
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php declare( strict_types = 1 );
namespace CodeKandis\SentryClient;
use CodeKandis\SentryClient\Configurations\SentryClientConfigurationInterface;
use CodeKandis\SentryClient\Configurations\SentryOptionsBuilder;
use CodeKandis\SentryClient\Handlers\ErrorHandler;
use CodeKandis\SentryClient\Handlers\ErrorHandlerInterface;
use CodeKandis\SentryClient\Handlers\ThrowableHandler;
use CodeKandis\SentryClient\Handlers\ThrowableHandlerInterface;
use CodeKandis\SentryClient\Outputs\ContextOutput;
use CodeKandis\SentryClient\Outputs\ErrorOutput;
use CodeKandis\SentryClient\Outputs\MessageOutput;
use CodeKandis\SentryClient\Outputs\TagsOutput;
use CodeKandis\SentryClient\Outputs\ThrowableOutput;
use CodeKandis\SentryClient\Outputs\UserOutput;
use Sentry\ClientBuilder;
use Sentry\FlushableClientInterface;
use Sentry\SentrySdk;
use Sentry\Severity as SentrySeverity;
use Sentry\State\Hub;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Throwable;
use function error_get_last;
use function error_reporting;
use function ini_set;
/**
* Represents a `SentryClient` implementation.
* @package codekandis/sentry-client
* @author Christian Ramelow <info@codekandis.net>
*/
class SentryClient implements SentryClientInterface
{
/**
* Stores the configuration of the `SentryClient`.
* @var SentryClientConfigurationInterface
*/
private SentryClientConfigurationInterface $configuration;
/**
* Stores the initiated `Hub`.
* @var HubInterface
*/
private HubInterface $hub;
/**
* Stores the error handler.
* @var ?ErrorHandlerInterface
*/
private ?ErrorHandlerInterface $errorHandler;
/**
* Stores the throwable handler.
* @var ?ThrowableHandlerInterface
*/
private ?ThrowableHandlerInterface $throwableHandler;
/**
* Constructor method.
* @param SentryClientConfigurationInterface $configuration The configuration of the `SentryClient`.
*/
public function __construct( SentryClientConfigurationInterface $configuration )
{
$this->configuration = $configuration;
$this->setupHub();
$this->setupRuntimeErrorHandling();
}
/**
* Setup the `Sentry SDK` hub managing the error and exception handlers.
*/
private function setupHub(): void
{
$this->hub = new Hub(
ClientBuilder::create(
( new SentryOptionsBuilder() )
->buildFromClientConfiguration( $this->configuration )
)->getClient(),
new Scope()
);
}
/**
* Setup the runtime error handling.
*/
private function setupRuntimeErrorHandling(): void
{
error_reporting( $this->configuration->getErrorTypes() );
ini_set(
'display_errors',
true === $this->configuration->getDisplayErrors()
? 'On'
: 'Off'
);
}
/**
* Registers the error handler to print informations about the error.
*/
private function registerErrorHandler(): void
{
$this->errorHandler = new ErrorHandler(
function ( int $code, string $message, string $file, int $line ): void
{
$this->flushOriginClient();
if ( true === $this->configuration->getDisplayErrors() )
{
( new ErrorOutput() )
->print( Severities::INFO, $code, $message, $file, $line );
}
}
);
$this->errorHandler->register();
}
/**
* Registers the throwable handler to print informations about the throwable.
*/
private function registerThrowableHandler(): void
{
$this->throwableHandler = new ThrowableHandler(
function ( Throwable $throwable ): void
{
$this->flushOriginClient();
if ( true === $this->configuration->getDisplayErrors() )
{
( new ThrowableOutput() )
->print( Severities::ERROR, $throwable );
}
}
);
$this->throwableHandler->register();
}
/**
* Sets the scope of the event to capture.
* @param string $severity The severity of the event.
* @param array $context The context of the event.
* @param array $tags The additional tags of the event.
* @param array $user The user causing the event.
*/
private function setScope( string $severity, array $context, array $tags, array $user ): void
{
$this->hub->configureScope(
static function ( Scope $scope ) use ( $severity, $context, $tags, $user )
{
$scope->setLevel( new SentrySeverity( $severity ) );
$scope->setExtras( $context );
$scope->setTags( $tags );
$scope->setUser( $user, true );
}
);
}
/**
* Flushes the origin client.
*/
private function flushOriginClient(): void
{
$client = $this->hub->getClient();
if ( $client instanceof FlushableClientInterface )
{
$client->flush();
}
}
/**
* {@inheritdoc}
*/
public function register(): void
{
SentrySdk::setCurrentHub( $this->hub );
$this->registerErrorHandler();
$this->registerThrowableHandler();
}
/**
* {@inheritdoc}
*/
public function getErrorHandler(): ?ErrorHandlerInterface
{
return $this->errorHandler;
}
/**
* {@inheritdoc}
*/
public function getThrowableHandler(): ?ThrowableHandlerInterface
{
return $this->throwableHandler;
}
/**
* {@inheritdoc}
*/
public function captureMessage( string $message, string $severity = Severities::INFO, array $context = [], array $tags = [], array $user = [] ): ?string
{
$this->setScope( $severity, $context, $tags, $user );
$sentryEventId = $this->hub->captureMessage( $message, new SentrySeverity( $severity ) );
$this->flushOriginClient();
if ( true === $this->configuration->getDisplayErrors() )
{
( new MessageOutput() )
->print( $severity, $message );
( new ContextOutput() )
->print( $context );
( new TagsOutput() )
->print( $tags );
( new UserOutput() )
->print( $user );
}
return $sentryEventId;
}
/**
* {@inheritdoc}
*/
public function captureLastError( string $severity = Severities::ERROR, array $context = [], array $tags = [], array $user = [] ): ?string
{
$this->setScope( $severity, $context, $tags, $user );
$sentryEventId = $this->hub->captureLastError();
$this->flushOriginClient();
$lastError = error_get_last();
if ( true === $this->configuration->getDisplayErrors() && null !== $lastError )
{
( new ErrorOutput() )
->print(
$severity,
$lastError[ 'type' ],
$lastError[ 'message' ],
$lastError[ 'file' ],
$lastError[ 'line' ]
);
( new ContextOutput() )
->print( $context );
( new TagsOutput() )
->print( $tags );
( new UserOutput() )
->print( $user );
}
return $sentryEventId;
}
/**
* {@inheritdoc}
*/
public function captureThrowable( Throwable $throwable, string $severity = Severities::ERROR, array $context = [], array $tags = [], array $user = [] ): ?string
{
$this->setScope( $severity, $context, $tags, $user );
$sentryEventId = $this->hub->captureException( $throwable );
$this->flushOriginClient();
if ( true === $this->configuration->getDisplayErrors() )
{
( new ThrowableOutput() )
->print( $severity, $throwable );
( new ContextOutput() )
->print( $context );
( new TagsOutput() )
->print( $tags );
( new UserOutput() )
->print( $user );
}
return $sentryEventId;
}
/**
* {@inheritdoc}
*/
public function getLastEventId(): ?string
{
return $this->hub->getLastEventId();
}
}