1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-25 10:33:18 +01:00

Restructuring how JSON error messages are built

This commit is contained in:
Michael Dowling 2013-05-29 22:07:58 -07:00
parent 6d9e6067b0
commit 922675c5c5

View File

@ -20,6 +20,18 @@ abstract class AbstractConfigLoader implements ConfigLoaderInterface
*/
protected $loadedFiles = array();
/**
* @var array JSON error code mappings
*/
protected static $jsonErrors = array(
JSON_ERROR_NONE => 'JSON_ERROR_NONE - No errors',
JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded',
JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch',
JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found',
JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON',
JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded'
);
/**
* {@inheritdoc}
*/
@ -108,29 +120,7 @@ abstract class AbstractConfigLoader implements ConfigLoaderInterface
$config = json_decode($json, true);
// Throw an exception if there was an error loading the file
if ($error = json_last_error()) {
switch ($error) {
case JSON_ERROR_NONE:
$message = 'JSON_ERROR_NONE - No errors';
break;
case JSON_ERROR_DEPTH:
$message = 'JSON_ERROR_DEPTH - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$message = 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$message = 'JSON_ERROR_CTRL_CHAR - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$message = 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$message = 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$message = 'Unknown error';
break;
}
$message = isset(self::$jsonErrors[$error]) ? self::$jsonErrors[$error] : 'Unknown error';
throw new RuntimeException("Error loading JSON data from {$filename}: ({$error}) - {$message}");
}
break;