Skip to content

Commit

Permalink
Merge pull request #5 from usoban/json-encode-error-handling
Browse files Browse the repository at this point in the history
Raising JsonEncodeException when json_encode fails.
  • Loading branch information
spinx authored Mar 30, 2017
2 parents 860c91d + 878b219 commit 2afe43d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
9 changes: 9 additions & 0 deletions spec/SidekiqJob/SerializerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use SidekiqJob\JsonEncodeException;

class SerializerSpec extends ObjectBehavior
{
Expand All @@ -27,6 +28,14 @@ function is_throws_exception_if_not_valid_arguments()
$this->serialize([], true, false)->shouldThrowExeption();
}

function it_throws_exception_if_arguments_not_json_encodable()
{
$badJobArguments = ['bad' => "\xc3\x28"];
$jobSerializeArguments = [1, new \stdClass(), $badJobArguments, true];

$this->shouldThrow(JsonEncodeException::class)->during('serialize', $jobSerializeArguments);
}

function it_returns_string(){
$result = $this->serialize($this->_get_valid_array()['jobid'], $this->_get_valid_array()['class'], $this->_get_valid_array()['args']);
$result->shouldBeString();
Expand Down
39 changes: 39 additions & 0 deletions src/JsonEncodeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace SidekiqJob;

/**
* @package SidekiqJob
*/
class JsonEncodeException extends \Exception
{
/** @var mixed */
private $nonJsonEncodableData;
/** @var int */
private $jsonErrorCode;
/** @var string */
private $jsonErrorMessage;

/**
* @param string $nonJsonEncodableData
* @param int $jsonErrorCode
* @param string $jsonErrorMessage
* @param \Exception|null $previous
*/
public function __construct($nonJsonEncodableData, $jsonErrorCode, $jsonErrorMessage, \Exception $previous = null)
{
$this->nonJsonEncodableData = $nonJsonEncodableData;
$this->jsonErrorCode = $jsonErrorCode;
$this->jsonErrorMessage = $jsonErrorMessage;

parent::__construct(sprintf('Json encode error [%d]: %s', $jsonErrorCode, $jsonErrorMessage), 0, $previous);
}

/**
* @return mixed
*/
public function getNonJsonEncodableData()
{
return $this->nonJsonEncodableData;
}
}
12 changes: 10 additions & 2 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class Serializer
* @param string $jobId
* @param object|string $class
* @param array $args
* @param bool $retry
*
* @return string
* @throws exception Exception
* @throws JsonEncodeException
*/
public function serialize($jobId, $class, $args = [], $retry = true)
{
Expand All @@ -30,7 +32,13 @@ public function serialize($jobId, $class, $args = [], $retry = true)
'retry' => $retry,
];

return json_encode($data);
$jsonEncodedData = json_encode($data);

if ($jsonEncodedData === false) {
throw new JsonEncodeException($data, json_last_error(), json_last_error_msg());
}

return $jsonEncodedData;
}

/**
Expand Down

0 comments on commit 2afe43d

Please sign in to comment.