In case of exception we return an html page in case the client is a browser
parent
a7e7f5e180
commit
c46f480031
@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
||||
*
|
||||
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\DAV\Files;
|
||||
|
||||
use OC\AppFramework\Http\Request;
|
||||
use OC_Template;
|
||||
use OCP\IRequest;
|
||||
use Sabre\DAV\Exception;
|
||||
use Sabre\DAV\Server;
|
||||
use Sabre\DAV\ServerPlugin;
|
||||
|
||||
class BrowserErrorPagePlugin extends ServerPlugin {
|
||||
|
||||
/** @var Server */
|
||||
private $server;
|
||||
|
||||
/**
|
||||
* This initializes the plugin.
|
||||
*
|
||||
* This function is called by Sabre\DAV\Server, after
|
||||
* addPlugin is called.
|
||||
*
|
||||
* This method should set up the required event subscriptions.
|
||||
*
|
||||
* @param Server $server
|
||||
* @return void
|
||||
*/
|
||||
function initialize(Server $server) {
|
||||
$this->server = $server;
|
||||
$server->on('exception', array($this, 'logException'), 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param IRequest $request
|
||||
* @return bool
|
||||
*/
|
||||
public static function isBrowserRequest(IRequest $request) {
|
||||
if ($request->getMethod() !== 'GET') {
|
||||
return false;
|
||||
}
|
||||
return $request->isUserAgent([Request::USER_AGENT_IE_8]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Exception $ex
|
||||
*/
|
||||
public function logException(\Exception $ex) {
|
||||
if ($ex instanceof Exception) {
|
||||
$httpCode = $ex->getHTTPCode();
|
||||
$headers = $ex->getHTTPHeaders($this->server);
|
||||
} else {
|
||||
$httpCode = 500;
|
||||
$headers = [];
|
||||
}
|
||||
$this->server->httpResponse->addHeaders($headers);
|
||||
$this->server->httpResponse->setStatus($httpCode);
|
||||
$body = $this->generateBody($ex);
|
||||
$this->server->httpResponse->setBody($body);
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
* @param \Exception $ex
|
||||
* @param int $httpCode
|
||||
* @return bool|string
|
||||
*/
|
||||
public function generateBody(\Exception $exception) {
|
||||
$request = \OC::$server->getRequest();
|
||||
$content = new OC_Template('dav', 'exception', 'guest');
|
||||
$content->assign('title', $this->server->httpResponse->getStatusText());
|
||||
$content->assign('message', $exception->getMessage());
|
||||
$content->assign('errorClass', get_class($exception));
|
||||
$content->assign('errorMsg', $exception->getMessage());
|
||||
$content->assign('errorCode', $exception->getCode());
|
||||
$content->assign('file', $exception->getFile());
|
||||
$content->assign('line', $exception->getLine());
|
||||
$content->assign('trace', $exception->getTraceAsString());
|
||||
$content->assign('debugMode', \OC::$server->getSystemConfig()->getValue('debug', false));
|
||||
$content->assign('remoteAddr', $request->getRemoteAddress());
|
||||
$content->assign('requestID', $request->getId());
|
||||
return $content->fetchPage();
|
||||
}
|
||||
|
||||
/*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function sendResponse() {
|
||||
$this->server->sapi->sendResponse($this->server->httpResponse);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
/** @var array $_ */
|
||||
/** @var OC_L10N $l */
|
||||
|
||||
style('core', ['styles', 'header']);
|
||||
?>
|
||||
<span class="error error-wide">
|
||||
<h2><strong><?php p($_['title']) ?></strong></h2>
|
||||
<p><?php p($_['message']) ?></p>
|
||||
<br>
|
||||
|
||||
<h2><strong><?php p($l->t('Technical details')) ?></strong></h2>
|
||||
<ul>
|
||||
<li><?php p($l->t('Remote Address: %s', $_['remoteAddr'])) ?></li>
|
||||
<li><?php p($l->t('Request ID: %s', $_['requestID'])) ?></li>
|
||||
<?php if($_['debugMode']): ?>
|
||||
<li><?php p($l->t('Type: %s', $_['errorClass'])) ?></li>
|
||||
<li><?php p($l->t('Code: %s', $_['errorCode'])) ?></li>
|
||||
<li><?php p($l->t('Message: %s', $_['errorMsg'])) ?></li>
|
||||
<li><?php p($l->t('File: %s', $_['file'])) ?></li>
|
||||
<li><?php p($l->t('Line: %s', $_['line'])) ?></li>
|
||||
<?php endif; ?>
|
||||
</ul>
|
||||
|
||||
<?php if($_['debugMode']): ?>
|
||||
<br />
|
||||
<h2><strong><?php p($l->t('Trace')) ?></strong></h2>
|
||||
<pre><?php p($_['trace']) ?></pre>
|
||||
<?php endif; ?>
|
||||
</span>
|
||||
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
||||
*
|
||||
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
namespace OCA\DAV\Tests\Unit\DAV;
|
||||
|
||||
use OCA\DAV\Files\BrowserErrorPagePlugin;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
use Sabre\DAV\Exception\NotFound;
|
||||
|
||||
class BrowserErrorPagePluginTest extends \Test\TestCase {
|
||||
|
||||
/**
|
||||
* @dataProvider providesExceptions
|
||||
* @param $expectedCode
|
||||
* @param $exception
|
||||
*/
|
||||
public function test($expectedCode, $exception) {
|
||||
/** @var BrowserErrorPagePlugin | PHPUnit_Framework_MockObject_MockObject $plugin */
|
||||
$plugin = $this->getMockBuilder('OCA\DAV\Files\BrowserErrorPagePlugin')->setMethods(['sendResponse', 'generateBody'])->getMock();
|
||||
$plugin->expects($this->once())->method('generateBody')->willReturn(':boom:');
|
||||
$plugin->expects($this->once())->method('sendResponse');
|
||||
/** @var \Sabre\DAV\Server | PHPUnit_Framework_MockObject_MockObject $server */
|
||||
$server = $this->getMockBuilder('Sabre\DAV\Server')->disableOriginalConstructor()->getMock();
|
||||
$server->expects($this->once())->method('on');
|
||||
$httpResponse = $this->getMockBuilder('Sabre\HTTP\Response')->disableOriginalConstructor()->getMock();
|
||||
$httpResponse->expects($this->once())->method('addHeaders');
|
||||
$httpResponse->expects($this->once())->method('setStatus')->with($expectedCode);
|
||||
$httpResponse->expects($this->once())->method('setBody')->with(':boom:');
|
||||
$server->httpResponse = $httpResponse;
|
||||
$plugin->initialize($server);
|
||||
$plugin->logException($exception);
|
||||
}
|
||||
|
||||
public function providesExceptions() {
|
||||
return [
|
||||
[ 404, new NotFound()],
|
||||
[ 500, new \RuntimeException()],
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue