ExceptionLoggerPlugin.php 4.2 KB

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