Blame view
sources/apps/contacts/lib/middleware/http.php
2.07 KB
|
d1bafeea1
|
1 2 3 4 5 6 |
<?php /** * ownCloud - HTTP Middleware * * @author Thomas Tanghus |
|
6d9380f96
|
7 |
* @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net) |
|
d1bafeea1
|
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE * License as published by the Free Software Foundation; either * version 3 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU AFFERO GENERAL PUBLIC LICENSE for more details. * * You should have received a copy of the GNU Affero General Public * License along with this library. If not, see <http://www.gnu.org/licenses/>. * */ namespace OCA\Contacts\Middleware; use OCA\Contacts\Controller, OCA\Contacts\JSONResponse, OCP\AppFramework\Middleware, |
|
6d9380f96
|
30 31 |
OCP\AppFramework\Http\Response, OCP\AppFramework\Http as HttpStatus; |
|
d1bafeea1
|
32 33 34 35 36 37 |
/**
* Used to intercept exceptions thrown in controllers and backends
* and transform them into valid HTTP responses.
*/
class Http extends Middleware {
|
|
d1bafeea1
|
38 39 40 41 42 43 44 45 46 |
/** * If an Exception is being caught, return a JSON error response with * a suitable status code * @param Controller $controller the controller that is being called * @param string $methodName the name of the method that will be called on * the controller * @param \Exception $exception the thrown exception * @return Response a Response object */ |
|
6d9380f96
|
47 48 |
public function afterException($controller, $methodName, \Exception $exception) {
\OCP\Util::writeLog('contacts', __METHOD__.' method: '.$methodName, \OCP\Util::DEBUG);
|
|
d1bafeea1
|
49 50 51 |
// If there's no proper status code associated, set it to 500.
$response = new JSONResponse();
if($exception->getCode() < 100) {
|
|
6d9380f96
|
52 |
$response->setStatus(HttpStatus::STATUS_INTERNAL_SERVER_ERROR); |
|
d1bafeea1
|
53 54 55 |
} else {
$response->setStatus($exception->getCode());
}
|
|
6d9380f96
|
56 |
|
|
d1bafeea1
|
57 |
$response->setErrorMessage($exception->getMessage()); |
|
d1bafeea1
|
58 |
|
|
6d9380f96
|
59 |
\OCP\Util::logException('contacts', $exception);
|
|
d1bafeea1
|
60 61 62 63 |
return $response; } } |