Skip to content
This repository has been archived by the owner on Mar 17, 2020. It is now read-only.

Commit

Permalink
エラーのサポート
Browse files Browse the repository at this point in the history
  • Loading branch information
miyukki committed Jan 31, 2014
1 parent 8329a08 commit 834a9f9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
define('ROUTE_FILE', BASE_DIR.'/Route.php');
define('ENVIRONMENT_DIR', BASE_DIR.'/environment');

$core_classes = array('View', 'Event', 'Vendor', 'Redis', 'Session', 'Hash', 'Validation', 'Validator', 'Model', 'Type', 'MySQL', 'Util', 'Input', 'Response', 'Cookie', 'Config', 'Route', 'Router', 'Environment', 'Logic', 'Controller', 'Log', 'Test', 'Console');
$core_classes = array('View', 'Event', 'Vendor', 'Redis', 'Session', 'Hash', 'Validation', 'Validator', 'Model', 'Type', 'MySQL', 'Util', 'Input', 'Response', 'Cookie', 'Config', 'Route', 'Router', 'Environment', 'Logic', 'Controller', 'Log', 'Test', 'Console', 'Exception');
foreach ($core_classes as $class) {
require(BASE_DIR.'/core/'.$class.'.class.php');
}
Expand Down Expand Up @@ -228,8 +228,11 @@ private function run($route) {

$controller->after();
View::api($result);
} catch (Exception $e) {
} catch (Error $e) {
Response::statusCode($e->getStatusCode());
View::api(array('message' => $e->getMessage()), false);
} catch (Exception $e) {
throw $e;
}
}else if(method_exists($controller, $route->controller_method)) {
try {
Expand All @@ -239,6 +242,9 @@ private function run($route) {
$controller->$method();

$controller->after();
} catch (Error $e) {
Response::statusCode($e->getStatusCode());
echo $e->getMessage();
} catch (Exception $e) {
throw $e;
}
Expand Down
14 changes: 14 additions & 0 deletions core/Exception.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

class Error extends Exception {
private $status;

public function __construct($message, $status = 500, $code = 0, Exception $previous = null) {
$this->status = $status;
parent::__construct($message, $code, $previous);
}

public function getStatusCode() {
return $this->status;
}
}
56 changes: 56 additions & 0 deletions core/Response.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@
<?php

class Response {
public static function statusCode($code = 500) {
if (function_exists('http_response_code')) {
http_response_code($code);
} else {
// from http://us2.php.net/manual/ja/function.http-response-code.php
switch ($code) {
case 100: $text = 'Continue'; break;
case 101: $text = 'Switching Protocols'; break;
case 200: $text = 'OK'; break;
case 201: $text = 'Created'; break;
case 202: $text = 'Accepted'; break;
case 203: $text = 'Non-Authoritative Information'; break;
case 204: $text = 'No Content'; break;
case 205: $text = 'Reset Content'; break;
case 206: $text = 'Partial Content'; break;
case 300: $text = 'Multiple Choices'; break;
case 301: $text = 'Moved Permanently'; break;
case 302: $text = 'Moved Temporarily'; break;
case 303: $text = 'See Other'; break;
case 304: $text = 'Not Modified'; break;
case 305: $text = 'Use Proxy'; break;
case 400: $text = 'Bad Request'; break;
case 401: $text = 'Unauthorized'; break;
case 402: $text = 'Payment Required'; break;
case 403: $text = 'Forbidden'; break;
case 404: $text = 'Not Found'; break;
case 405: $text = 'Method Not Allowed'; break;
case 406: $text = 'Not Acceptable'; break;
case 407: $text = 'Proxy Authentication Required'; break;
case 408: $text = 'Request Time-out'; break;
case 409: $text = 'Conflict'; break;
case 410: $text = 'Gone'; break;
case 411: $text = 'Length Required'; break;
case 412: $text = 'Precondition Failed'; break;
case 413: $text = 'Request Entity Too Large'; break;
case 414: $text = 'Request-URI Too Large'; break;
case 415: $text = 'Unsupported Media Type'; break;
case 500: $text = 'Internal Server Error'; break;
case 501: $text = 'Not Implemented'; break;
case 502: $text = 'Bad Gateway'; break;
case 503: $text = 'Service Unavailable'; break;
case 504: $text = 'Gateway Time-out'; break;
case 505: $text = 'HTTP Version not supported'; break;
default:
exit('Unknown http status code "' . htmlentities($code) . '"');
break;
}

$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');

header($protocol . ' ' . $code . ' ' . $text);

$GLOBALS['http_response_code'] = $code;
}
}

public static function redirect($url, $permanently = false) {
if($permanently) {
header('HTTP/1.1 301 Moved Permanently');
Expand Down

0 comments on commit 834a9f9

Please sign in to comment.