Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Catch weather errors accurately

Currently I'm using the Weather API introduced in SDK 4.3.0 and used the catch function to collect errors.

This error is sent to my function which checks if the error is "Error: Cannot fetch current location".

However it doesn't say true even though the console prints out "Companion: Error: Cannot fetch current location"

Does anyone have a reliable method of catching these errors?

Thank you

 

Best Answer
0 Votes
5 REPLIES 5

Would need to see some code, but you just need to go to a different code path if getWeatherData throws an error.

Best Answer
0 Votes

weather.catch(ex) => console.error(ex); checkError(ex);

 

checkError is a function I created to detect errors

 

function checkError(data) {
let test = data;
console.log(test);
if (checkError === "Error: Cannot fetch current location") {
console.log("trigger success");
}
}
however it doesn't work

 

Best Answer
0 Votes
import { me as companion } from "companion";
import weather from "weather";

if (companion.permissions.granted("access_location")) {
   weather
     .getWeatherData()
     .then(data => {
       if (data.locations.length > 0) {
         const temp = Math.floor(data.locations[0].currentWeather.temperature);
         const cond = data.locations[0].currentWeather.weatherCondition;
         const loc = data.locations[0].name;
         const unit = data.temperatureUnit;
         console.log(`It's ${temp}\u00B0 ${unit} and ${cond}
 in ${loc}`);
       }
     })
     .catch(ex => {
       checkError(ex);
     });
}

function checkError(ex) {
       console.error(ex);
       // You don't need to check the reason here, it already failed
       if (ex === "Error: Cannot fetch current location") {
              console.log("trigger success");  //why? it failed already
       }
}
Best Answer
0 Votes

I'm using the trigger to catch different weather errors, that's all but if I just need to catch the error that works I guess.

Best Answer
0 Votes

I am having this too, and it makes my clockface crash.

It is important for my clockFace to have weather data, and i know how to catch the error, but i need to fix it

My code is:

 

if (companion.permissions.granted("access_location")) {
   weather.getWeatherData().then((data) => {
       if (data.locations.length > 0) {
         let temp = 111;
         let tempPrev = -222;
         tempPrev = temp;
         temp = Math.floor(data.locations[0].currentWeather.temperature);
         const cond = gettext(util.findWeatherConditionName(WeatherCondition,data.locations[0].currentWeather.weatherCondition));
         console.log(util.findWeatherConditionName(WeatherCondition,data.locations[0].currentWeather.weatherCondition));
         const loc = data.locations[0].name;
         const unit = data.temperatureUnit;

 

 And it throws the (catched) error:

Error: Cannot fetch current location

Phone: OnePlus Nord Android 11, Watch: Versa 3
Best Answer