better error handling. fallback widget

Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
This commit is contained in:
Benny Samir Hierl 2020-10-22 09:39:05 +02:00
parent f4addec5d2
commit 1b24b4e209
No known key found for this signature in database
GPG key ID: 69DE3C3C097DB7F7

View file

@ -2,9 +2,21 @@
// These must be at the very top of the file. Do not edit. // These must be at the very top of the file. Do not edit.
// icon-color: teal; icon-glyph: magic; // icon-color: teal; icon-glyph: magic;
// Credits: /**************
// - Sillium@GitHub (https://gist.github.com/Sillium/f904fb89444bc8dde12cfc07b8fa8728) Version 1.0.1
// - Chaeimg@Github (https://github.com/chaeimg/battCircle)
Changelog:
v1.0.1:
- Better Error handling
- Better logging
- Fallback Widget screen in case of an error
If you have problems or need help, ask for support here: https://github.com/ThisIsBenny/iOS-Widgets/issues
Credits:
- Sillium@GitHub (https://gist.github.com/Sillium/f904fb89444bc8dde12cfc07b8fa8728)
- Chaeimg@Github (https://github.com/chaeimg/battCircle)
**************/
// How many minutes should the cache be valid // How many minutes should the cache be valid
let cacheMinutes = 60; let cacheMinutes = 60;
@ -101,12 +113,10 @@ async function getSessionCookies() {
}) })
try { try {
let res = await req.loadJSON() let res = await req.loadJSON()
console.log("Login sucessfull")
return { cookies: req.response.cookies, msisdn: res.msisdn} return { cookies: req.response.cookies, msisdn: res.msisdn}
} catch (e) { } catch (e) {
console.log("Login vailed! Please check if Wifi is disabled.") console.log("Login vailed! Please check if Wifi is disabled.")
console.log(e) throw new Error(`Login failed with HTTP-Status-Code ${req.response.statusCode}`)
throw e
} }
}; };
@ -130,15 +140,26 @@ async function getUsage() {
let datenContainer = res['serviceUsageVBO']['usageAccounts'][0]['usageGroup'].find(function(v){ let datenContainer = res['serviceUsageVBO']['usageAccounts'][0]['usageGroup'].find(function(v){
return v.container == "Daten" return v.container == "Daten"
}) })
if (datenContainer === undefined) {
throw new Error("Can't find usageGroup 'Daten'")
} else {
console.log("usageGroup 'Daten' founded") console.log("usageGroup 'Daten' founded")
}
let datenvolumen = datenContainer.usage.find(function(v){ let datenvolumen = datenContainer.usage.find(function(v){
return v.code == "-1" return v.code == "-1"
}) })
console.log(datenvolumen)
if (datenvolumen === undefined) {
console.log("Can't find Usage with Code -1. Please check the following log to find the correct code for your case and addjust the code to 4 lines above: " + JSON.stringify(datenContainer.usage))
throw new Error("Can't find Usage with Code -1.")
}
return { return {
total: datenvolumen.total || 0, total: datenvolumen.total,
used: datenvolumen.used || 0, used: datenvolumen.used,
remaining: datenvolumen.remaining || 0 remaining: datenvolumen.remaining
} }
} catch (e) { } catch (e) {
console.log("Loading usage data failed") console.log("Loading usage data failed")
@ -181,18 +202,21 @@ try {
} }
} catch (e) { } catch (e) {
console.log(e) console.log(e)
if (cacheExists) {
console.log("Get from Cache") console.log("Get from Cache")
data = JSON.parse(files.readString(cachePath)) data = JSON.parse(files.readString(cachePath))
lastUpdate = cacheDate lastUpdate = cacheDate
} else {
console.log("No fallback to cache possible. Due to missing cache.")
}
} }
console.log(data)
// Create Widget // Create Widget
let widget = new ListWidget(); let widget = new ListWidget();
widget.setPadding(10, 10, 10, 10) widget.setPadding(10, 10, 10, 10)
if (data !== undefined) {
if (useGradient) { if (useGradient) {
const gradient = new LinearGradient() const gradient = new LinearGradient()
gradient.locations = [0, 1] gradient.locations = [0, 1]
@ -252,6 +276,11 @@ lastUpdateText.font = Font.mediumSystemFont(10)
lastUpdateText.centerAlignText() lastUpdateText.centerAlignText()
lastUpdateText.applyTimeStyle() lastUpdateText.applyTimeStyle()
lastUpdateText.textColor = Color.lightGray() lastUpdateText.textColor = Color.lightGray()
} else {
let fallbackText = widget.addText("Es ist ein Fehler aufgetreten! Bitte prüfen Sie die Logs direkt in der App.")
fallbackText.font = Font.mediumSystemFont(12)
fallbackText.textColor = new Color(textColor)
}
if(!config.runsInWidget) { if(!config.runsInWidget) {
await widget.presentSmall() await widget.presentSmall()