Skip to content

Commit 637718b

Browse files
Throw custom exceptions
1 parent 34e7c53 commit 637718b

File tree

7 files changed

+78
-9
lines changed

7 files changed

+78
-9
lines changed

src/Exception/ExceptionInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Dotenv\Exception;
4+
5+
/**
6+
* This is the exception interface.
7+
*/
8+
interface ExceptionInterface
9+
{
10+
//
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Dotenv\Exception;
4+
5+
use InvalidArgumentException;
6+
7+
/**
8+
* This is the invalid callback exception class.
9+
*/
10+
class InvalidCallbackException extends InvalidArgumentException implements ExceptionInterface
11+
{
12+
//
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Dotenv\Exception;
4+
5+
use InvalidArgumentException;
6+
7+
/**
8+
* This is the invalid file exception class.
9+
*/
10+
class InvalidFileException extends InvalidArgumentException implements ExceptionInterface
11+
{
12+
//
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Dotenv\Exception;
4+
5+
use InvalidArgumentException;
6+
7+
/**
8+
* This is the invalid path exception class.
9+
*/
10+
class InvalidPathException extends InvalidArgumentException implements ExceptionInterface
11+
{
12+
//
13+
}

src/Exception/ValidationException.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Dotenv\Exception;
4+
5+
use RuntimeException;
6+
7+
/**
8+
* This is the validation exception class.
9+
*/
10+
class ValidationException extends RuntimeException implements ExceptionInterface
11+
{
12+
//
13+
}

src/Loader.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace Dotenv;
44

5-
use InvalidArgumentException;
5+
use Dotenv\Exception\InvalidFileException;
6+
use Dotenv\Exception\InvalidPathException;
67

78
/**
89
* Loader.
@@ -67,17 +68,17 @@ public function load()
6768
/**
6869
* Ensures the given filePath is readable.
6970
*
70-
* @throws \InvalidArgumentException
71+
* @throws \Dotenv\Exception\InvalidPathException
7172
*
7273
* @return void
7374
*/
7475
protected function ensureFileIsReadable()
7576
{
7677
$filePath = $this->filePath;
7778
if (!is_readable($filePath) || !is_file($filePath)) {
78-
throw new InvalidArgumentException(sprintf(
79+
throw new InvalidPathException(sprintf(
7980
'Dotenv: Environment file .env not found or not readable. '.
80-
'Create file with your environment settings at %s',
81+
'Create file with your environment settings at %s.',
8182
$filePath
8283
));
8384
}
@@ -194,7 +195,7 @@ protected function splitCompoundStringIntoParts($name, $value)
194195
* @param string $name
195196
* @param string $value
196197
*
197-
* @throws \InvalidArgumentException
198+
* @throws \Dotenv\Exception\InvalidFileException
198199
*
199200
* @return array
200201
*/
@@ -231,7 +232,7 @@ protected function sanitiseVariableValue($name, $value)
231232

232233
// Unquoted values cannot contain whitespace
233234
if (preg_match('/\s+/', $value) > 0) {
234-
throw new InvalidArgumentException('Dotenv values containing spaces must be surrounded by quotes.');
235+
throw new InvalidFileException('Dotenv values containing spaces must be surrounded by quotes.');
235236
}
236237
}
237238

src/Validator.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Dotenv;
44

5+
use Dotenv\Exception\InvalidCallbackException;
6+
use Dotenv\Exception\InvalidPathException;
7+
58
/**
69
* Validator.
710
*
@@ -82,12 +85,14 @@ function ($value) use ($choices) {
8285
* @param callable $callback
8386
* @param string $message
8487
*
88+
* @throws \Dotenv\Exception\InvalidCallbackException|\Dotenv\Exception\ValidationException
89+
*
8590
* @return \Dotenv\Validator
8691
*/
8792
protected function assertCallback($callback, $message = 'failed callback assertion')
8893
{
8994
if (!is_callable($callback)) {
90-
throw new \InvalidArgumentException('Callback must be callable');
95+
throw new InvalidCallbackException('The provided callback must be callable.');
9196
}
9297

9398
$variablesFailingAssertion = array();
@@ -99,8 +104,8 @@ protected function assertCallback($callback, $message = 'failed callback asserti
99104
}
100105

101106
if (count($variablesFailingAssertion) > 0) {
102-
throw new \RuntimeException(sprintf(
103-
'One or more environment variables failed assertions: %s',
107+
throw new ValidationException(sprintf(
108+
'One or more environment variables failed assertions: %s.',
104109
implode(', ', $variablesFailingAssertion)
105110
));
106111
}

0 commit comments

Comments
 (0)