From 657af43f4f94423aa25d6177b5770df573f2327f Mon Sep 17 00:00:00 2001 From: markn Date: Mon, 7 Nov 2022 12:38:31 -0800 Subject: [PATCH] Add error checking for statusCode 403. For case of `API key expires`. --- index.js | 13 ++++++++++--- views/pages/weatherbit.ejs | 7 +++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index f003102..7648b20 100644 --- a/index.js +++ b/index.js @@ -277,12 +277,15 @@ function getWeatherBitDailyForecast(city){ // console.log(uriWeatherBitStr); try { request(uriWeatherBitStr, async function (err, response, body) { - console.log(response.statusCode); - if (response.statusCode == 429) { console.log("WARNING: You have exceeded your API call limit with weatherbit.io!"); resolve(null); } + if (response.statusCode == 403) { + console.log("WARNING: Free plan limit is used up. Data will be available again on the 3rd of next month."); + retCode = JSON.parse(body); + resolve(retCode); + } if (response.statusCode == 200) { retCode = await JSON.parse(body); resolve(retCode); @@ -362,8 +365,12 @@ async function gatherWeatheBits(city) { ]); + // Check is the service trial expired + if (dailyForecast.error === 'API key not valid, or not yet activated.') { + return { locale: city, alertStatus: 403, alertData: null, curStatus: 403, curData: null, foreStatus: 403, foreData: null, airqStatus: 403, airqData: null, error: 'Free plan has reached its limit. Data will be available again on the 3rd of next month.' }; + } // Make sure all promisses fulfilled. - if (dailyForecast !== null && alerts !== null && airQuality !== null && currentConditions !== null ) { + else if (dailyForecast !== null && alerts !== null && airQuality !== null && currentConditions !== null ) { let currentHour = new Date().getHours(); let aStatus = 200; let aData = alerts; diff --git a/views/pages/weatherbit.ejs b/views/pages/weatherbit.ejs index e30ded5..67b402f 100644 --- a/views/pages/weatherbit.ejs +++ b/views/pages/weatherbit.ejs @@ -31,6 +31,8 @@
<% if( typeof(error)==="undefined" || curStatus===400 ){ %>

Waiting for data.

+ <% } else if ( typeof(error)!="undefined" && curStatus===403 ) { %> +

We apologize. <%= error %>

<% } else { %>

City: <%= curData.city_name %> , <%= curData.state_code %> @@ -122,8 +124,11 @@

Current Air Quality
+ <% if( typeof(error)==="undefined" || airqStatus===400 ) { %>

Waiting for data.

+ <% } else if ( typeof(error)!="undefined" && airqStatus===403 ) { %> +

<%= error %>

<% } else { %>

Local hourly: <%= airqData.timestamp_local.split('T')[1] %>

@@ -222,6 +227,8 @@
<% if( typeof(error)==="undefined" || foreStatus===400 ){ %>

Waiting for data.

+ <% } else if ( typeof(error)!="undefined" && foreStatus===403 ) { %> +

<%= error %>

<% } else { %>
<%= trendingTemperature(foreData.data) %>