mirror of
https://github.com/filp/whoops.git
synced 2026-09-03 14:08:36 +00:00
Merge #75 - send HTTP status code
This commit is contained in:
@@ -22,6 +22,7 @@ class Run
|
||||
protected $isRegistered;
|
||||
protected $allowQuit = true;
|
||||
protected $sendOutput = true;
|
||||
protected $sendHttpCode = 500;
|
||||
|
||||
/**
|
||||
* @var HandlerInterface[]
|
||||
@@ -142,6 +143,7 @@ class Run
|
||||
}
|
||||
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
* Silence particular errors in particular files
|
||||
* @param array|string $patterns List or a single regex pattern to match
|
||||
* @param integer $levels Defaults to E_STRICT | E_DEPRECATED
|
||||
@@ -164,6 +166,36 @@ class Run
|
||||
return $this;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
public function sendHttpCode($code = null)
|
||||
{
|
||||
if(func_num_args() == 0) {
|
||||
return $this->sendHttpCode;
|
||||
}
|
||||
|
||||
if(!$code) {
|
||||
return $this->sendHttpCode = false;
|
||||
}
|
||||
|
||||
if($code === true) {
|
||||
$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'
|
||||
);
|
||||
}
|
||||
|
||||
return $this->sendHttpCode = $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should Whoops push output directly to the client?
|
||||
* If this is false, output will be returned by handleException
|
||||
@@ -231,6 +263,10 @@ class Run
|
||||
while (ob_get_level() > 0) ob_end_clean();
|
||||
}
|
||||
|
||||
if ($this->sendHttpCode() && isset($_SERVER['REQUEST_URI']) && !headers_sent()) {
|
||||
header(' ', true, $this->sendHttpCode());
|
||||
}
|
||||
|
||||
echo $output;
|
||||
}
|
||||
|
||||
|
||||
@@ -360,4 +360,18 @@ class RunTest extends TestCase
|
||||
$this->assertEquals("hello there", $run->handleException(new RuntimeException));
|
||||
$this->assertEquals("", ob_get_clean());
|
||||
}
|
||||
|
||||
public function testSendHttpCode()
|
||||
{
|
||||
$run = $this->getRunInstance();
|
||||
$run->sendHttpCode(true);
|
||||
$this->assertEquals(500, $run->sendHttpCode());
|
||||
}
|
||||
|
||||
public function testSendHttpCodeWrongCode()
|
||||
{
|
||||
$run = $this->getRunInstance();
|
||||
$this->setExpectedException('Exception');
|
||||
$run->sendHttpCode(403);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user