Repository navigation
Expand file tree
/
Copy pathDockerCommandExecutionFailed.php
More file actions
50 lines (42 loc) · 1.83 KB
/
Copy pathDockerCommandExecutionFailed.php
File metadata and controls
50 lines (42 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
declare(strict_types=1);
namespace TinyBlocks\DockerContainer\Exceptions;
use RuntimeException;
use TinyBlocks\DockerContainer\DockerCommand;
use TinyBlocks\DockerContainer\ExecutionCompleted;
/**
* Raised when a Docker command cannot run, exceeds its timeout, or reports a failure the library cannot recover from.
*/
final class DockerCommandExecutionFailed extends RuntimeException
{
private function __construct(string $reason, string $command)
{
$template = 'Failed to execute command <%s> in Docker container. Reason: %s';
parent::__construct(message: sprintf($template, $command, $reason));
}
/**
* Creates a DockerCommandExecutionFailed from a failure reason and the command line that produced it.
*
* @param string $reason The reason the command failed.
* @param string $command The command line that failed.
* @return DockerCommandExecutionFailed The created exception.
*/
public static function from(string $reason, string $command): DockerCommandExecutionFailed
{
return new DockerCommandExecutionFailed(reason: $reason, command: $command);
}
/**
* Creates a DockerCommandExecutionFailed from a command and the unsuccessful outcome it produced.
*
* @param DockerCommand $command The command that failed.
* @param ExecutionCompleted $execution The unsuccessful outcome of the command.
* @return DockerCommandExecutionFailed The created exception, with the command rendered shell-escaped.
*/
public static function fromCommand(
DockerCommand $command,
ExecutionCompleted $execution
): DockerCommandExecutionFailed {
$rendered = implode(' ', array_map('escapeshellarg', $command->toArguments()));
return new DockerCommandExecutionFailed(reason: $execution->getOutput(), command: $rendered);
}
}