Passed
Push — master ( 97f2a2...ac92d0 )
by Christoph
14:43 queued 12s
created

ErrorHandler::onError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2016, ownCloud, Inc.
7
 *
8
 * @author Bart Visscher <[email protected]>
9
 * @author Björn Schießle <[email protected]>
10
 * @author Christoph Wurst <[email protected]>
11
 * @author Joas Schilling <[email protected]>
12
 * @author Julius Härtl <[email protected]>
13
 * @author Morris Jobke <[email protected]>
14
 * @author Thomas Müller <[email protected]>
15
 *
16
 * @license AGPL-3.0
17
 *
18
 * This code is free software: you can redistribute it and/or modify
19
 * it under the terms of the GNU Affero General Public License, version 3,
20
 * as published by the Free Software Foundation.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License, version 3,
28
 * along with this program. If not, see <http://www.gnu.org/licenses/>
29
 *
30
 */
31
namespace OC\Log;
32
33
use Error;
34
use OCP\ILogger;
35
use Psr\Log\LoggerInterface;
36
use Throwable;
37
38
class ErrorHandler {
39
	private LoggerInterface $logger;
40
41
	public function __construct(LoggerInterface $logger) {
42
		$this->logger = $logger;
43
	}
44
45
	/**
46
	 * Remove password in URLs
47
	 */
48
	private static function removePassword(string $msg): string {
49
		return preg_replace('#//(.*):(.*)@#', '//xxx:xxx@', $msg);
50
	}
51
52
	/**
53
	 * Fatal errors handler
54
	 */
55
	public function onShutdown(): void {
56
		$error = error_get_last();
57
		if ($error) {
58
			$msg = $error['message'] . ' at ' . $error['file'] . '#' . $error['line'];
59
			$this->logger->critical(self::removePassword($msg), ['app' => 'PHP']);
60
		}
61
	}
62
63
	/**
64
	 * Uncaught exception handler
65
	 */
66
	public function onException(Throwable $exception): void {
67
		$class = get_class($exception);
68
		$msg = $exception->getMessage();
69
		$msg = "$class: $msg at " . $exception->getFile() . '#' . $exception->getLine();
70
		$this->logger->critical(self::removePassword($msg), ['app' => 'PHP']);
71
	}
72
73
	/**
74
	 * Recoverable errors handler
75
	 */
76
	public function onError(int $number, string $message, string $file, int $line): bool {
77
		if (!(error_reporting() & $number)) {
78
			return true;
79
		}
80
		$msg = $message . ' at ' . $file . '#' . $line;
81
		$e = new Error(self::removePassword($msg));
82
		$this->logger->log(self::errnoToLogLevel($number), $e->getMessage(), ['app' => 'PHP']);
83
		return true;
84
	}
85
86
	/**
87
	 * Recoverable handler which catch all errors, warnings and notices
88
	 */
89
	public function onAll(int $number, string $message, string $file, int $line): bool {
90
		$msg = $message . ' at ' . $file . '#' . $line;
91
		$e = new Error(self::removePassword($msg));
92
		$this->logger->log(self::errnoToLogLevel($number), $e->getMessage(), ['app' => 'PHP']);
93
		return true;
94
	}
95
96
	private static function errnoToLogLevel(int $errno): int {
97
		switch ($errno) {
98
			case E_USER_WARNING:
99
				return ILogger::WARN;
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::WARN has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

99
				return /** @scrutinizer ignore-deprecated */ ILogger::WARN;

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
100
101
			case E_DEPRECATED:
102
			case E_USER_DEPRECATED:
103
				return ILogger::DEBUG;
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::DEBUG has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

103
				return /** @scrutinizer ignore-deprecated */ ILogger::DEBUG;

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
104
105
			case E_USER_NOTICE:
106
				return ILogger::INFO;
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::INFO has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

106
				return /** @scrutinizer ignore-deprecated */ ILogger::INFO;

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
107
108
			case E_USER_ERROR:
109
			default:
110
				return ILogger::ERROR;
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::ERROR has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

110
				return /** @scrutinizer ignore-deprecated */ ILogger::ERROR;

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
111
		}
112
	}
113
}
114