ExceptionMiddleware::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Deck\Middleware;
25
26
use OCA\Deck\Controller\PageController;
27
use OCA\Deck\StatusException;
28
use OCP\AppFramework\Db\DoesNotExistException;
29
use OCP\AppFramework\Middleware;
30
use OCP\AppFramework\Http\JSONResponse;
31
use OCP\ILogger;
32
use OCP\Util;
33
use OCP\IConfig;
34
35
36
class ExceptionMiddleware extends Middleware {
37
38
	/** @var ILogger */
39
	private $logger;
40
	/** @var IConfig */
41
	private $config;
42
43
	/**
44
	 * SharingMiddleware constructor.
45
	 *
46
	 * @param ILogger $logger
47
	 * @param IConfig $config
48
	 */
49
	public function __construct(ILogger $logger, IConfig $config) {
50
		$this->logger = $logger;
51
		$this->config = $config;
52
	}
53
54
	/**
55
	 * Return JSON error response if the user has no sufficient permission
56
	 *
57
	 * @param \OCP\AppFramework\Controller $controller
58
	 * @param string $methodName
59
	 * @param \Exception $exception
60
	 * @return JSONResponse
61
	 * @throws \Exception
62
	 */
63
	public function afterException($controller, $methodName, \Exception $exception) {
64
		if ($exception instanceof StatusException) {
65
			if ($this->config->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) {
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\Util::WARN has been deprecated with message: 14.0.0 use \OCP\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...
Deprecated Code introduced by
The constant OCP\Util::DEBUG has been deprecated with message: 14.0.0 use \OCP\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...
66
				$this->logger->logException($exception);
0 ignored issues
show
Documentation introduced by
$exception is of type object<OCA\Deck\StatusException>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
			}
68
			return new JSONResponse([
69
				'status' => $exception->getStatus(),
70
				'message' => $exception->getMessage()
71
			], $exception->getStatus());
72
		}
73
74
		if (strpos(get_class($controller), 'OCA\\Deck\\Controller\\') === 0) {
75
			$response = [
76
				'status' => 500,
77
				'message' => $exception->getMessage()
78
			];
79
			if ($this->config->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) {
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\Util::WARN has been deprecated with message: 14.0.0 use \OCP\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...
Deprecated Code introduced by
The constant OCP\Util::DEBUG has been deprecated with message: 14.0.0 use \OCP\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...
80
				$this->logger->logException($exception);
0 ignored issues
show
Documentation introduced by
$exception is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
			}
82
			if ($this->config->getSystemValue('debug', true) === true) {
83
				$response['exception'] = (array) $exception;
84
			}
85
			return new JSONResponse($response, 500);
86
		}
87
88
		// uncatched DoesNotExistExceptions will be thrown when the main entity is not found
89
		// we return a 403 so we don't leak information over existing entries
90
		// TODO: At some point those should properly be catched in the service classes
91
		if ($exception instanceof DoesNotExistException) {
92
			return new JSONResponse([
93
				'status' => 403,
94
				'message' => 'Permission denied'
95
			], 403);
96
		}
97
98
		throw $exception;
99
	}
100
101
}
102