From c99ca318de30ec40a1b55b71129532d6d901ad72 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Sun, 17 Feb 2019 13:38:03 +0100 Subject: [PATCH] improved compiler error handling --- src/providers/compiler/compiler.ts | 19 +++++++++++-------- src/providers/ota-wifi/ota-wifi.ts | 23 +++++++++++++++-------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/providers/compiler/compiler.ts b/src/providers/compiler/compiler.ts index 596b4d8..4bfe93c 100644 --- a/src/providers/compiler/compiler.ts +++ b/src/providers/compiler/compiler.ts @@ -8,11 +8,9 @@ const URL = "https://compiler.sensebox.de" @Injectable() export class CompilerProvider { - constructor(public http: HttpClient) { - console.log('Hello CompilerProvider Provider'); - } + constructor(public http: HttpClient) { } - async callcompiler(sketch : string): Promise { + async compile(sketch : string): Promise { const headers = new HttpHeaders({'Content-Type': 'application/json'} ); const data = { board: 'sensebox-mcu', sketch } @@ -27,10 +25,15 @@ export class CompilerProvider { try { // attempt to extract the compilation error message and clean it up - msg = JSON.parse(err.error.message).process - msg = `compilation error: ${msg.substr(msg.indexOf(' ') + 14)}` - msg = msg.substr(0, msg.indexOf('^')) - } catch (err) {} + console.error(err) + msg = JSON.parse(err.error.message) + if (msg.process) { + msg = `compilation error: ${msg.process.substr(msg.process.indexOf(' '))}` + msg = msg.substr(0, msg.indexOf('^')) + } + } catch (err2) { + console.error(err2) + } throw Error(msg) }) // download the resulting sketch binary diff --git a/src/providers/ota-wifi/ota-wifi.ts b/src/providers/ota-wifi/ota-wifi.ts index 3559d57..3176dce 100644 --- a/src/providers/ota-wifi/ota-wifi.ts +++ b/src/providers/ota-wifi/ota-wifi.ts @@ -43,14 +43,21 @@ export class OtaWifiProvider { if (this.strategy != WifiStrategy.Automatic) throw new Error('can not search for WiFi networks on this platform') - return WifiWizard2.scan() - .then(networks => { - if (filterSsids) - networks = networks.filter(n => n.SSID.toLowerCase().includes(SSID_PREFIX)) - networks = networks.filter(n => n.capabilities.includes('[ESS]')) + try { + let networks = await WifiWizard2.scan() + if (filterSsids) + networks = networks.filter(n => n.SSID.toLowerCase().includes(SSID_PREFIX)) - return networks.map(n => n.SSID) - }) + return networks + .filter(n => n.capabilities.includes('[ESS]')) // only unencrypted networks + .map(n => n.SSID) + .filter((val, i, arr) => arr.indexOf(val) === i) // unique filter (as networks are duplicated per BSSID) + } catch (err) { + if (err === 'SCAN_FAILED') + throw new Error('WiFi scan failed. Maybe location services are disabled or the location permission isn\'t set for this app?') + else + throw new Error(err) + } } async connectToSensebox (ssid: string): Promise { @@ -59,7 +66,7 @@ export class OtaWifiProvider { return this.platform.is('ios') ? WifiWizard2.iOSConnectNetwork(ssid) - : WifiWizard2.connect(ssid, true) + : WifiWizard2.connect(ssid, true) // true: bind to all connections, even without internet } async uploadFirmware (binary: ArrayBuffer): Promise {