API Error page support for Security controller errors

This commit is contained in:
Damian Mooyman 2014-05-02 12:52:18 +12:00
parent fcb6a9140c
commit f9cb8805e1
3 changed files with 34 additions and 2 deletions

View File

@ -1,3 +1,6 @@
LeftAndMain:
extensions:
- LeftAndMainPageIconsExtension
Security:
extensions:
- ErrorPageControllerExtension

View File

@ -0,0 +1,17 @@
<?php
/**
* Enhances error handling for a controller with ErrorPage generated output
*
* @package cms
* @subpackage controller
*/
class ErrorPageControllerExtension extends Extension {
public function onBeforeHTTPError($statusCode, $request) {
$response = ErrorPage::response_for($statusCode);
if($response) {
throw new SS_HTTPResponse_Exception($response, $statusCode);
}
}
}

View File

@ -70,4 +70,16 @@ class ErrorPageTest extends FunctionalTest {
$this->assertEquals($response->getStatusCode(), '403');
$this->assertNotNull($response->getBody(), 'We have body text from the error page');
}
public function testSecurityError() {
// Generate 404 page
$page = $this->objFromFixture('ErrorPage', '404');
$page->publish('Stage', 'Live');
// Test invalid action
$response = $this->get('Security/nosuchaction');
$this->assertEquals($response->getStatusCode(), '404');
$this->assertNotNull($response->getBody());
$this->assertContains('text/html', $response->getHeader('Content-Type'));
}
}