Skip to content

Instantly share code, notes, and snippets.

@mlewis-everley
Created September 6, 2019 09:17
Show Gist options
  • Save mlewis-everley/9ac0c8055241adb9c9ad9d9aebc23e3c to your computer and use it in GitHub Desktop.
Save mlewis-everley/9ac0c8055241adb9c9ad9d9aebc23e3c to your computer and use it in GitHub Desktop.
Controller Extension to add styled error pages
<?php
class ControllerExtension extends Extension {
private static $error_codes = array(
400 => 'Bad Request',
401 => 'Unauthorized',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Request Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
);
public function onBeforeHTTPError($code, $request) {
if (array_key_exists($code,self::$error_codes))
$title = self::$error_codes[$code];
else {
$title = 'Bad Request';
$code = 400;
}
$body = $this
->owner
->customise(array(
'Title' => $title,
'Code' => $code
))->renderWith(array(
'Error',
'Page'
));
$response = new SS_HTTPResponse($body,$code,$title);
throw new SS_HTTPResponse_Exception($response, $code);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment