Skip to content

Commit

Permalink
Merge pull request #30 from CamiloManrique/29-easier-stderr
Browse files Browse the repository at this point in the history
Add command error output to stderr
  • Loading branch information
jens1o authored May 19, 2019
2 parents 6ccde0e + 928bb3c commit ae5abc1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
21 changes: 21 additions & 0 deletions src/Alchemy/BinaryDriver/Exception/ExecutionFailureException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,25 @@

class ExecutionFailureException extends \RuntimeException implements ExceptionInterface
{
/** @var string */
protected $command;

/** @var string */
protected $errorOutput;

public function __construct($binaryName, $command, $errorOutput = null, $code = 0, $previous = null)
{
$message = sprintf("%s failed to execute command %s:\n\nError Output:\n\n %s", $binaryName, $command, $errorOutput);
parent::__construct($message, $code, $previous);
$this->command = $command;
$this->errorOutput = $errorOutput;
}

public function getCommand(){
return $this->command;
}

public function getErrorOutput(){
return $this->errorOutput;
}
}
26 changes: 12 additions & 14 deletions src/Alchemy/BinaryDriver/ProcessRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,18 @@ public function run(Process $process, SplObjectStorage $listeners, $bypassErrors
$process->run($this->buildCallback($listeners));
} catch (RuntimeException $e) {
if (!$bypassErrors) {
$this->doExecutionFailure($process->getCommandLine(), $e);
$this->doExecutionFailure($process->getCommandLine(), $process->getErrorOutput(), $e);
}
}


if (!$bypassErrors && !$process->isSuccessful()) {
$this->doExecutionFailure($process->getCommandLine());
$this->doExecutionFailure($process->getCommandLine(), $process->getErrorOutput());
} elseif (!$process->isSuccessful()) {
$this->logger->error(sprintf(
'%s failed to execute command %s: %s', $this->name, $process->getCommandLine(), $process->getErrorOutput()
));

$this->logger->error($this->createErrorMessage($process->getCommandLine(), $process->getErrorOutput()));
return;
} else {
$this->logger->info(sprintf('%s executed command successfully', $this->name));

return $process->getOutput();
}
}
Expand All @@ -92,13 +89,14 @@ private function buildCallback(SplObjectStorage $listeners)
};
}

private function doExecutionFailure($command, \Exception $e = null)
private function doExecutionFailure($command, $errorOutput, \Exception $e = null)
{
$this->logger->error(sprintf(
'%s failed to execute command %s', $this->name, $command
));
throw new ExecutionFailureException(sprintf(
'%s failed to execute command %s', $this->name, $command
), $e ? $e->getCode() : null, $e ?: null);
$this->logger->error($this->createErrorMessage($command, $errorOutput));
throw new ExecutionFailureException($this->name, $command, $errorOutput,
$e ? $e->getCode() : 0, $e ?: null);
}

private function createErrorMessage($command, $errorOutput){
return sprintf('%s failed to execute command %s: %s', $this->name, $command, $errorOutput);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Alchemy\Tests\BinaryDriver\Exceptions;

use Alchemy\BinaryDriver\BinaryDriverTestCase;
use Alchemy\BinaryDriver\Exception\ExecutionFailureException;
use Alchemy\BinaryDriver\ProcessRunner;

class ExecutionFailureExceptionTest extends BinaryDriverTestCase
{
public function getProcessRunner($logger)
{
return new ProcessRunner($logger, 'test-runner');
}

public function testGetExceptionInfo(){

$logger = $this->createLoggerMock();
$runner = $this->getProcessRunner($logger);

$process = $this->createProcessMock(1, false, '--helloworld--', null, "Error Output", true);
try{
$runner->run($process, new \SplObjectStorage(), false);
$this->fail('An exception should have been raised');
}
catch (ExecutionFailureException $e){
$this->assertEquals("--helloworld--", $e->getCommand());
$this->assertEquals("Error Output", $e->getErrorOutput());
}

}

}

2 comments on commit ae5abc1

@Xakki
Copy link

@Xakki Xakki commented on ae5abc1 May 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix! Please ad some tags for this (5.0.1 example) - this commit very useful!

@jens1o
Copy link
Contributor Author

@jens1o jens1o commented on ae5abc1 Jun 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reminder (and sorry my late response)! 5.1.0 is out!

Please sign in to comment.