08-15-2021 04:01
08-15-2021 04:01
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
08-26-2021 03:58
08-26-2021 03:58
Would need to see some code, but you just need to go to a different code path if getWeatherData throws an error.
08-26-2021 04:19
08-26-2021 04:19
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
08-26-2021 05:04
08-26-2021 05:04
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
}
}
08-27-2021 04:17
08-27-2021 04:17
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.
05-20-2022 21:54
05-20-2022 21:54
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