2015-10-29 12:27:14 -04:00
|
|
|
<?php
|
2024-05-28 10:42:42 -04:00
|
|
|
|
2015-10-29 12:27:14 -04:00
|
|
|
/**
|
2024-05-28 10:42:42 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-10-29 12:27:14 -04:00
|
|
|
*/
|
2016-05-09 09:09:50 -04:00
|
|
|
namespace OCA\Federation\Middleware;
|
2015-10-29 12:27:14 -04:00
|
|
|
|
2017-03-13 07:13:57 -04:00
|
|
|
use OCA\Federation\Controller\SettingsController;
|
2017-07-26 04:50:39 -04:00
|
|
|
use OCP\AppFramework\Controller;
|
2015-10-29 12:27:14 -04:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
|
use OCP\AppFramework\Middleware;
|
2021-06-29 19:20:33 -04:00
|
|
|
use OCP\HintException;
|
2015-10-29 12:27:14 -04:00
|
|
|
use OCP\IL10N;
|
2022-06-24 09:24:16 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2015-10-29 12:27:14 -04:00
|
|
|
|
|
|
|
|
class AddServerMiddleware extends Middleware {
|
2022-06-24 09:24:16 -04:00
|
|
|
protected string $appName;
|
|
|
|
|
protected IL10N $l;
|
|
|
|
|
protected LoggerInterface $logger;
|
2015-10-29 12:27:14 -04:00
|
|
|
|
2022-06-24 09:24:16 -04:00
|
|
|
public function __construct(string $appName, IL10N $l, LoggerInterface $logger) {
|
2015-10-29 12:27:14 -04:00
|
|
|
$this->appName = $appName;
|
|
|
|
|
$this->l = $l;
|
|
|
|
|
$this->logger = $logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Log error message and return a response which can be displayed to the user
|
|
|
|
|
*
|
2017-07-26 04:50:39 -04:00
|
|
|
* @param Controller $controller
|
2015-10-29 12:27:14 -04:00
|
|
|
* @param string $methodName
|
|
|
|
|
* @param \Exception $exception
|
|
|
|
|
* @return JSONResponse
|
2017-07-26 04:50:39 -04:00
|
|
|
* @throws \Exception
|
2015-10-29 12:27:14 -04:00
|
|
|
*/
|
2017-08-01 11:32:03 -04:00
|
|
|
public function afterException($controller, $methodName, \Exception $exception) {
|
2017-03-13 07:13:57 -04:00
|
|
|
if (($controller instanceof SettingsController) === false) {
|
|
|
|
|
throw $exception;
|
|
|
|
|
}
|
2022-06-24 09:24:16 -04:00
|
|
|
$this->logger->error($exception->getMessage(), [
|
2018-01-17 09:21:56 -05:00
|
|
|
'app' => $this->appName,
|
2022-06-24 09:24:16 -04:00
|
|
|
'exception' => $exception,
|
2018-01-17 09:21:56 -05:00
|
|
|
]);
|
2015-10-29 12:27:14 -04:00
|
|
|
if ($exception instanceof HintException) {
|
|
|
|
|
$message = $exception->getHint();
|
|
|
|
|
} else {
|
2016-02-09 07:58:13 -05:00
|
|
|
$message = $exception->getMessage();
|
2015-10-29 12:27:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new JSONResponse(
|
|
|
|
|
['message' => $message],
|
|
|
|
|
Http::STATUS_BAD_REQUEST
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|