error handling

This commit is contained in:
Wolfgang Hottgenroth
2017-08-28 15:00:30 +02:00
parent 1cf6c76a7d
commit f44a7109b4
7 changed files with 132 additions and 12 deletions

View File

@ -59,7 +59,11 @@ export abstract class AAsyncBaseChainItem extends AChainItem {
}
this.addListener('yourturn', (message : any) : void => {
log.info(`Executing ${this.toString()}`)
this.func(message, this.next.send)
try {
this.func(message, this.next.send)
} catch (e) {
log.error(`Chain interrupted: ${e}`)
}
})
}
}
@ -77,8 +81,12 @@ export abstract class ABaseChainItem extends AChainItem {
}
this.addListener('yourturn', (message : any) : void => {
log.info(`Executing ${this.toString()}`)
let result : any = this.func(message)
this.next.send(result)
try {
let result : any = this.func(message)
this.next.send(result)
} catch (e) {
log.error(`Chain interrupted: ${e}`)
}
})
}
}

View File

@ -1,6 +1,12 @@
import * as log from './log'
import * as utils from './utils'
export class EspThermError extends Error {
constructor(message : any) {
super(message)
}
}
export class EspThermData {
private temperature : number
private voltage : number
@ -61,8 +67,30 @@ export class EspThermMessage {
export function espThermToJson(message : any) : any {
let messageStr : string = "" + <string>message
let parts : string[] = messageStr.split(' ')
let espThermMessage : EspThermMessage = new EspThermMessage(parts[0],
parseFloat(parts[1]), parseFloat(parts[2]),
parseInt(parts[3]))
if (parts.length != 4) {
throw new EspThermError(`too few or too much, ${parts.length} elements in message, it was "${message}"`)
}
let clientId = parts[0]
let temperature = parseFloat(parts[1])
log.info(`temperature: ${temperature}`)
if ((typeof(temperature) != "number") || (temperature == null) || isNaN(temperature)) {
throw new EspThermError(`temperature element should be a number, message was "${message}"`)
}
let voltage = parseFloat(parts[2])
if ((typeof(voltage) != "number") || (voltage == null) || isNaN(voltage)) {
throw new EspThermError(`voltage element should be a number, message was "${message}"`)
}
let timeConsumed = parseInt(parts[3])
if ((typeof(timeConsumed) != "number") || (timeConsumed == null) || isNaN(timeConsumed)) {
throw new EspThermError(`timeConsumed element should be a number, message was "${message}"`)
}
let espThermMessage : EspThermMessage = new EspThermMessage(clientId,
temperature, voltage, timeConsumed)
return espThermMessage
}

View File

@ -17,7 +17,7 @@ config.readConfig()
let dispatcher = new mqtt.MqttDispatcher(config.dict.brokerUrl,
config.dict.brokerUser, config.dict.brokerPass, config.dict.brokerCa)
const ESP_THERM_TOPIC : string = 'IoT/espThermometer2/#'
const ESP_THERM_TOPIC : string = 'IoT/espThermometer3/#'
dispatcher.register(ESP_THERM_TOPIC, 'toJson', EspThermToJson.espThermToJson)
let missingeventdetector : MissingEventDetector.MissingEventDetector =