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

10
dist/callchain.js vendored
View File

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

View File

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

2
dist/main.js vendored
View File

@ -9,7 +9,7 @@ const MissingEventDetector = require("./missingeventdetector");
log.info("Dispatcher starting"); log.info("Dispatcher starting");
config.readConfig(); config.readConfig();
let dispatcher = new mqtt.MqttDispatcher(config.dict.brokerUrl, config.dict.brokerUser, config.dict.brokerPass, config.dict.brokerCa); 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); dispatcher.register(ESP_THERM_TOPIC, 'toJson', EspThermToJson.espThermToJson);
let missingeventdetector = new MissingEventDetector.MissingEventDetector(); let missingeventdetector = new MissingEventDetector.MissingEventDetector();
dispatcher.register(ESP_THERM_TOPIC, 'MissingEventDetector', missingeventdetector); dispatcher.register(ESP_THERM_TOPIC, 'MissingEventDetector', missingeventdetector);

50
npm-debug.log Normal file
View File

@ -0,0 +1,50 @@
0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/nodejs',
1 verbose cli '/usr/bin/npm',
1 verbose cli 'start',
1 verbose cli '--',
1 verbose cli '-c',
1 verbose cli 'config.json' ]
2 info using npm@3.10.10
3 info using node@v6.11.2
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle dispatcher@1.0.0~prestart: dispatcher@1.0.0
6 silly lifecycle dispatcher@1.0.0~prestart: no script for prestart, continuing
7 info lifecycle dispatcher@1.0.0~start: dispatcher@1.0.0
8 verbose lifecycle dispatcher@1.0.0~start: unsafe-perm in lifecycle true
9 verbose lifecycle dispatcher@1.0.0~start: PATH: /usr/lib/node_modules/npm/bin/node-gyp-bin:/home/wn/workspace-node/Dispatcher/node_modules/.bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/wn/.local/bin:/home/wn/bin:/opt/jdk/bin:/opt/apache-maven/bin:/home/wn/mos/bin
10 verbose lifecycle dispatcher@1.0.0~start: CWD: /home/wn/workspace-node/Dispatcher
11 silly lifecycle dispatcher@1.0.0~start: Args: [ '-c', 'node dist/main.js "-c" "config.json"' ]
12 silly lifecycle dispatcher@1.0.0~start: Returned: code: 1 signal: null
13 info lifecycle dispatcher@1.0.0~start: Failed to exec start script
14 verbose stack Error: dispatcher@1.0.0 start: `node dist/main.js "-c" "config.json"`
14 verbose stack Exit status 1
14 verbose stack at EventEmitter.<anonymous> (/usr/lib/node_modules/npm/lib/utils/lifecycle.js:255:16)
14 verbose stack at emitTwo (events.js:106:13)
14 verbose stack at EventEmitter.emit (events.js:191:7)
14 verbose stack at ChildProcess.<anonymous> (/usr/lib/node_modules/npm/lib/utils/spawn.js:40:14)
14 verbose stack at emitTwo (events.js:106:13)
14 verbose stack at ChildProcess.emit (events.js:191:7)
14 verbose stack at maybeClose (internal/child_process.js:891:16)
14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
15 verbose pkgid dispatcher@1.0.0
16 verbose cwd /home/wn/workspace-node/Dispatcher
17 error Linux 4.9.0-3-amd64
18 error argv "/usr/bin/nodejs" "/usr/bin/npm" "start" "--" "-c" "config.json"
19 error node v6.11.2
20 error npm v3.10.10
21 error code ELIFECYCLE
22 error dispatcher@1.0.0 start: `node dist/main.js "-c" "config.json"`
22 error Exit status 1
23 error Failed at the dispatcher@1.0.0 start script 'node dist/main.js "-c" "config.json"'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the dispatcher package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error node dist/main.js "-c" "config.json"
23 error You can get information on how to open an issue for this project with:
23 error npm bugs dispatcher
23 error Or if that isn't available, you can get their info via:
23 error npm owner ls dispatcher
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]

View File

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

View File

@ -1,6 +1,12 @@
import * as log from './log' import * as log from './log'
import * as utils from './utils' import * as utils from './utils'
export class EspThermError extends Error {
constructor(message : any) {
super(message)
}
}
export class EspThermData { export class EspThermData {
private temperature : number private temperature : number
private voltage : number private voltage : number
@ -61,8 +67,30 @@ export class EspThermMessage {
export function espThermToJson(message : any) : any { export function espThermToJson(message : any) : any {
let messageStr : string = "" + <string>message let messageStr : string = "" + <string>message
let parts : string[] = messageStr.split(' ') let parts : string[] = messageStr.split(' ')
let espThermMessage : EspThermMessage = new EspThermMessage(parts[0],
parseFloat(parts[1]), parseFloat(parts[2]), if (parts.length != 4) {
parseInt(parts[3])) 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 return espThermMessage
} }

View File

@ -17,7 +17,7 @@ config.readConfig()
let dispatcher = new mqtt.MqttDispatcher(config.dict.brokerUrl, let dispatcher = new mqtt.MqttDispatcher(config.dict.brokerUrl,
config.dict.brokerUser, config.dict.brokerPass, config.dict.brokerCa) 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) dispatcher.register(ESP_THERM_TOPIC, 'toJson', EspThermToJson.espThermToJson)
let missingeventdetector : MissingEventDetector.MissingEventDetector = let missingeventdetector : MissingEventDetector.MissingEventDetector =