Skip to content

Commit d661f7b

Browse files
Add error message and new function for parsing error
1 parent cb4cfd1 commit d661f7b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Differ/Translator.php

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Differ\Translator;
44

55
use Symfony\Component\Yaml\Yaml;
6+
use function Differ\Parser\parseError;
67

78
function getJson(string $path): array
89
{
@@ -19,11 +20,25 @@ function getFileContent(string $path): string
1920
{
2021
$dirPath = __DIR__;
2122

23+
// Colors for beautiful :)
24+
$normalColor = "\x1b[0m";
25+
$redColor = "\x1b[41m";
26+
2227
if (str_starts_with($path, '/')) {
2328
$fullPath = $path;
2429
} else {
2530
$fullPath = "{$dirPath}/../../{$path}";
2631
}
2732

33+
if (!file_exists($fullPath)) {
34+
$errorMessage = <<<DOC
35+
{$redColor}ERROR!{$normalColor}
36+
37+
File: "{$fullPath}" don't exists!
38+
DOC;
39+
40+
parseError($errorMessage);
41+
}
42+
2843
return file_get_contents($fullPath);
2944
}

src/Parser.php

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Differ\Parser;
44

5+
use JetBrains\PhpStorm\NoReturn;
56
use function cli\line;
67
use function Differ\Differ\genDiff;
78

@@ -10,3 +11,9 @@ function parse(string $pathToFile1, string $pathToFile2, string $format): void
1011
$resultString = genDiff($pathToFile1, $pathToFile2, $format);
1112
line('%s', $resultString);
1213
}
14+
15+
#[NoReturn] function parseError(string $errorMessage): void
16+
{
17+
line('%s', $errorMessage);
18+
exit();
19+
}

0 commit comments

Comments
 (0)