AddServerMiddleware.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 OC\HintException;
  31. use OCA\Federation\Controller\SettingsController;
  32. use OCP\AppFramework\Controller;
  33. use OCP\AppFramework\Http;
  34. use OCP\AppFramework\Http\JSONResponse;
  35. use OCP\AppFramework\Middleware;
  36. use OCP\IL10N;
  37. use OCP\ILogger;
  38. class AddServerMiddleware extends Middleware {
  39. /** @var string */
  40. protected $appName;
  41. /** @var IL10N */
  42. protected $l;
  43. /** @var ILogger */
  44. protected $logger;
  45. /**
  46. * @param string $appName
  47. * @param IL10N $l
  48. * @param ILogger $logger
  49. */
  50. public function __construct($appName, IL10N $l, ILogger $logger) {
  51. $this->appName = $appName;
  52. $this->l = $l;
  53. $this->logger = $logger;
  54. }
  55. /**
  56. * Log error message and return a response which can be displayed to the user
  57. *
  58. * @param Controller $controller
  59. * @param string $methodName
  60. * @param \Exception $exception
  61. * @return JSONResponse
  62. * @throws \Exception
  63. */
  64. public function afterException($controller, $methodName, \Exception $exception) {
  65. if (($controller instanceof SettingsController) === false) {
  66. throw $exception;
  67. }
  68. $this->logger->logException($exception, [
  69. 'level' => ILogger::ERROR,
  70. 'app' => $this->appName,
  71. ]);
  72. if ($exception instanceof HintException) {
  73. $message = $exception->getHint();
  74. } else {
  75. $message = $exception->getMessage();
  76. }
  77. return new JSONResponse(
  78. ['message' => $message],
  79. Http::STATUS_BAD_REQUEST
  80. );
  81. }
  82. }