Work on EnigError and usage as experiment; This will go to many other areas of the code

This commit is contained in:
Bryan Ashby 2016-09-19 21:30:26 -06:00
parent 6a28b3ff35
commit 7da0abdc39
4 changed files with 25 additions and 9 deletions

View File

@ -2,11 +2,14 @@
'use strict'; 'use strict';
class EnigError extends Error { class EnigError extends Error {
constructor(message) { constructor(message, code, reason, reasonCode) {
super(message); super(message);
this.name = this.constructor.name; this.name = this.constructor.name;
this.message = message; this.message = message;
this.code = code;
this.reason = reason;
this.reasonCode = reasonCode;
if(typeof Error.captureStackTrace === 'function') { if(typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor); Error.captureStackTrace(this, this.constructor);
@ -16,4 +19,12 @@ class EnigError extends Error {
} }
} }
exports.EnigError = EnigError; class EnigMenuError extends EnigError { }
exports.EnigError = EnigError;
exports.EnigMenuError = EnigMenuError;
exports.Errors = {
General : (reason, reasonCode) => new EnigError('An error occurred', -33000, reason, reasonCode),
MenuStack : (reason, reasonCode) => new EnigMenuError('Menu stack error', -33001, reason, reasonCode),
};

View File

@ -3,6 +3,7 @@
// ENiGMA½ // ENiGMA½
const loadMenu = require('./menu_util.js').loadMenu; const loadMenu = require('./menu_util.js').loadMenu;
const Errors = require('./enig_error.js').Errors;
// deps // deps
const _ = require('lodash'); const _ = require('lodash');
@ -57,16 +58,16 @@ module.exports = class MenuStack {
if(_.isArray(menuConfig.next)) { if(_.isArray(menuConfig.next)) {
nextMenu = this.client.acs.getConditionalValue(menuConfig.next, 'next'); nextMenu = this.client.acs.getConditionalValue(menuConfig.next, 'next');
if(!nextMenu) { if(!nextMenu) {
return cb(new Error('No matching condition for \'next\'!')); return cb(Errors.MenuStack('No matching condition for "next"', 'NOCONDMATCH'));
} }
} else if(_.isString(menuConfig.next)) { } else if(_.isString(menuConfig.next)) {
nextMenu = menuConfig.next; nextMenu = menuConfig.next;
} else { } else {
return cb(new Error('Invalid or missing \'next\' member in menu config!')); return cb(Errors.MenuStack('Invalid or missing "next" member in menu config', 'BADNEXT'));
} }
if(nextMenu === currentModuleInfo.name) { if(nextMenu === currentModuleInfo.name) {
return cb(new Error('Menu config \'next\' specifies current menu!')); return cb(Errors.MenuStack('Menu config "next" specifies current menu', 'ALREADYTHERE'));
} }
this.goto(nextMenu, { }, cb); this.goto(nextMenu, { }, cb);
@ -90,7 +91,7 @@ module.exports = class MenuStack {
return this.goto(previousModuleInfo.name, opts, cb); return this.goto(previousModuleInfo.name, opts, cb);
} }
return cb(new Error('No previous menu available!')); return cb(Errors.MenuStack('No previous menu available', 'NOPREV'));
} }
goto(name, options, cb) { goto(name, options, cb) {
@ -104,7 +105,7 @@ module.exports = class MenuStack {
if(currentModuleInfo && name === currentModuleInfo.name) { if(currentModuleInfo && name === currentModuleInfo.name) {
if(cb) { if(cb) {
cb(new Error('Already at supplied menu!')); cb(Errors.MenuStack('Already at supplied menu', 'ALREADYTHERE'));
} }
return; return;
} }

View File

@ -139,7 +139,7 @@ Message.createMessageUUID = function(areaTag, modTimestamp, subject, body) {
body = iconvEncode(body.replace(/\r\n|[\n\v\f\r\x85\u2028\u2029]/g, '').trim(), 'CP437'); body = iconvEncode(body.replace(/\r\n|[\n\v\f\r\x85\u2028\u2029]/g, '').trim(), 'CP437');
return uuid.unparse(createNamedUUID(ENIGMA_MESSAGE_UUID_NAMESPACE, Buffer.concat( [ areaTag, modTimestamp, subject, body ] ))); return uuid.unparse(createNamedUUID(ENIGMA_MESSAGE_UUID_NAMESPACE, Buffer.concat( [ areaTag, modTimestamp, subject, body ] )));
} };
Message.getMessageIdByUuid = function(uuid, cb) { Message.getMessageIdByUuid = function(uuid, cb) {
msgDb.get( msgDb.get(

View File

@ -49,7 +49,11 @@ function ViewController(options) {
menuUtil.handleAction(self.client, formData, actionBlock, (err) => { menuUtil.handleAction(self.client, formData, actionBlock, (err) => {
if(err) { if(err) {
// :TODO: What can we really do here? // :TODO: What can we really do here?
self.client.log.warn( { err : err }, 'Error during handleAction()'); if('ALREADYTHERE' === err.reasonCode) {
self.client.log.trace( err.reason );
} else {
self.client.log.warn( { err : err }, 'Error during handleAction()');
}
} }
self.waitActionCompletion = false; self.waitActionCompletion = false;