Skip to content

Commit

Permalink
Set fields on data model fallback
Browse files Browse the repository at this point in the history
for a more thorough dummy
  • Loading branch information
jonom committed Jul 16, 2018
1 parent 9115403 commit 35a9889
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/CustomErrorControllerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ public function customError($errorCode, $customFields, $template = null, $contro
Director::set_current_page(null);
// Set some default properties for the page, then override with custom ones if provided
$customFields = array_merge(
// Use title from config if set, otherwise fall back to framework definition
['Title' => $response->getStatusDescription()],
[
// Use title from config if set, otherwise fall back to framework definition
'Title' => $response->getStatusDescription(),
// Add the error code so it's available in templates
'ErrorResponseCode' => $errorCode,
],
$defaultCustomFields,
$customFields
);
Expand All @@ -97,18 +101,24 @@ public function customError($errorCode, $customFields, $template = null, $contro
$dataRecord = $pageType::create();
// Negative ID as it's a fake. Use error code so we have an ID for partial caching etc.
$dataRecord->ID = -$errorCode;
$dataRecord->update($customFields);
// Create a request with an empty session, so session data is not rendered and potentially lost to error responses.
$request = new HTTPRequest('GET', '');
$request->setSession(new Session([]));
// Render the response body
$controller = $controllerType::create($dataRecord);

// To Do: all of this...
$controller->setRequest($request);
$controller->setResponse(new HTTPResponse());
$controller->doInit();
$controller->pushCurrent();
$body = $controller->renderWith($templates, $customFields);
$controller->popCurrent();
$response->setBody($body);
// ... Could be replaced with just this, but would lose custom template support
// $response = $controller->handleRequest($request);

if ($response) {
throw new HTTPResponse_Exception($response, $errorCode);
}
Expand Down

0 comments on commit 35a9889

Please sign in to comment.