ExceptionLoggerPlugin.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 OCA\DAV\Exception\ServerMaintenanceMode;
  32. use OCP\Files\StorageNotAvailableException;
  33. use Psr\Log\LoggerInterface;
  34. use Sabre\DAV\Exception\BadRequest;
  35. use Sabre\DAV\Exception\Conflict;
  36. use Sabre\DAV\Exception\Forbidden;
  37. use Sabre\DAV\Exception\InvalidSyncToken;
  38. use Sabre\DAV\Exception\MethodNotAllowed;
  39. use Sabre\DAV\Exception\NotAuthenticated;
  40. use Sabre\DAV\Exception\NotFound;
  41. use Sabre\DAV\Exception\NotImplemented;
  42. use Sabre\DAV\Exception\PreconditionFailed;
  43. use Sabre\DAV\Exception\RequestedRangeNotSatisfiable;
  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. ServerMaintenanceMode::class => true,
  83. ];
  84. private string $appName;
  85. private LoggerInterface $logger;
  86. /**
  87. * @param string $loggerAppName app name to use when logging
  88. */
  89. public function __construct(string $loggerAppName, LoggerInterface $logger) {
  90. $this->appName = $loggerAppName;
  91. $this->logger = $logger;
  92. }
  93. /**
  94. * This initializes the plugin.
  95. *
  96. * This function is called by \Sabre\DAV\Server, after
  97. * addPlugin is called.
  98. *
  99. * This method should set up the required event subscriptions.
  100. *
  101. * @param \Sabre\DAV\Server $server
  102. * @return void
  103. */
  104. public function initialize(\Sabre\DAV\Server $server) {
  105. $server->on('exception', [$this, 'logException'], 10);
  106. }
  107. /**
  108. * Log exception
  109. */
  110. public function logException(\Throwable $ex) {
  111. $exceptionClass = get_class($ex);
  112. if (isset($this->nonFatalExceptions[$exceptionClass])) {
  113. $this->logger->debug($ex->getMessage(), [
  114. 'app' => $this->appName,
  115. 'exception' => $ex,
  116. ]);
  117. return;
  118. }
  119. $this->logger->critical($ex->getMessage(), [
  120. 'app' => $this->appName,
  121. 'exception' => $ex,
  122. ]);
  123. }
  124. }