ExceptionLoggerPlugin   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 77
ccs 0
cts 31
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A initialize() 0 4 1
B logException() 0 26 4
1
<?php
2
/**
3
 * @author Morris Jobke <[email protected]>
4
 * @author Robin Appelman <[email protected]>
5
 * @author Thomas Müller <[email protected]>
6
 * @author Vincent Petry <[email protected]>
7
 *
8
 * @copyright Copyright (c) 2015, ownCloud, Inc.
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
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, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\Contacts\Sabre;
26
27
use OCP\ILogger;
28
use Sabre\DAV\Exception;
29
use Sabre\HTTP\Response;
30
31
class ExceptionLoggerPlugin extends \Sabre\DAV\ServerPlugin {
32
	protected $nonFatalExceptions = array(
33
		'Sabre\DAV\Exception\NotAuthenticated' => true,
34
		// the sync client uses this to find out whether files exist,
35
		// so it is not always an error, log it as debug
36
		'Sabre\DAV\Exception\NotFound' => true,
37
		// this one mostly happens when the same file is uploaded at
38
		// exactly the same time from two clients, only one client
39
		// wins, the second one gets "Precondition failed"
40
		'Sabre\DAV\Exception\PreconditionFailed' => true,
41
		// forbidden can be expected when trying to upload to
42
		// read-only folders for example
43
		'Sabre\DAV\Exception\Forbidden' => true,
44
	);
45
46
	/** @var string */
47
	private $appName;
48
49
	/** @var ILogger */
50
	private $logger;
51
52
	/**
53
	 * @param string $loggerAppName app name to use when logging
54
	 * @param ILogger $logger
55
	 */
56
	public function __construct($loggerAppName, $logger) {
57
		$this->appName = $loggerAppName;
58
		$this->logger = $logger;
59
	}
60
61
	/**
62
	 * This initializes the plugin.
63
	 *
64
	 * This function is called by \Sabre\DAV\Server, after
65
	 * addPlugin is called.
66
	 *
67
	 * This method should set up the required event subscriptions.
68
	 *
69
	 * @param \Sabre\DAV\Server $server
70
	 * @return void
71
	 */
72
	public function initialize(\Sabre\DAV\Server $server) {
73
74
		$server->on('exception', array($this, 'logException'), 10);
75
	}
76
77
	/**
78
	 * Log exception
79
	 *
80
	 */
81
	public function logException(\Exception $ex) {
82
		$exceptionClass = get_class($ex);
83
		$level = \OCP\Util::FATAL;
84
		if (isset($this->nonFatalExceptions[$exceptionClass])) {
85
			$level = \OCP\Util::DEBUG;
86
		}
87
88
		$message = $ex->getMessage();
89
		if ($ex instanceof Exception) {
0 ignored issues
show
Bug introduced by
The class Sabre\DAV\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
90
			if (empty($message)) {
91
				$response = new Response($ex->getHTTPCode());
92
				$message = $response->getStatusText();
93
			}
94
			$message = "HTTP/1.1 {$ex->getHTTPCode()} $message";
95
		}
96
97
		$exception = [
98
			'Message' => $message,
99
			'Exception' => $exceptionClass,
100
			'Code' => $ex->getCode(),
101
			'Trace' => $ex->getTraceAsString(),
102
			'File' => $ex->getFile(),
103
			'Line' => $ex->getLine(),
104
		];
105
		$this->logger->log($level, 'Exception: ' . json_encode($exception), ['app' => $this->appName]);
106
	}
107
}
108