|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author Christoph Wurst <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @license GNU AGPL version 3 or any later version |
|
9
|
|
|
* |
|
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
13
|
|
|
* License, or (at your option) any later version. |
|
14
|
|
|
* |
|
15
|
|
|
* This program is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU Affero General Public License for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace OCA\Sentry\Reporter; |
|
26
|
|
|
|
|
27
|
|
|
use Exception; |
|
28
|
|
|
use OCP\IConfig; |
|
29
|
|
|
use OCP\ILogger; |
|
30
|
|
|
use OCP\IUserSession; |
|
31
|
|
|
use OCP\Support\CrashReport\IReporter; |
|
32
|
|
|
use Raven_Client; |
|
33
|
|
|
use Throwable; |
|
34
|
|
|
|
|
35
|
|
|
class SentryReporterSimpleAdapter implements IReporter { |
|
36
|
|
|
|
|
37
|
|
|
/** @var IUserSession */ |
|
38
|
|
|
protected $userSession; |
|
39
|
|
|
|
|
40
|
|
|
/** @var Raven_Client */ |
|
41
|
|
|
protected $client; |
|
42
|
|
|
|
|
43
|
|
|
/** @var array mapping of log levels */ |
|
44
|
|
|
protected $levels = [ |
|
45
|
|
|
ILogger::DEBUG => 'debug', |
|
46
|
|
|
ILogger::INFO => 'info', |
|
47
|
|
|
ILogger::WARN => 'warning', |
|
48
|
|
|
ILogger::ERROR => 'error', |
|
49
|
|
|
ILogger::FATAL => 'fatal', |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
/** @var int */ |
|
53
|
|
|
protected $minimumLogLevel; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param Raven_Client $client |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct(Raven_Client $client, IUserSession $userSession, IConfig $config) { |
|
59
|
|
|
$this->client = $client; |
|
60
|
|
|
$this->userSession = $userSession; |
|
61
|
|
|
$this->minimumLogLevel = (int)$config->getSystemValue('sentry.minimum.log.level', ILogger::WARN); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Report an (unhandled) exception to Sentry |
|
66
|
|
|
* |
|
67
|
|
|
* @param Exception|Throwable $exception |
|
68
|
|
|
* @param array $context |
|
69
|
|
|
*/ |
|
70
|
|
|
public function report($exception, array $context = []) { |
|
71
|
|
|
if (isset($context['level']) |
|
72
|
|
|
&& $context['level'] < $this->minimumLogLevel) { |
|
73
|
|
|
// TODO: report as breadcrumb instead? |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$sentryContext = $this->buildSentryContext($context); |
|
78
|
|
|
|
|
79
|
|
|
$this->client->captureException($exception, $sentryContext); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function buildSentryContext(array $context) { |
|
83
|
|
|
$sentryContext = []; |
|
84
|
|
|
$sentryContext['tags'] = []; |
|
85
|
|
|
|
|
86
|
|
|
if (isset($context['level'])) { |
|
87
|
|
|
$sentryContext['level'] = $this->levels[$context['level']]; |
|
88
|
|
|
} |
|
89
|
|
|
if (isset($context['app'])) { |
|
90
|
|
|
$sentryContext['tags']['app'] = $context['app']; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$user = $this->userSession->getUser(); |
|
94
|
|
|
if (!is_null($user)) { |
|
95
|
|
|
$sentryContext['user'] = [ |
|
96
|
|
|
'id' => $user->getUID(), |
|
97
|
|
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
return $sentryContext; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|