diff options
author | ArthurHoaro <arthur@hoa.ro> | 2017-03-12 16:09:34 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2017-03-12 16:09:34 +0100 |
commit | c6a4c2882d89c6bcceeeccd319549611a5d1801b (patch) | |
tree | d402f0c42c04a480b086d354951ad1f0c8732696 | |
parent | 7fc5f07492b9bfe58cb33907ceb42e9bccd624ce (diff) | |
download | Shaarli-c6a4c2882d89c6bcceeeccd319549611a5d1801b.tar.gz Shaarli-c6a4c2882d89c6bcceeeccd319549611a5d1801b.tar.zst Shaarli-c6a4c2882d89c6bcceeeccd319549611a5d1801b.zip |
Proper error if the conf file is invalid instead of fatal error
Error:
An error occurred while parsing configuration JSON file (data/config.json.php): error code #4
➜ Syntax error
Please check your JSON syntax (without PHP comment tags) using a JSON lint tool such as jsonlint.com.
-rw-r--r-- | application/config/ConfigJson.php | 10 | ||||
-rw-r--r-- | application/config/ConfigManager.php | 6 | ||||
-rw-r--r-- | tests/config/ConfigJsonTest.php | 2 |
3 files changed, 14 insertions, 4 deletions
diff --git a/application/config/ConfigJson.php b/application/config/ConfigJson.php index 30908d90..9ef2ef56 100644 --- a/application/config/ConfigJson.php +++ b/application/config/ConfigJson.php | |||
@@ -21,8 +21,14 @@ class ConfigJson implements ConfigIO | |||
21 | $data = str_replace(self::getPhpSuffix(), '', $data); | 21 | $data = str_replace(self::getPhpSuffix(), '', $data); |
22 | $data = json_decode($data, true); | 22 | $data = json_decode($data, true); |
23 | if ($data === null) { | 23 | if ($data === null) { |
24 | $error = json_last_error(); | 24 | $errorCode = json_last_error(); |
25 | throw new \Exception('An error occurred while parsing JSON file: error code #'. $error); | 25 | $error = 'An error occurred while parsing JSON configuration file ('. $filepath .'): error code #'; |
26 | $error .= $errorCode. '<br>➜ <code>' . json_last_error_msg() .'</code>'; | ||
27 | if ($errorCode === JSON_ERROR_SYNTAX) { | ||
28 | $error .= '<br>Please check your JSON syntax (without PHP comment tags) using a JSON lint tool such as '; | ||
29 | $error .= '<a href="http://jsonlint.com/">jsonlint.com</a>.'; | ||
30 | } | ||
31 | throw new \Exception($error); | ||
26 | } | 32 | } |
27 | return $data; | 33 | return $data; |
28 | } | 34 | } |
diff --git a/application/config/ConfigManager.php b/application/config/ConfigManager.php index f2097410..e98af8ab 100644 --- a/application/config/ConfigManager.php +++ b/application/config/ConfigManager.php | |||
@@ -81,7 +81,11 @@ class ConfigManager | |||
81 | */ | 81 | */ |
82 | protected function load() | 82 | protected function load() |
83 | { | 83 | { |
84 | $this->loadedConfig = $this->configIO->read($this->getConfigFileExt()); | 84 | try { |
85 | $this->loadedConfig = $this->configIO->read($this->getConfigFileExt()); | ||
86 | } catch (\Exception $e) { | ||
87 | die($e->getMessage()); | ||
88 | } | ||
85 | $this->setDefaultValues(); | 89 | $this->setDefaultValues(); |
86 | } | 90 | } |
87 | 91 | ||
diff --git a/tests/config/ConfigJsonTest.php b/tests/config/ConfigJsonTest.php index 3527f83d..d237bc80 100644 --- a/tests/config/ConfigJsonTest.php +++ b/tests/config/ConfigJsonTest.php | |||
@@ -40,7 +40,7 @@ class ConfigJsonTest extends \PHPUnit_Framework_TestCase | |||
40 | * Read a non existent config file -> empty array. | 40 | * Read a non existent config file -> empty array. |
41 | * | 41 | * |
42 | * @expectedException \Exception | 42 | * @expectedException \Exception |
43 | * @expectedExceptionMessage An error occurred while parsing JSON file: error code #4 | 43 | * @expectedExceptionMessageRegExp /An error occurred while parsing JSON configuration file \([\w\/\.]+\): error code #4/ |
44 | */ | 44 | */ |
45 | public function testReadInvalidJson() | 45 | public function testReadInvalidJson() |
46 | { | 46 | { |