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.

ExceptionLoggerPlugin.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <vincent@nextcloud.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 Psr\Log\LoggerInterface;
  33. use Sabre\DAV\Exception\BadRequest;
  34. use Sabre\DAV\Exception\Conflict;
  35. use Sabre\DAV\Exception\Forbidden;
  36. use Sabre\DAV\Exception\InvalidSyncToken;
  37. use Sabre\DAV\Exception\MethodNotAllowed;
  38. use Sabre\DAV\Exception\NotAuthenticated;
  39. use Sabre\DAV\Exception\NotFound;
  40. use Sabre\DAV\Exception\NotImplemented;
  41. use Sabre\DAV\Exception\PreconditionFailed;
  42. use Sabre\DAV\Exception\RequestedRangeNotSatisfiable;
  43. use Sabre\DAV\Exception\ServiceUnavailable;
  44. class ExceptionLoggerPlugin extends \Sabre\DAV\ServerPlugin {
  45. protected $nonFatalExceptions = [
  46. NotAuthenticated::class => true,
  47. // If tokenauth can throw this exception (which is basically as
  48. // NotAuthenticated. So not fatal.
  49. PasswordLoginForbidden::class => true,
  50. // basically a NotAuthenticated
  51. InvalidSyncToken::class => true,
  52. // the sync client uses this to find out whether files exist,
  53. // so it is not always an error, log it as debug
  54. NotFound::class => true,
  55. // the sync client messed up their request
  56. // (e.g. propfind for tags with string instead of int)
  57. // so it is not always an error, log it as debug
  58. BadRequest::class => true,
  59. // this one mostly happens when the same file is uploaded at
  60. // exactly the same time from two clients, only one client
  61. // wins, the second one gets "Precondition failed"
  62. PreconditionFailed::class => true,
  63. // forbidden can be expected when trying to upload to
  64. // read-only folders for example
  65. Forbidden::class => true,
  66. // Happens when an external storage or federated share is temporarily
  67. // not available
  68. StorageNotAvailableException::class => true,
  69. // happens if some a client uses the wrong method for a given URL
  70. // the error message itself is visible on the client side anyways
  71. NotImplemented::class => true,
  72. // happens when the parent directory is not present (for example when a
  73. // move is done to a non-existent directory)
  74. Conflict::class => true,
  75. // happens when a certain method is not allowed to be called
  76. // for example creating a folder that already exists
  77. MethodNotAllowed::class => true,
  78. // A locked file is perfectly valid and can happen in various cases
  79. FileLocked::class => true,
  80. // An invalid range is requested
  81. RequestedRangeNotSatisfiable::class => true,
  82. ];
  83. private string $appName;
  84. private LoggerInterface $logger;
  85. /**
  86. * @param string $loggerAppName app name to use when logging
  87. */
  88. public function __construct(string $loggerAppName, LoggerInterface $logger) {
  89. $this->appName = $loggerAppName;
  90. $this->logger = $logger;
  91. }
  92. /**
  93. * This initializes the plugin.
  94. *
  95. * This function is called by \Sabre\DAV\Server, after
  96. * addPlugin is called.
  97. *
  98. * This method should set up the required event subscriptions.
  99. *
  100. * @param \Sabre\DAV\Server $server
  101. * @return void
  102. */
  103. public function initialize(\Sabre\DAV\Server $server) {
  104. $server->on('exception', [$this, 'logException'], 10);
  105. }
  106. /**
  107. * Log exception
  108. */
  109. public function logException(\Throwable $ex) {
  110. $exceptionClass = get_class($ex);
  111. if (isset($this->nonFatalExceptions[$exceptionClass]) ||
  112. (
  113. $exceptionClass === ServiceUnavailable::class &&
  114. $ex->getMessage() === 'System in maintenance mode.'
  115. )
  116. ) {
  117. $this->logger->debug($ex->getMessage(), [
  118. 'app' => $this->appName,
  119. 'exception' => $ex,
  120. ]);
  121. return;
  122. }
  123. $this->logger->critical($ex->getMessage(), [
  124. 'app' => $this->appName,
  125. 'exception' => $ex,
  126. ]);
  127. }
  128. }