Skip to content

Commit

Permalink
Add messages of Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaiProgramist committed Nov 16, 2024
1 parent c19b570 commit 5444952
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Differ\Parser;

use Exception;
use Symfony\Component\Yaml\Yaml;

use function Differ\Formatters\selectFormatter;
Expand All @@ -11,24 +12,30 @@ function parse(array $resultDiff, string $format): string
return selectFormatter($resultDiff, $format);
}

/**
* @throws Exception
*/
function parseToJson(string $path): array
{
$content = getFileContent($path);
$offset = strpos($path, '.');

if ($offset === false) {
throw new \Exception("Incorrect path");
throw new Exception("Incorrect path: {$path}");
}

$extension = substr($path, $offset + 1);

return match ($extension) {
'json' => json_decode($content, true),
'yaml' => Yaml::parse($content),
default => throw new \Exception("There is no such extension: {$extension}")
default => throw new Exception("The file format is not supported by the current version of the program: {$extension}")
};
}

/**
* @throws Exception
*/
function getFileContent(string $path): string
{
$dirPath = __DIR__;
Expand All @@ -40,7 +47,7 @@ function getFileContent(string $path): string
}

if (!file_exists($fullPath)) {
throw new \Exception("Incorrect path");
throw new Exception("File does not exist: {$path}");
}

return file_get_contents($fullPath);
Expand Down

0 comments on commit 5444952

Please sign in to comment.