AddServerMiddleware.php 2.4 KB

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