Skip to content

Commit

Permalink
API Error page support for Security controller errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Mooyman committed May 5, 2014
1 parent fcb6a91 commit f9cb880
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
5 changes: 4 additions & 1 deletion _config/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
LeftAndMain:
extensions:
- LeftAndMainPageIconsExtension
- LeftAndMainPageIconsExtension
Security:
extensions:
- ErrorPageControllerExtension
17 changes: 17 additions & 0 deletions code/controllers/ErrorPageControllerExtension.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
14 changes: 13 additions & 1 deletion tests/model/ErrorPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,16 @@ public function testBehaviourOf403() {
$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'));
}
}

0 comments on commit f9cb880

Please sign in to comment.