Merge pull request #817 from ArthurHoaro/feature/json-conf-parsing

Proper error if the conf file is invalid instead of fatal error
This commit is contained in:
ArthurHoaro 2017-03-21 20:04:09 +01:00 committed by GitHub
commit 4bad4bde5a
3 changed files with 14 additions and 4 deletions

View file

@ -21,8 +21,14 @@ class ConfigJson implements ConfigIO
$data = str_replace(self::getPhpSuffix(), '', $data);
$data = json_decode($data, true);
if ($data === null) {
$error = json_last_error();
throw new \Exception('An error occurred while parsing JSON file: error code #'. $error);
$errorCode = json_last_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;
}

View file

@ -81,7 +81,11 @@ class ConfigManager
*/
protected function load()
{
$this->loadedConfig = $this->configIO->read($this->getConfigFileExt());
try {
$this->loadedConfig = $this->configIO->read($this->getConfigFileExt());
} catch (\Exception $e) {
die($e->getMessage());
}
$this->setDefaultValues();
}

View file

@ -40,7 +40,7 @@ class ConfigJsonTest extends \PHPUnit_Framework_TestCase
* Read a non existent config file -> empty array.
*
* @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()
{