Skip to content

Commit

Permalink
allow to customize the exit code send in cli context
Browse files Browse the repository at this point in the history
  • Loading branch information
lode committed Mar 19, 2021
1 parent 6ecda52 commit 051070d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/API Documentation.md
Expand Up @@ -60,6 +60,10 @@ Run::register()
Run::unregister()
#=> Whoops\Run

// Send a custom exit code in CLI context (default: 1)
Run::sendExitCode($code = null)
#=> int

// If true, allows Whoops to terminate script execution (default: true)
Run::allowQuit($allowQuit = null)
#=> bool
Expand Down
34 changes: 33 additions & 1 deletion src/Whoops/Run.php
Expand Up @@ -38,6 +38,11 @@ final class Run implements RunInterface
*/
private $sendHttpCode = 500;

/**
* @var integer|false
*/
private $sendExitCode = 1;

/**
* @var HandlerInterface[]
*/
Expand Down Expand Up @@ -288,6 +293,31 @@ public function sendHttpCode($code = null)
return $this->sendHttpCode = $code;
}

/**
* Should Whoops exit with a specific code on the CLI if possible?
* Whoops will exit with 1 by default, but you can specify something else.
*
* @param int $code
*
* @return int
*
* @throws InvalidArgumentException
*/
public function sendExitCode($code = null)
{
if (func_num_args() == 0) {
return $this->sendExitCode;
}

if ($code < 0 || 255 <= $code) {
throw new InvalidArgumentException(
"Invalid status code '$code', must be between 0 and 254"
);
}

return $this->sendExitCode = (int) $code;
}

/**
* Should Whoops push output directly to the client?
* If this is false, output will be returned by handleException.
Expand Down Expand Up @@ -380,7 +410,9 @@ public function handleException($exception)
// HHVM fix for https://github.com/facebook/hhvm/issues/4055
$this->system->flushOutputBuffer();

$this->system->stopExecution(1);
$this->system->stopExecution(
$this->sendExitCode()
);
}

return $output;
Expand Down
9 changes: 9 additions & 0 deletions src/Whoops/RunInterface.php
Expand Up @@ -90,6 +90,15 @@ public function silenceErrorsInPaths($patterns, $levels = 10240);
*/
public function sendHttpCode($code = null);

/**
* Should Whoops exit with a specific code on the CLI if possible?
* Whoops will exit with 1 by default, but you can specify something else.
*
* @param int $code
* @return int
*/
public function sendExitCode($code = null);

/**
* Should Whoops push output directly to the client?
* If this is false, output will be returned by handleException
Expand Down
29 changes: 29 additions & 0 deletions tests/Whoops/RunTest.php
Expand Up @@ -503,4 +503,33 @@ public function testSendHttpCodeWrongCode()

$this->getRunInstance()->sendHttpCode(1337);
}

/**
* @covers Whoops\Run::sendHttpCode
*/
public function testSendExitCode()
{
$run = $this->getRunInstance();
$run->sendExitCode(42);
$this->assertEquals(42, $run->sendExitCode());
}

/**
* @covers Whoops\Run::sendExitCode
*/
public function testSendExitCodeDefaultCode()
{
$run = $this->getRunInstance();
$this->assertEquals(1, $run->sendExitCode());
}

/**
* @covers Whoops\Run::sendExitCode
*/
public function testSendExitCodeWrongCode()
{
$this->expectExceptionOfType('InvalidArgumentException');

$this->getRunInstance()->sendExitCode(255);
}
}

0 comments on commit 051070d

Please sign in to comment.