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.
This commit is contained in:
parent
7fc5f07492
commit
c6a4c2882d
3 changed files with 14 additions and 4 deletions
|
@ -21,8 +21,14 @@ public function read($filepath)
|
||||||
$data = str_replace(self::getPhpSuffix(), '', $data);
|
$data = str_replace(self::getPhpSuffix(), '', $data);
|
||||||
$data = json_decode($data, true);
|
$data = json_decode($data, true);
|
||||||
if ($data === null) {
|
if ($data === null) {
|
||||||
$error = json_last_error();
|
$errorCode = json_last_error();
|
||||||
throw new \Exception('An error occurred while parsing JSON file: error code #'. $error);
|
$error = 'An error occurred while parsing JSON configuration file ('. $filepath .'): error code #';
|
||||||
|
$error .= $errorCode. '<br>➜ <code>' . json_last_error_msg() .'</code>';
|
||||||
|
if ($errorCode === JSON_ERROR_SYNTAX) {
|
||||||
|
$error .= '<br>Please check your JSON syntax (without PHP comment tags) using a JSON lint tool such as ';
|
||||||
|
$error .= '<a href="http://jsonlint.com/">jsonlint.com</a>.';
|
||||||
|
}
|
||||||
|
throw new \Exception($error);
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,7 +81,11 @@ protected function initialize()
|
||||||
*/
|
*/
|
||||||
protected function load()
|
protected function load()
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$this->loadedConfig = $this->configIO->read($this->getConfigFileExt());
|
$this->loadedConfig = $this->configIO->read($this->getConfigFileExt());
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
die($e->getMessage());
|
||||||
|
}
|
||||||
$this->setDefaultValues();
|
$this->setDefaultValues();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ public function testReadNonExistent()
|
||||||
* Read a non existent config file -> empty array.
|
* Read a non existent config file -> empty array.
|
||||||
*
|
*
|
||||||
* @expectedException \Exception
|
* @expectedException \Exception
|
||||||
* @expectedExceptionMessage An error occurred while parsing JSON file: error code #4
|
* @expectedExceptionMessageRegExp /An error occurred while parsing JSON configuration file \([\w\/\.]+\): error code #4/
|
||||||
*/
|
*/
|
||||||
public function testReadInvalidJson()
|
public function testReadInvalidJson()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue