Completed
Push — master ( d8af9d...efa52e )
by Morris
28:06 queued 11:26
created

ExceptionLoggerPlugin::logException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Morris Jobke <[email protected]>
6
 * @author Pierre Jochem <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 * @author Roeland Jago Douma <[email protected]>
9
 * @author Thomas Müller <[email protected]>
10
 * @author Vincent Petry <[email protected]>
11
 *
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
28
namespace OCA\DAV\Connector\Sabre;
29
30
use OCP\ILogger;
31
use Sabre\DAV\Exception;
32
use Sabre\HTTP\Response;
33
34
class ExceptionLoggerPlugin extends \Sabre\DAV\ServerPlugin {
35
	protected $nonFatalExceptions = [
36
		'Sabre\DAV\Exception\NotAuthenticated' => true,
37
		// If tokenauth can throw this exception (which is basically as
38
		// NotAuthenticated. So not fatal.
39
		'OCA\DAV\Connector\Sabre\Exception\PasswordLoginForbidden' => true,
40
		// the sync client uses this to find out whether files exist,
41
		// so it is not always an error, log it as debug
42
		'Sabre\DAV\Exception\NotFound' => true,
43
		// this one mostly happens when the same file is uploaded at
44
		// exactly the same time from two clients, only one client
45
		// wins, the second one gets "Precondition failed"
46
		'Sabre\DAV\Exception\PreconditionFailed' => true,
47
		// forbidden can be expected when trying to upload to
48
		// read-only folders for example
49
		'Sabre\DAV\Exception\Forbidden' => true,
50
		// Happens when an external storage or federated share is temporarily
51
		// not available
52
		'Sabre\DAV\Exception\StorageNotAvailableException' => true,
53
	];
54
55
	/** @var string */
56
	private $appName;
57
58
	/** @var ILogger */
59
	private $logger;
60
61
	/**
62
	 * @param string $loggerAppName app name to use when logging
63
	 * @param ILogger $logger
64
	 */
65
	public function __construct($loggerAppName, $logger) {
66
		$this->appName = $loggerAppName;
67
		$this->logger = $logger;
68
	}
69
70
	/**
71
	 * This initializes the plugin.
72
	 *
73
	 * This function is called by \Sabre\DAV\Server, after
74
	 * addPlugin is called.
75
	 *
76
	 * This method should set up the required event subscriptions.
77
	 *
78
	 * @param \Sabre\DAV\Server $server
79
	 * @return void
80
	 */
81
	public function initialize(\Sabre\DAV\Server $server) {
82
83
		$server->on('exception', array($this, 'logException'), 10);
84
	}
85
86
	/**
87
	 * Log exception
88
	 *
89
	 */
90
	public function logException(\Exception $ex) {
91
		$exceptionClass = get_class($ex);
92
		$level = \OCP\Util::FATAL;
93
		if (isset($this->nonFatalExceptions[$exceptionClass])) {
94
			$level = \OCP\Util::DEBUG;
95
		}
96
97
		$this->logger->logException($ex, [
98
			'app' => $this->appName,
99
			'level' => $level,
100
		]);
101
	}
102
}
103