Skip to content

Commit 657af43

Browse files
committed
Add error checking for statusCode 403. For case of API key expires.
1 parent 3de9b08 commit 657af43

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,15 @@ function getWeatherBitDailyForecast(city){
277277
// console.log(uriWeatherBitStr);
278278
try {
279279
request(uriWeatherBitStr, async function (err, response, body) {
280-
console.log(response.statusCode);
281-
282280
if (response.statusCode == 429) {
283281
console.log("WARNING: You have exceeded your API call limit with weatherbit.io!");
284282
resolve(null);
285283
}
284+
if (response.statusCode == 403) {
285+
console.log("WARNING: Free plan limit is used up. Data will be available again on the 3rd of next month.");
286+
retCode = JSON.parse(body);
287+
resolve(retCode);
288+
}
286289
if (response.statusCode == 200) {
287290
retCode = await JSON.parse(body);
288291
resolve(retCode);
@@ -362,8 +365,12 @@ async function gatherWeatheBits(city) {
362365
]);
363366

364367

368+
// Check is the service trial expired
369+
if (dailyForecast.error === 'API key not valid, or not yet activated.') {
370+
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.' };
371+
}
365372
// Make sure all promisses fulfilled.
366-
if (dailyForecast !== null && alerts !== null && airQuality !== null && currentConditions !== null ) {
373+
else if (dailyForecast !== null && alerts !== null && airQuality !== null && currentConditions !== null ) {
367374
let currentHour = new Date().getHours();
368375
let aStatus = 200;
369376
let aData = alerts;

views/pages/weatherbit.ejs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
<div class="jumbotron jumbotron-fluid" style="padding:10px 5px 15px 20px;">
3232
<% if( typeof(error)==="undefined" || curStatus===400 ){ %>
3333
<p style="color:gray"> Waiting for data.</p>
34+
<% } else if ( typeof(error)!="undefined" && curStatus===403 ) { %>
35+
<p style="color:gray">We apologize. <%= error %></p>
3436
<% } else { %>
3537
3638
<p style="color:gray"><strong>City</strong>: <%= curData.city_name %> , <%= curData.state_code %>
@@ -122,8 +124,11 @@
122124
<div class="col-sm-6">
123125
<h5>Current Air Quality</h5>
124126
<div class="jumbotron jumbotron-fluid" style="padding:10px 5px 15px 20px;">
127+
125128
<% if( typeof(error)==="undefined" || airqStatus===400 ) { %>
126129
<p style="color:gray"> Waiting for data.</p>
130+
<% } else if ( typeof(error)!="undefined" && airqStatus===403 ) { %>
131+
<p style="color:gray"> <%= error %></p>
127132
<% } else { %>
128133
<p style="color:gray"><strong>Local hourly</strong>: <%= airqData.timestamp_local.split('T')[1] %>
129134
</p>
@@ -222,6 +227,8 @@
222227
<div class="jumbotron jumbotron-fluid" style="padding:10px 5px 15px 20px; overflow-x:auto;">
223228
<% if( typeof(error)==="undefined" || foreStatus===400 ){ %>
224229
<p style="color:gray"> Waiting for data.</p>
230+
<% } else if ( typeof(error)!="undefined" && foreStatus===403 ) { %>
231+
<p style="color:gray"> <%= error %></p>
225232
<% } else { %>
226233
<div style="color:rgb(18, 83, 175);"><%= trendingTemperature(foreData.data) %></div>
227234
<table class="styled-table">

0 commit comments

Comments
 (0)