1
0

AddServerMiddleware.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Federation\Middleware;
  30. use OCA\Federation\Controller\SettingsController;
  31. use OCP\AppFramework\Controller;
  32. use OCP\AppFramework\Http;
  33. use OCP\AppFramework\Http\JSONResponse;
  34. use OCP\AppFramework\Middleware;
  35. use OCP\HintException;
  36. use OCP\IL10N;
  37. use Psr\Log\LoggerInterface;
  38. class AddServerMiddleware extends Middleware {
  39. protected string $appName;
  40. protected IL10N $l;
  41. protected LoggerInterface $logger;
  42. public function __construct(string $appName, IL10N $l, LoggerInterface $logger) {
  43. $this->appName = $appName;
  44. $this->l = $l;
  45. $this->logger = $logger;
  46. }
  47. /**
  48. * Log error message and return a response which can be displayed to the user
  49. *
  50. * @param Controller $controller
  51. * @param string $methodName
  52. * @param \Exception $exception
  53. * @return JSONResponse
  54. * @throws \Exception
  55. */
  56. public function afterException($controller, $methodName, \Exception $exception) {
  57. if (($controller instanceof SettingsController) === false) {
  58. throw $exception;
  59. }
  60. $this->logger->error($exception->getMessage(), [
  61. 'app' => $this->appName,
  62. 'exception' => $exception,
  63. ]);
  64. if ($exception instanceof HintException) {
  65. $message = $exception->getHint();
  66. } else {
  67. $message = $exception->getMessage();
  68. }
  69. return new JSONResponse(
  70. ['message' => $message],
  71. Http::STATUS_BAD_REQUEST
  72. );
  73. }
  74. }