AddServerMiddleware.php 1.9 KB

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