mirror of
https://github.com/filp/whoops.git
synced 2026-09-05 15:08:19 +00:00
Return correct http status code & message, fix #87
This commit is contained in:
+72
-29
@@ -15,14 +15,53 @@ use Exception;
|
||||
|
||||
class Run
|
||||
{
|
||||
const EXCEPTION_HANDLER = 'handleException';
|
||||
const ERROR_HANDLER = 'handleError';
|
||||
const SHUTDOWN_HANDLER = 'handleShutdown';
|
||||
const EXCEPTION_HANDLER = "handleException";
|
||||
const ERROR_HANDLER = "handleError";
|
||||
const SHUTDOWN_HANDLER = "handleShutdown";
|
||||
|
||||
protected $isRegistered;
|
||||
protected $allowQuit = true;
|
||||
protected $sendOutput = true;
|
||||
protected $sendHttpCode = 500;
|
||||
protected $allowQuit = true;
|
||||
protected $sendOutput = true;
|
||||
protected $sendHttpCode = 500;
|
||||
protected $httpStatusCodes = array(
|
||||
100 => "Continue",
|
||||
101 => "Switching Protocols",
|
||||
200 => "OK",
|
||||
201 => "Created",
|
||||
202 => "Accepted",
|
||||
203 => "Non-Authoritative Information",
|
||||
204 => "No Content",
|
||||
205 => "Reset Content",
|
||||
206 => "Partial Content",
|
||||
300 => "Multiple Choices",
|
||||
301 => "Moved Permanently",
|
||||
302 => "Moved Temporarily",
|
||||
303 => "See Other",
|
||||
304 => "Not Modified",
|
||||
305 => "Use Proxy",
|
||||
400 => "Bad Request",
|
||||
401 => "Unauthorized",
|
||||
402 => "Payment Required",
|
||||
403 => "Forbidden",
|
||||
404 => "Not Found",
|
||||
405 => "Method Not Allowed",
|
||||
406 => "Not Acceptable",
|
||||
407 => "Proxy Authentication Required",
|
||||
408 => "Request Time-out",
|
||||
409 => "Conflict",
|
||||
410 => "Gone",
|
||||
411 => "Length Required",
|
||||
412 => "Precondition Failed",
|
||||
413 => "Request Entity Too Large",
|
||||
414 => "Request-URI Too Large",
|
||||
415 => "Unsupported Media Type",
|
||||
500 => "Internal Server Error",
|
||||
501 => "Not Implemented",
|
||||
502 => "Bad Gateway",
|
||||
503 => "Service Unavailable",
|
||||
504 => "Gateway Time-out",
|
||||
505 => "HTTP Version not supported"
|
||||
);
|
||||
|
||||
/**
|
||||
* @var HandlerInterface[]
|
||||
@@ -46,8 +85,8 @@ class Run
|
||||
|
||||
if(!$handler instanceof HandlerInterface) {
|
||||
throw new InvalidArgumentException(
|
||||
'Argument to ' . __METHOD__ . ' must be a callable, or instance of'
|
||||
. 'Whoops\\Handler\\HandlerInterface'
|
||||
"Argument to " . __METHOD__ . " must be a callable, or instance of"
|
||||
. "Whoops\\Handler\\HandlerInterface"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -57,7 +96,7 @@ class Run
|
||||
|
||||
/**
|
||||
* Removes the last handler in the stack and returns it.
|
||||
* Returns null if there's nothing else to pop.
|
||||
* Returns null if there"s nothing else to pop.
|
||||
* @return null|HandlerInterface
|
||||
*/
|
||||
public function popHandler()
|
||||
@@ -104,10 +143,10 @@ class Run
|
||||
if(!$this->isRegistered) {
|
||||
// Workaround PHP bug 42098
|
||||
// https://bugs.php.net/bug.php?id=42098
|
||||
class_exists('\\Whoops\\Exception\\ErrorException');
|
||||
class_exists('\\Whoops\\Exception\\FrameCollection');
|
||||
class_exists('\\Whoops\\Exception\\Frame');
|
||||
class_exists('\\Whoops\\Exception\\Inspector');
|
||||
class_exists("\\Whoops\\Exception\\ErrorException");
|
||||
class_exists("\\Whoops\\Exception\\FrameCollection");
|
||||
class_exists("\\Whoops\\Exception\\Frame");
|
||||
class_exists("\\Whoops\\Exception\\Inspector");
|
||||
|
||||
set_error_handler(array($this, self::ERROR_HANDLER));
|
||||
set_exception_handler(array($this, self::EXCEPTION_HANDLER));
|
||||
@@ -162,8 +201,8 @@ class Run
|
||||
array_map(
|
||||
function ($pattern) use ($levels) {
|
||||
return array(
|
||||
'pattern' => $pattern,
|
||||
'levels' => $levels,
|
||||
"pattern" => $pattern,
|
||||
"levels" => $levels,
|
||||
);
|
||||
},
|
||||
(array) $patterns
|
||||
@@ -176,6 +215,7 @@ class Run
|
||||
* Should Whoops send HTTP error code to the browser if possible?
|
||||
* Whoops will by default send HTTP code 500, but you may wish to
|
||||
* use 502, 503, or another 5xx family code.
|
||||
*
|
||||
* @param bool|int $code
|
||||
* @return bool
|
||||
*/
|
||||
@@ -193,9 +233,9 @@ class Run
|
||||
$code = 500;
|
||||
}
|
||||
|
||||
if(!is_numeric($code) || $code < 500 || 600 <= $code) {
|
||||
throw new \Exception(
|
||||
'Only status codes 500-599 should be used in case of a server error'
|
||||
if(!isset($this->httpStatusCodes[$code])) {
|
||||
throw new InvalidArgumentException(
|
||||
"Unknown status code '$code'"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -258,19 +298,22 @@ class Run
|
||||
|
||||
$output = ob_get_clean();
|
||||
|
||||
// If we're allowed to, send output generated by handlers directly
|
||||
// to the output, otherwise, and if the script doesn't quit, return
|
||||
// If we"re allowed to, send output generated by handlers directly
|
||||
// to the output, otherwise, and if the script doesn"t quit, return
|
||||
// it so that it may be used by the caller
|
||||
if($this->writeToOutput()) {
|
||||
// @todo Might be able to clean this up a bit better
|
||||
// If we're going to quit execution, cleanup all other output
|
||||
// If we"re going to quit execution, cleanup all other output
|
||||
// buffers before sending our own output:
|
||||
if($handlerResponse == Handler::QUIT && $this->allowQuit()) {
|
||||
while (ob_get_level() > 0) ob_end_clean();
|
||||
}
|
||||
|
||||
if ($this->sendHttpCode() && isset($_SERVER['REQUEST_URI']) && !headers_sent()) {
|
||||
header(' ', true, $this->sendHttpCode());
|
||||
if ($this->sendHttpCode() && isset($_SERVER["REQUEST_URI"]) && !headers_sent()) {
|
||||
$httpCode = $this->sendHttpCode();
|
||||
$httpStatus = $this->httpStatusCodes[$httpCode];
|
||||
|
||||
header("HTTP/1.0 $httpCode $httpStatus", true, $httpCode);
|
||||
}
|
||||
|
||||
echo $output;
|
||||
@@ -303,8 +346,8 @@ class Run
|
||||
{
|
||||
if ($level & error_reporting()) {
|
||||
foreach ($this->silencedPatterns as $entry) {
|
||||
$pathMatches = (bool) preg_match($entry['pattern'], $file);
|
||||
$levelMatches = $level & $entry['levels'];
|
||||
$pathMatches = (bool) preg_match($entry["pattern"], $file);
|
||||
$levelMatches = $level & $entry["levels"];
|
||||
if ($pathMatches && $levelMatches) {
|
||||
// Ignore the error, abort handling
|
||||
return true;
|
||||
@@ -325,10 +368,10 @@ class Run
|
||||
{
|
||||
if($error = error_get_last()) {
|
||||
$this->handleError(
|
||||
$error['type'],
|
||||
$error['message'],
|
||||
$error['file'],
|
||||
$error['line']
|
||||
$error["type"],
|
||||
$error["message"],
|
||||
$error["file"],
|
||||
$error["line"]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ use Whoops\Run;
|
||||
use Whoops\Handler\Handler;
|
||||
use ArrayObject;
|
||||
use Mockery as m;
|
||||
use InvalidArgumentException;
|
||||
use RuntimeException;
|
||||
use Exception;
|
||||
|
||||
@@ -362,6 +363,9 @@ class RunTest extends TestCase
|
||||
$this->assertEquals("", ob_get_clean());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Whoops\Run::sendHttpCode
|
||||
*/
|
||||
public function testSendHttpCode()
|
||||
{
|
||||
$run = $this->getRunInstance();
|
||||
@@ -369,10 +373,12 @@ class RunTest extends TestCase
|
||||
$this->assertEquals(500, $run->sendHttpCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Whoops\Run::sendHttpCode
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testSendHttpCodeWrongCode()
|
||||
{
|
||||
$run = $this->getRunInstance();
|
||||
$this->setExpectedException('Exception');
|
||||
$run->sendHttpCode(403);
|
||||
$this->getRunInstance()->sendHttpCode(1337);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user