blob: b80deb92e07e5f4ede2a0d16916204dfbd417904 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
<?php
/**
* ownCloud - gallery
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Bernhard Posselt <dev@bernhard-posselt.com>
* @author Olivier Paroz <owncloud@interfasys.ch>
*
* @copyright Bernhard Posselt 2014-2015
* @copyright Olivier Paroz 2014-2015
*/
namespace OCA\Gallery\Controller;
use Exception;
use OCP\IURLGenerator;
use OCP\ISession;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\RedirectResponse;
use OCA\Gallery\Environment\NotFoundEnvException;
use OCA\Gallery\Service\NotFoundServiceException;
use OCA\Gallery\Service\ForbiddenServiceException;
/**
* Our classes extend both Controller and ApiController, so we need to use
* traits to add some common methods
*
* @package OCA\Gallery\Controller
*/
trait HttpError {
/**
* @param \Exception $exception
*
* @return JSONResponse
*/
public function jsonError(Exception $exception) {
$message = $exception->getMessage();
$code = $this->getHttpStatusCode($exception);
return new JSONResponse(
[
'message' => $message . ' (' . $code . ')',
'success' => false
],
$code
);
}
/**
* @param ISession $session
* @param IURLGenerator $urlGenerator
* @param string $appName
* @param \Exception $exception
*
* @return RedirectResponse
*/
public function htmlError($session, $urlGenerator, $appName, Exception $exception) {
$message = $exception->getMessage();
$code = $this->getHttpStatusCode($exception);
$session->set('galleryErrorMessage', $message);
$url = $urlGenerator->linkToRoute(
$appName . '.page.error_page', ['code' => $code]
);
return new RedirectResponse($url);
}
/**
* Returns an error array
*
* @param $exception
*
* @return array<null|int|string>
*/
public function getHttpStatusCode($exception) {
$code = Http::STATUS_INTERNAL_SERVER_ERROR;
if ($exception instanceof NotFoundServiceException
|| $exception instanceof NotFoundEnvException
) {
$code = Http::STATUS_NOT_FOUND;
}
if ($exception instanceof ForbiddenServiceException) {
$code = Http::STATUS_FORBIDDEN;
}
return $code;
}
}
|