You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.8 KiB

9 years ago
9 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\DAV\Connector\Sabre;
  27. use OCA\DAV\Connector\Sabre\Exception\PasswordLoginForbidden;
  28. use OCP\Files\StorageNotAvailableException;
  29. use OCP\ILogger;
  30. use Sabre\DAV\Exception\Conflict;
  31. use Sabre\DAV\Exception\Forbidden;
  32. use Sabre\DAV\Exception\InvalidSyncToken;
  33. use Sabre\DAV\Exception\NotAuthenticated;
  34. use Sabre\DAV\Exception\NotFound;
  35. use Sabre\DAV\Exception\NotImplemented;
  36. use Sabre\DAV\Exception\PreconditionFailed;
  37. use Sabre\DAV\Exception\ServiceUnavailable;
  38. class ExceptionLoggerPlugin extends \Sabre\DAV\ServerPlugin {
  39. protected $nonFatalExceptions = [
  40. NotAuthenticated::class => true,
  41. // If tokenauth can throw this exception (which is basically as
  42. // NotAuthenticated. So not fatal.
  43. PasswordLoginForbidden::class => true,
  44. // basically a NotAuthenticated
  45. InvalidSyncToken::class => true,
  46. // the sync client uses this to find out whether files exist,
  47. // so it is not always an error, log it as debug
  48. NotFound::class => true,
  49. // this one mostly happens when the same file is uploaded at
  50. // exactly the same time from two clients, only one client
  51. // wins, the second one gets "Precondition failed"
  52. PreconditionFailed::class => true,
  53. // forbidden can be expected when trying to upload to
  54. // read-only folders for example
  55. Forbidden::class => true,
  56. // Happens when an external storage or federated share is temporarily
  57. // not available
  58. StorageNotAvailableException::class => true,
  59. // happens if some a client uses the wrong method for a given URL
  60. // the error message itself is visible on the client side anyways
  61. NotImplemented::class => true,
  62. // happens when the parent directory is not present (for example when a
  63. // move is done to a non-existent directory)
  64. Conflict::class => true,
  65. ];
  66. /** @var string */
  67. private $appName;
  68. /** @var ILogger */
  69. private $logger;
  70. /**
  71. * @param string $loggerAppName app name to use when logging
  72. * @param ILogger $logger
  73. */
  74. public function __construct($loggerAppName, $logger) {
  75. $this->appName = $loggerAppName;
  76. $this->logger = $logger;
  77. }
  78. /**
  79. * This initializes the plugin.
  80. *
  81. * This function is called by \Sabre\DAV\Server, after
  82. * addPlugin is called.
  83. *
  84. * This method should set up the required event subscriptions.
  85. *
  86. * @param \Sabre\DAV\Server $server
  87. * @return void
  88. */
  89. public function initialize(\Sabre\DAV\Server $server) {
  90. $server->on('exception', array($this, 'logException'), 10);
  91. }
  92. /**
  93. * Log exception
  94. *
  95. */
  96. public function logException(\Exception $ex) {
  97. $exceptionClass = get_class($ex);
  98. $level = \OCP\Util::FATAL;
  99. if (isset($this->nonFatalExceptions[$exceptionClass]) ||
  100. (
  101. $exceptionClass === ServiceUnavailable::class &&
  102. $ex->getMessage() === 'System in maintenance mode.'
  103. )
  104. ) {
  105. $level = \OCP\Util::DEBUG;
  106. }
  107. $this->logger->logException($ex, [
  108. 'app' => $this->appName,
  109. 'level' => $level,
  110. ]);
  111. }
  112. }