From 0535dbf3f39fefbce4d18266409ea68d9af0fbda Mon Sep 17 00:00:00 2001 From: Urban Soban Date: Thu, 30 Mar 2017 12:17:14 +0100 Subject: [PATCH 1/3] Raising JsonEncodeException when json_encode fails. --- spec/SidekiqJob/SerializerSpec.php | 9 +++++++ src/JsonEncodeException.php | 39 ++++++++++++++++++++++++++++++ src/Serializer.php | 22 +++++++++++------ 3 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 src/JsonEncodeException.php diff --git a/spec/SidekiqJob/SerializerSpec.php b/spec/SidekiqJob/SerializerSpec.php index 7aa307e..f30c6f0 100644 --- a/spec/SidekiqJob/SerializerSpec.php +++ b/spec/SidekiqJob/SerializerSpec.php @@ -4,6 +4,7 @@ use PhpSpec\ObjectBehavior; use Prophecy\Argument; +use SidekiqJob\JsonEncodeException; class SerializerSpec extends ObjectBehavior { @@ -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(); diff --git a/src/JsonEncodeException.php b/src/JsonEncodeException.php new file mode 100644 index 0000000..245c1c0 --- /dev/null +++ b/src/JsonEncodeException.php @@ -0,0 +1,39 @@ +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; + } +} \ No newline at end of file diff --git a/src/Serializer.php b/src/Serializer.php index 45aaec4..05a3f69 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -14,23 +14,31 @@ 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) { $class = is_object($class) ? get_class($class) : $class; $data = [ - 'class' => $class, - 'jid' => $jobId, - 'created_at' => microtime(true), + 'class' => $class, + 'jid' => $jobId, + 'created_at' => microtime(true), 'enqueued_at' => microtime(true), - 'args' => $args, - 'retry' => $retry, + 'args' => $args, + '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; } /** From 10656001f657dc8d995d3cd7fe18894c84857d43 Mon Sep 17 00:00:00 2001 From: Urban Soban Date: Thu, 30 Mar 2017 12:23:37 +0100 Subject: [PATCH 2/3] Reverted formatting. --- src/Serializer.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Serializer.php b/src/Serializer.php index 05a3f69..1391be7 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -24,12 +24,12 @@ public function serialize($jobId, $class, $args = [], $retry = true) $class = is_object($class) ? get_class($class) : $class; $data = [ - 'class' => $class, - 'jid' => $jobId, - 'created_at' => microtime(true), + 'class' => $class, + 'jid' => $jobId, + 'created_at' => microtime(true), 'enqueued_at' => microtime(true), - 'args' => $args, - 'retry' => $retry, + 'args' => $args, + 'retry' => $retry, ]; $jsonEncodedData = json_encode($data); From 878b219471943954075c75970a936e70ffbe8110 Mon Sep 17 00:00:00 2001 From: Urban Soban Date: Thu, 30 Mar 2017 12:24:38 +0100 Subject: [PATCH 3/3] Reverted formatting. --- spec/SidekiqJob/SerializerSpec.php | 2 +- src/JsonEncodeException.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/SidekiqJob/SerializerSpec.php b/spec/SidekiqJob/SerializerSpec.php index f30c6f0..a52d73e 100644 --- a/spec/SidekiqJob/SerializerSpec.php +++ b/spec/SidekiqJob/SerializerSpec.php @@ -30,7 +30,7 @@ function is_throws_exception_if_not_valid_arguments() function it_throws_exception_if_arguments_not_json_encodable() { - $badJobArguments = ['bad' => "\xc3\x28"]; + $badJobArguments = ['bad' => "\xc3\x28"]; $jobSerializeArguments = [1, new \stdClass(), $badJobArguments, true]; $this->shouldThrow(JsonEncodeException::class)->during('serialize', $jobSerializeArguments); diff --git a/src/JsonEncodeException.php b/src/JsonEncodeException.php index 245c1c0..a42fd5e 100644 --- a/src/JsonEncodeException.php +++ b/src/JsonEncodeException.php @@ -23,8 +23,8 @@ class JsonEncodeException extends \Exception public function __construct($nonJsonEncodableData, $jsonErrorCode, $jsonErrorMessage, \Exception $previous = null) { $this->nonJsonEncodableData = $nonJsonEncodableData; - $this->jsonErrorCode = $jsonErrorCode; - $this->jsonErrorMessage = $jsonErrorMessage; + $this->jsonErrorCode = $jsonErrorCode; + $this->jsonErrorMessage = $jsonErrorMessage; parent::__construct(sprintf('Json encode error [%d]: %s', $jsonErrorCode, $jsonErrorMessage), 0, $previous); }