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

16
dist/callchain.js vendored
View File

@ -38,7 +38,12 @@ class AAsyncBaseChainItem extends AChainItem {
}
this.addListener('yourturn', (message) => {
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}`);
}
});
}
}
@ -53,8 +58,13 @@ class ABaseChainItem extends AChainItem {
}
this.addListener('yourturn', (message) => {
log.info(`Executing ${this.toString()}`);
let result = this.func(message);
this.next.send(result);
try {
let result = this.func(message);
this.next.send(result);
}
catch (e) {
log.error(`Chain interrupted: ${e}`);
}
});
}
}

View File

@ -1,6 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const log = require("./log");
const utils = require("./utils");
class EspThermError extends Error {
constructor(message) {
super(message);
}
}
exports.EspThermError = EspThermError;
class EspThermData {
constructor(temperature, voltage, timeConsumed) {
this.temperature = temperature;
@ -44,7 +51,24 @@ exports.EspThermMessage = EspThermMessage;
function espThermToJson(message) {
let messageStr = "" + message;
let parts = messageStr.split(' ');
let 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 = new EspThermMessage(clientId, temperature, voltage, timeConsumed);
return espThermMessage;
}
exports.espThermToJson = espThermToJson;

2
dist/main.js vendored
View File

@ -9,7 +9,7 @@ const MissingEventDetector = require("./missingeventdetector");
log.info("Dispatcher starting");
config.readConfig();
let dispatcher = new mqtt.MqttDispatcher(config.dict.brokerUrl, config.dict.brokerUser, config.dict.brokerPass, config.dict.brokerCa);
const ESP_THERM_TOPIC = 'IoT/espThermometer2/#';
const ESP_THERM_TOPIC = 'IoT/espThermometer3/#';
dispatcher.register(ESP_THERM_TOPIC, 'toJson', EspThermToJson.espThermToJson);
let missingeventdetector = new MissingEventDetector.MissingEventDetector();
dispatcher.register(ESP_THERM_TOPIC, 'MissingEventDetector', missingeventdetector);