AddServerMiddleware.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. public function __construct(
  18. protected string $appName,
  19. protected IL10N $l,
  20. protected LoggerInterface $logger,
  21. ) {
  22. }
  23. /**
  24. * Log error message and return a response which can be displayed to the user
  25. *
  26. * @param Controller $controller
  27. * @param string $methodName
  28. * @param \Exception $exception
  29. * @return JSONResponse
  30. * @throws \Exception
  31. */
  32. public function afterException($controller, $methodName, \Exception $exception) {
  33. if (($controller instanceof SettingsController) === false) {
  34. throw $exception;
  35. }
  36. $this->logger->error($exception->getMessage(), [
  37. 'app' => $this->appName,
  38. 'exception' => $exception,
  39. ]);
  40. if ($exception instanceof HintException) {
  41. $message = $exception->getHint();
  42. } else {
  43. $message = $exception->getMessage();
  44. }
  45. return new JSONResponse(
  46. ['message' => $message],
  47. Http::STATUS_BAD_REQUEST
  48. );
  49. }
  50. }