ExceptionLoggerPlugin.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\Forbidden;
  31. use Sabre\DAV\Exception\NotAuthenticated;
  32. use Sabre\DAV\Exception\NotFound;
  33. use Sabre\DAV\Exception\PreconditionFailed;
  34. use Sabre\DAV\Exception\ServiceUnavailable;
  35. class ExceptionLoggerPlugin extends \Sabre\DAV\ServerPlugin {
  36. protected $nonFatalExceptions = [
  37. NotAuthenticated::class => true,
  38. // If tokenauth can throw this exception (which is basically as
  39. // NotAuthenticated. So not fatal.
  40. PasswordLoginForbidden::class => true,
  41. // the sync client uses this to find out whether files exist,
  42. // so it is not always an error, log it as debug
  43. NotFound::class => true,
  44. // this one mostly happens when the same file is uploaded at
  45. // exactly the same time from two clients, only one client
  46. // wins, the second one gets "Precondition failed"
  47. PreconditionFailed::class => true,
  48. // forbidden can be expected when trying to upload to
  49. // read-only folders for example
  50. Forbidden::class => true,
  51. // Happens when an external storage or federated share is temporarily
  52. // not available
  53. StorageNotAvailableException::class => true,
  54. ];
  55. /** @var string */
  56. private $appName;
  57. /** @var ILogger */
  58. private $logger;
  59. /**
  60. * @param string $loggerAppName app name to use when logging
  61. * @param ILogger $logger
  62. */
  63. public function __construct($loggerAppName, $logger) {
  64. $this->appName = $loggerAppName;
  65. $this->logger = $logger;
  66. }
  67. /**
  68. * This initializes the plugin.
  69. *
  70. * This function is called by \Sabre\DAV\Server, after
  71. * addPlugin is called.
  72. *
  73. * This method should set up the required event subscriptions.
  74. *
  75. * @param \Sabre\DAV\Server $server
  76. * @return void
  77. */
  78. public function initialize(\Sabre\DAV\Server $server) {
  79. $server->on('exception', array($this, 'logException'), 10);
  80. }
  81. /**
  82. * Log exception
  83. *
  84. */
  85. public function logException(\Exception $ex) {
  86. $exceptionClass = get_class($ex);
  87. $level = \OCP\Util::FATAL;
  88. if (isset($this->nonFatalExceptions[$exceptionClass]) ||
  89. (
  90. $exceptionClass === ServiceUnavailable::class &&
  91. $ex->getMessage() === 'System in maintenance mode.'
  92. )
  93. ) {
  94. $level = \OCP\Util::DEBUG;
  95. }
  96. $this->logger->logException($ex, [
  97. 'app' => $this->appName,
  98. 'level' => $level,
  99. ]);
  100. }
  101. }