Improve json_return and move it to future
authorJoey Schulze <joey@infodrom.org>
Sun, 17 Jul 2016 07:46:56 +0000 (09:46 +0200)
committerJoey Schulze <joey@infodrom.org>
Sun, 17 Jul 2016 07:46:56 +0000 (09:46 +0200)
src/ajax.php
src/future.php

index 927ce94..c8926f4 100644 (file)
@@ -2,13 +2,6 @@
 require_once('config.php');
 require_once('future.php');
 
-function json_return($data)
-{
-  header('Content-type: application/json; charset="UTF-8"');
-  echo json_encode($data);
-  exit();
-}
-
 $backend = new AJAXBackend();
 
 $data = array();
index 8e35a80..1cf7bcb 100644 (file)
@@ -10,6 +10,24 @@ function __autoload($class)
   require_once($fname);
 }
 
+function json_return($data)
+{
+  header('Content-type: application/json; charset="UTF-8"');
+
+  $encoded = json_encode($data);
+
+  if ($encoded === false) {
+    error_log('Wrong encoding: ' . var_export($encoded,true));
+    return json_encode(array('status' => false,
+                            'error' => 'Cannot encode return values, see server log.'));
+  } else {
+    echo $encoded;
+  }
+
+  exit();
+}
+
+
 global $db;
 
 $dsn = sprintf('%s:host=%s;dbname=%s', DBDRIVER, DBHOST, DBNAME);