Add error handling to graf loading request

Right now the graf isn't loading because of a cert error. This change
adds error handling so the error is displayed to the user.

Change-Id: I424e44aa21a611bb02bd70d4b56431b6dc249d26
diff --git a/api.php b/api.php
index 176fdfe..3fdcf7b 100644
--- a/api.php
+++ b/api.php
@@ -8,6 +8,7 @@
   }
 
   public static function error($n, $msg) {
+    http_response_code(400);
     self::output(["error" => $n, "msg" => $msg]);
   }
 }
@@ -23,11 +24,25 @@
 
 switch ($_GET["action"]) {
   case "getgraf":
-  $graf = file_get_contents($conf["apiurl"]);
-  echo $graf;
-  break;
+    $ch = curl_init($conf["apiurl"]);
+
+    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+    curl_setopt($ch, CURLOPT_FAILONERROR, true);
+
+    $graf = curl_exec($ch);
+    $error = curl_errno($ch);
+    if ($error)
+      $errorMsg = curl_error($ch);
+
+    curl_close($ch);
+
+    if ($error)
+      write::error(3, "Error while retrieving Graf from dirbaio's website: ".$errorMsg);
+
+    echo $graf;
+    break;
 
   default:
-  write::error(2, "Unknown action");
+    write::error(2, "Unknown action");
 }
 ?>
diff --git a/js/graf.js b/js/graf.js
index 66d7cbd..4fc4b39 100644
--- a/js/graf.js
+++ b/js/graf.js
@@ -15,8 +15,13 @@
     }

   }

   http.onload = function() {

-    if(this.status != 200) {

+    if (this.status != 200) {

       console.warn("Attention, status code "+this.status+" when loading via xhr url "+url);

+

+      console.log(response);

+      if (('error' in response) && ('msg' in response)) {

+        alert(response.msg);

+      }

     }

     callback(this.responseText, this.status);

   };