Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/JsonHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Keboola\Component;

use Keboola\Component\JsonHelper\JsonHelperException;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\Serializer\Encoder\JsonEncoder;

Expand Down Expand Up @@ -42,13 +43,13 @@ public static function writeFile(string $filePath, array $data, bool $formatted
mkdir($filePathDir, 0777, true);
}

$result = file_put_contents(
$result = @file_put_contents(
$filePath,
self::encode($data, $formatted)
);

if ($result === false) {
throw new \ErrorException('Could not write to file "%s".');
throw new JsonHelperException(sprintf('Could not write to file "%s".', $filePath));
}
}
}
11 changes: 11 additions & 0 deletions src/JsonHelper/JsonHelperException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Keboola\Component\JsonHelper;

use Exception;

class JsonHelperException extends Exception
{
}
9 changes: 5 additions & 4 deletions tests/JsonHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Keboola\Component\Tests;

use Keboola\Component\JsonHelper;
use Keboola\Component\JsonHelper\JsonHelperException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
Expand Down Expand Up @@ -142,13 +143,13 @@ public function testWriteToNonExistingDirectorySuccessfully(): void
rmdir(pathinfo($filePath, PATHINFO_DIRNAME));
}

public function testWriteToProtectedDirectoryThrowsException(): void
public function testFailedWriteThrowsException(): void
{
$filePath = '/tmp.json';
$filePath = 'php://stdin';
$array = ['key'];

$this->expectException(\ErrorException::class);
$this->expectExceptionMessageRegExp('~^file_put_contents(.*): failed to open stream: Permission denied$~');
$this->expectException(JsonHelperException::class);
$this->expectExceptionMessage('Could not write to file "php://stdin".');
JsonHelper::writeFile($filePath, $array);
}
}