1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @see https://github.com/soluble-io/soluble-mediatools for the canonical repository |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) 2018-2020 Sébastien Vanvelthem. (https://github.com/belgattitude) |
9
|
|
|
* @license https://github.com/soluble-io/soluble-mediatools/blob/master/LICENSE.md MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Soluble\MediaTools\Common\Exception; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Process\Exception as SPException; |
15
|
|
|
use Symfony\Component\Process\Process; |
16
|
|
|
|
17
|
|
|
class ProcessException extends RuntimeException implements ProcessExceptionInterface |
18
|
|
|
{ |
19
|
|
|
public const DEFAULT_EXCEPTION_CODE = 1; |
20
|
|
|
|
21
|
|
|
/** @var Process */ |
22
|
|
|
private $process; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string|null $message if not set will use the previousException message |
26
|
|
|
*/ |
27
|
16 |
|
public function __construct(Process $process, SPException\RuntimeException $previousException, ?string $message = null) |
28
|
|
|
{ |
29
|
16 |
|
if ($previousException instanceof SPException\ProcessFailedException || |
30
|
5 |
|
$previousException instanceof SPException\ProcessTimedOutException || |
31
|
16 |
|
$previousException instanceof SPException\ProcessSignaledException |
32
|
|
|
) { |
33
|
16 |
|
$code = $previousException->getProcess()->getExitCode(); |
34
|
|
|
} else { |
35
|
|
|
$code = self::DEFAULT_EXCEPTION_CODE; |
36
|
|
|
} |
37
|
|
|
|
38
|
16 |
|
if ($message === null) { |
39
|
16 |
|
$errOutput = $process->isStarted() ? trim($process->getErrorOutput()) : ''; |
40
|
|
|
|
41
|
16 |
|
$message = sprintf( |
42
|
16 |
|
'%s, exit %s: %s (%s)', |
43
|
16 |
|
$process->getExitCodeText() ?? 'Empty exit code text from process', |
44
|
16 |
|
$process->getExitCode() ?? '9999', |
45
|
16 |
|
$process->getCommandLine(), |
46
|
16 |
|
$errOutput !== '' ? $errOutput : $previousException->getMessage() |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
16 |
|
parent::__construct( |
51
|
16 |
|
$message, |
52
|
16 |
|
is_int($code) ? $code : self::DEFAULT_EXCEPTION_CODE, |
53
|
|
|
$previousException |
54
|
|
|
); |
55
|
|
|
|
56
|
16 |
|
$this->process = $process; |
57
|
16 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return symfony process object. |
61
|
|
|
*/ |
62
|
1 |
|
public function getProcess(): Process |
63
|
|
|
{ |
64
|
1 |
|
return $this->process; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
public function getErrorOutput(): string |
68
|
|
|
{ |
69
|
1 |
|
return $this->process->getErrorOutput(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return SPException\RuntimeException|SPException\ProcessFailedException|SPException\ProcessSignaledException|SPException\ProcessTimedOutException |
74
|
|
|
*/ |
75
|
1 |
|
public function getSymfonyProcessRuntimeException(): SPException\RuntimeException |
76
|
|
|
{ |
77
|
|
|
/** |
78
|
|
|
* @var SPException\RuntimeException $previous |
79
|
|
|
*/ |
80
|
1 |
|
$previous = $this->getPrevious(); |
81
|
|
|
|
82
|
1 |
|
return $previous; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|