Skip to content

Commit 1e8a47f

Browse files
committed
feat: add new exceptions
Add new exceptions. #15
1 parent 10e45d4 commit 1e8a47f

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* PHP simple library for managing JSON files.
7+
*
8+
* @author Josantonius <hello@josantonius.dev>
9+
* @copyright 2016 (c) Josantonius
10+
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
11+
* @link https://github.com/josantonius/php-json
12+
* @since 2.0.0
13+
*/
14+
namespace Josantonius\Json\Exception;
15+
16+
/**
17+
* You can use an exception and error handler with this library.
18+
*
19+
* @link https://github.com/josantonius/php-errorhandler
20+
*/
21+
class CreateDirectoryException extends \Exception
22+
{
23+
public function __construct(string $path, \Throwable $th = null)
24+
{
25+
$lastError = error_get_last()['message'] ?? '';
26+
27+
$message = "Could not create directory in '$path'.";
28+
$message .= $lastError ? " $lastError." : '';
29+
30+
parent::__construct($th ? $th->getMessage() : $message, 0, $th);
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* PHP simple library for managing JSON files.
7+
*
8+
* @author Josantonius <hello@josantonius.dev>
9+
* @copyright 2016 (c) Josantonius
10+
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
11+
* @link https://github.com/josantonius/php-json
12+
* @since 2.0.0
13+
*/
14+
namespace Josantonius\Json\Exception;
15+
16+
/**
17+
* You can use an exception and error handler with this library.
18+
*
19+
* @link https://github.com/josantonius/php-errorhandler
20+
*/
21+
class CreateFileException extends \Exception
22+
{
23+
public function __construct(string $filepath, \Throwable $th = null)
24+
{
25+
$lastError = error_get_last()['message'] ?? '';
26+
27+
$message = "Could not create file '$filepath'.";
28+
$message .= $lastError ? " $lastError." : '';
29+
30+
parent::__construct($th ? $th->getMessage() : $message, 0, $th);
31+
}
32+
}

src/Exception/GetFileException.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* PHP simple library for managing JSON files.
7+
*
8+
* @author Josantonius <hello@josantonius.dev>
9+
* @copyright 2016 (c) Josantonius
10+
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
11+
* @link https://github.com/josantonius/php-json
12+
* @since 2.0.0
13+
*/
14+
namespace Josantonius\Json\Exception;
15+
16+
/**
17+
* You can use an exception and error handler with this library.
18+
*
19+
* @link https://github.com/josantonius/php-errorhandler
20+
*/
21+
class GetFileException extends \Exception
22+
{
23+
public function __construct(string $filepath, \Throwable $th = null)
24+
{
25+
$lastError = error_get_last()['message'] ?? '';
26+
27+
$message = "Error reading file: '$filepath'.";
28+
$message .= $lastError ? " $lastError." : '';
29+
30+
parent::__construct($th ? $th->getMessage() : $message, 0, $th);
31+
}
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* PHP simple library for managing JSON files.
7+
*
8+
* @author Josantonius <hello@josantonius.dev>
9+
* @copyright 2016 (c) Josantonius
10+
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
11+
* @link https://github.com/josantonius/php-json
12+
* @since 2.0.0
13+
*/
14+
namespace Josantonius\Json\Exception;
15+
16+
/**
17+
* You can use an exception and error handler with this library.
18+
*
19+
* @link https://github.com/josantonius/php-errorhandler
20+
*/
21+
class JsonErrorException extends \Exception
22+
{
23+
public function __construct(\Throwable $th = null)
24+
{
25+
$message = 'JSON error: ' . json_last_error_msg();
26+
27+
parent::__construct($th ? $th->getMessage() : $message, 0, $th);
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* PHP simple library for managing JSON files.
7+
*
8+
* @author Josantonius <hello@josantonius.dev>
9+
* @copyright 2016 (c) Josantonius
10+
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
11+
* @link https://github.com/josantonius/php-json
12+
* @since 2.0.0
13+
*/
14+
namespace Josantonius\Json\Exception;
15+
16+
/**
17+
* You can use an exception and error handler with this library.
18+
*
19+
* @link https://github.com/josantonius/php-errorhandler
20+
*/
21+
class UnavailableMethodException extends \Exception
22+
{
23+
public function __construct(string $method, \Throwable $th = null)
24+
{
25+
$message = 'The "' . $method . '" method is not available for remote JSON files.';
26+
27+
parent::__construct($th ? $th->getMessage() : $message, 0, $th);
28+
}
29+
}

0 commit comments

Comments
 (0)