1
0

AddServerMiddleware.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Federation\Middleware;
  8. use OCA\Federation\Controller\SettingsController;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\JSONResponse;
  12. use OCP\AppFramework\Middleware;
  13. use OCP\HintException;
  14. use OCP\IL10N;
  15. use Psr\Log\LoggerInterface;
  16. class AddServerMiddleware extends Middleware {
  17. protected string $appName;
  18. protected IL10N $l;
  19. protected LoggerInterface $logger;
  20. public function __construct(string $appName, IL10N $l, LoggerInterface $logger) {
  21. $this->appName = $appName;
  22. $this->l = $l;
  23. $this->logger = $logger;
  24. }
  25. /**
  26. * Log error message and return a response which can be displayed to the user
  27. *
  28. * @param Controller $controller
  29. * @param string $methodName
  30. * @param \Exception $exception
  31. * @return JSONResponse
  32. * @throws \Exception
  33. */
  34. public function afterException($controller, $methodName, \Exception $exception) {
  35. if (($controller instanceof SettingsController) === false) {
  36. throw $exception;
  37. }
  38. $this->logger->error($exception->getMessage(), [
  39. 'app' => $this->appName,
  40. 'exception' => $exception,
  41. ]);
  42. if ($exception instanceof HintException) {
  43. $message = $exception->getHint();
  44. } else {
  45. $message = $exception->getMessage();
  46. }
  47. return new JSONResponse(
  48. ['message' => $message],
  49. Http::STATUS_BAD_REQUEST
  50. );
  51. }
  52. }