Issues (263)

lib/Controller/HttpError.php (7 issues)

1
<?php
2
/**
3
 * Nextcloud - Gallery
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Bernhard Posselt <[email protected]>
9
 * @author Olivier Paroz <[email protected]>
10
 *
11
 * @copyright Bernhard Posselt 2017
12
 * @copyright Olivier Paroz 2017
13
 */
14
15
namespace OCA\Gallery\Controller;
16
17
use Exception;
18
19
use OCP\ILogger;
0 ignored issues
show
The type OCP\ILogger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use OCP\IRequest;
0 ignored issues
show
The type OCP\IRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use OCP\IURLGenerator;
0 ignored issues
show
The type OCP\IURLGenerator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
23
use OCP\AppFramework\Http;
0 ignored issues
show
The type OCP\AppFramework\Http was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use OCP\AppFramework\Http\JSONResponse;
0 ignored issues
show
The type OCP\AppFramework\Http\JSONResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use OCP\AppFramework\Http\RedirectResponse;
0 ignored issues
show
The type OCP\AppFramework\Http\RedirectResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
27
use OCA\Gallery\Environment\NotFoundEnvException;
28
use OCA\Gallery\Service\NotFoundServiceException;
29
use OCA\Gallery\Service\ForbiddenServiceException;
30
31
/**
32
 * Our classes extend both Controller and ApiController, so we need to use
33
 * traits to add some common methods
34
 *
35
 * @package OCA\Gallery\Controller
36
 */
37
trait HttpError {
38
39
	/**
40
	 * @param \Exception $exception
41
	 * @param IRequest $request
42
	 * @param ILogger $logger
43
	 *
44
	 * @return JSONResponse
45
	 */
46
	public function jsonError(Exception $exception,
47
							  IRequest $request,
48
							  ILogger $logger) {
49
		$code = $this->getHttpStatusCode($exception);
50
51
		// If the exception is not of type ForbiddenServiceException only show a
52
		// generic error message to avoid leaking information.
53
		if(!($exception instanceof ForbiddenServiceException)) {
54
			$logger->logException($exception, ['app' => 'gallery']);
55
			$message = sprintf('An error occurred. Request ID: %s', $request->getId());
56
		} else {
57
			$message = $exception->getMessage() . ' (' . $code . ')';
0 ignored issues
show
Are you sure $code of type array<mixed,integer|null|string> can be used in concatenation? ( Ignorable by Annotation )

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

57
			$message = $exception->getMessage() . ' (' . /** @scrutinizer ignore-type */ $code . ')';
Loading history...
58
		}
59
60
		return new JSONResponse(
61
			[
62
				'message' => $message,
63
				'success' => false,
64
			],
65
			$code
66
		);
67
	}
68
69
	/**
70
	 * @param IURLGenerator $urlGenerator
71
	 * @param string $appName
72
	 * @param \Exception $exception
73
	 *
74
	 * @return RedirectResponse
75
	 */
76
	public function htmlError($urlGenerator, $appName, Exception $exception) {
77
		$message = $exception->getMessage();
78
		$code = $this->getHttpStatusCode($exception);
79
		$url = $urlGenerator->linkToRoute(
80
			$appName . '.page.error_page', ['code' => $code]
81
		);
82
83
		$response = new RedirectResponse($url);
84
		$response->addCookie('galleryErrorMessage', $message);
85
86
		return $response;
87
	}
88
89
	/**
90
	 * Returns an error array
91
	 *
92
	 * @param $exception
93
	 *
94
	 * @return array<null|int|string>
95
	 */
96
	public function getHttpStatusCode($exception) {
97
		$code = Http::STATUS_INTERNAL_SERVER_ERROR;
98
		if ($exception instanceof NotFoundServiceException
99
			|| $exception instanceof NotFoundEnvException
100
		) {
101
			$code = Http::STATUS_NOT_FOUND;
102
		}
103
		if ($exception instanceof ForbiddenServiceException) {
104
			$code = Http::STATUS_FORBIDDEN;
105
		}
106
107
		return $code;
108
	}
109
}
110