Skip to content

Commit

Permalink
Throw exception instead of supressing error reporting momentarily.
Browse files Browse the repository at this point in the history
  • Loading branch information
bezhermoso authored and polyfractal committed Nov 6, 2014
1 parent 5d90aaa commit aadb82b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Elasticsearch\Common\Exceptions\Serializer;

use Elasticsearch\Common\Exceptions\ElasticsearchException;

/**
* Class JsonErrorException
*
* @author Bez Hermoso <[email protected]>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
* @link http://elasticsearch.org
*/
class JsonErrorException extends \Exception implements ElasticsearchException
{
private $input;

private $result;

private static $messages = array(
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',

// JSON_ERROR_* constant values that are available on PHP >= 5.5.0
6 => 'One or more recursive references in the value to be encoded',
7 => 'One or more NAN or INF values in the value to be encoded',
8 => 'A value of a type that cannot be encoded was given',

);

public function __construct($code, $input, $result, $previous = null)
{
if (!isset(self::$messages[$code])) {
throw new \InvalidArgumentException('Invalid JSON error code.');
}

parent::__construct(self::$messages[$code], $code, $previous);
$this->input = $input;
$this->result = $result;
}

/**
* @return mixed
*/
public function getInput()
{
return $this->input;
}

/**
* @return mixed
*/
public function getResult()
{
return $this->result;
}
}
18 changes: 12 additions & 6 deletions src/Elasticsearch/Serializers/SmartSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Elasticsearch\Serializers;

use Elasticsearch\Common\Exceptions\Serializer\JsonErrorException;

/**
* Class SmartSerializer
*
Expand Down Expand Up @@ -51,20 +53,24 @@ public function serialize($data)
* @param string $data JSON encoded string
* @param array $headers Response Headers
*
* @throws JsonErrorException
* @return array
*/
public function deserialize($data, $headers)
{
if (isset($headers['content_type']) === true) {
if (strpos($headers['content_type'], 'json') !== false) {

// Unusually high/low scores may cause integer overflows. Supress E_NOTICEs momentarily.
$e = error_reporting();
error_reporting($e ^ E_NOTICE);
$decoded = json_decode($data, true);
error_reporting($e);
$result = @json_decode($data, true);

// Throw exception only if E_NOTICE is on to maintain backwards-compatibility on systems that silently ignore E_NOTICEs.
if (json_last_error() !== JSON_ERROR_NONE && (error_reporting() & E_NOTICE) === E_NOTICE) {
$e = new JsonErrorException(json_last_error(), $data, $result);
throw $e;
}

return $result;

return $decoded;
} else {
//Not json, return as string
return $data;
Expand Down

0 comments on commit aadb82b

Please sign in to comment.