Awight has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/314462

Change subject: Throw an exception when we can't decode JSON
......................................................................

Throw an exception when we can't decode JSON

Bug: T147532
Change-Id: I684d517e73d74dd5e7e4682cdb72ff6b0c3fa7f4
---
M src/PHPQueue/Backend/Predis.php
A src/PHPQueue/Exception/JsonException.php
A src/PHPQueue/Json.php
3 files changed, 28 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/php-queue 
refs/changes/62/314462/1

diff --git a/src/PHPQueue/Backend/Predis.php b/src/PHPQueue/Backend/Predis.php
index e74e6a5..aaeb9bd 100644
--- a/src/PHPQueue/Backend/Predis.php
+++ b/src/PHPQueue/Backend/Predis.php
@@ -8,6 +8,7 @@
 use PHPQueue\Interfaces\AtomicReadBuffer;
 use PHPQueue\Interfaces\KeyValueStore;
 use PHPQueue\Interfaces\FifoQueueStore;
+use PHPQueue\Json;
 
 /**
  * Wraps several styles of redis use:
@@ -199,7 +200,7 @@
         $this->last_job_id = time();
         $this->afterGet();
 
-        return json_decode($data, true);
+        return Json::safe_decode($data);
     }
 
     public function popAtomic($callback) {
@@ -225,7 +226,7 @@
             $tx->multi();
 
             $data = $tx->lpop($self->queue_name);
-            $data = json_decode($data, true);
+            $data = Json::safe_decode($data);
             if ($data !== null) {
                 call_user_func($callback, $data);
             }
@@ -280,7 +281,7 @@
         $this->last_job_id = time();
         $this->afterGet();
 
-        return json_decode($data, true);
+        return Json::safe_decode($data);
     }
 
     public function release($jobId=null)
@@ -395,7 +396,7 @@
         $this->beforeGet($key);
         if ($this->order_key) {
             $data = $this->getConnection()->get($key);
-            return json_decode($data, true);
+            return Json::safe_decode($data);
         }
         $type = $this->getConnection()->type($key);
         switch ($type) {
diff --git a/src/PHPQueue/Exception/JsonException.php 
b/src/PHPQueue/Exception/JsonException.php
new file mode 100644
index 0000000..76ebd1e
--- /dev/null
+++ b/src/PHPQueue/Exception/JsonException.php
@@ -0,0 +1,9 @@
+<?php
+namespace PHPQueue\Exception;
+
+/**
+ * Something bad with the JSON
+ */
+class JsonException extends PHPQueue\Exception\Exception
+{
+}
diff --git a/src/PHPQueue/Json.php b/src/PHPQueue/Json.php
new file mode 100644
index 0000000..ad518ed
--- /dev/null
+++ b/src/PHPQueue/Json.php
@@ -0,0 +1,14 @@
+<?php
+namespace PHPQueue;
+
+use PHPQueue\Exception\JsonException;
+
+class Json {
+    public static function safe_decode( $text ) {
+        $data = json_decode($text, true);
+        if ( $data === null ) {
+            throw new JsonException("JSON could not be decoded: '{$text}'");
+        }
+        return $data;
+    }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/314462
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I684d517e73d74dd5e7e4682cdb72ff6b0c3fa7f4
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/php-queue
Gerrit-Branch: master
Gerrit-Owner: Awight <awi...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to