JsonErrorException.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Elasticsearch\Common\Exceptions\Serializer;
  3. use Elasticsearch\Common\Exceptions\ElasticsearchException;
  4. /**
  5. * Class JsonErrorException
  6. *
  7. * @category Elasticsearch
  8. * @package Elasticsearch\Common\Exceptions\Curl
  9. * @author Bez Hermoso <bezalelhermoso@gmail.com>
  10. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
  11. * @link http://elastic.co
  12. */
  13. class JsonErrorException extends \Exception implements ElasticsearchException
  14. {
  15. /**
  16. * @var mixed
  17. */
  18. private $input;
  19. /**
  20. * @var mixed
  21. */
  22. private $result;
  23. private static $messages = array(
  24. JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
  25. JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
  26. JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
  27. JSON_ERROR_SYNTAX => 'Syntax error',
  28. JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
  29. JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded',
  30. JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded',
  31. JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given',
  32. // JSON_ERROR_* constant values that are available on PHP >= 7.0
  33. 9 => 'Decoding of value would result in invalid PHP property name', //JSON_ERROR_INVALID_PROPERTY_NAME
  34. 10 => 'Attempted to decode nonexistent UTF-16 code-point' //JSON_ERROR_UTF16
  35. );
  36. public function __construct($code, $input, $result, $previous = null)
  37. {
  38. if (isset(self::$messages[$code]) !== true) {
  39. throw new \InvalidArgumentException(sprintf('Encountered unknown JSON error code: [%d]', $code));
  40. }
  41. parent::__construct(self::$messages[$code], $code, $previous);
  42. $this->input = $input;
  43. $this->result = $result;
  44. }
  45. /**
  46. * @return mixed
  47. */
  48. public function getInput()
  49. {
  50. return $this->input;
  51. }
  52. /**
  53. * @return mixed
  54. */
  55. public function getResult()
  56. {
  57. return $this->result;
  58. }
  59. }