From e2f385b30d60bce643a9c4ba75912a1283cb83a4 Mon Sep 17 00:00:00 2001
From: Erick Hitter <services@ethitter.com>
Date: Fri, 13 Jan 2017 20:51:19 -0800
Subject: [PATCH] Return an error code when an API error occurs, rather than an
 empty "success"

---
 index.js | 41 ++++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 19 deletions(-)

diff --git a/index.js b/index.js
index 40938f3..3256f91 100644
--- a/index.js
+++ b/index.js
@@ -44,27 +44,30 @@ app.post('/post', function(req, res){
   console.log(parsed_url);
 
   request(parsed_url, function (error, response, body) {
-    if (!error && response.statusCode == 200) {
-      var data = JSON.parse(body);
-      var temperature = data.current_observation.temperature_string;
-      var weatherCondition = data.current_observation.weather;
-      var icon_url = data.current_observation.icon_url;
-      var location = data.current_observation.display_location.full;
+    if ( error || 200 !== response.statusCode ) {
+      return res.sendStatus( 500 );
+    }
 
-      var body = {
-        response_type: "in_channel",
-        "attachments": [
-          {
-            "text": "Location: " + location + "\n"
-                  + "Temperature: " + temperature + "\n"
-                  + "Condition: " + weatherCondition,
-            "image_url": icon_url,
-          }
-        ]
-      };
+    // Get current conditions first
+    var conditionData = JSON.parse( body );
+    var temperature = conditionData.current_observation.temperature_string;
+    var weatherCondition = conditionData.current_observation.weather;
+    var icon_url = conditionData.current_observation.icon_url;
+    var location = conditionData.current_observation.display_location.full;
 
-      res.send(body);
-    }
+    var body = {
+      response_type: "in_channel",
+      "attachments": [
+        {
+          "text": "Location: " + location + "\n"
+                + "Temperature: " + temperature + "\n"
+                + "Condition: " + weatherCondition,
+          "image_url": icon_url,
+        }
+      ]
+    };
+
+    res.send( body );
   });
 });
 
-- 
GitLab