Comparing version 1.1.1 to 1.1.2
@@ -166,5 +166,17 @@ var EventEmitter = require('events').EventEmitter; | ||
// Set defaults | ||
this.config.suppressErrors = this.config.suppressErrors || false; | ||
this.config.ifMissingKeysOnStartUp = this.config.ifMissingKeysOnStartUp || 'exit'; | ||
this.config.ifMissingKeysOnUpdate = this.config.ifMissingKeysOnUpdate || 'exit'; | ||
this.handleError = function (errObj) { | ||
if (!this.config.suppressErrors) { | ||
console.error(errObj.message); | ||
} | ||
this.emit('error', errObj); | ||
// Exit if the error was fatal | ||
if (errObj.level === 'FATAL') { | ||
process.exit(1); | ||
} | ||
}; | ||
// Start the watcher | ||
@@ -199,6 +211,8 @@ this.watchStart(); | ||
if (res.statusCode !== 200) { | ||
console.error( | ||
'Consul error: HTTP/' + res.statusCode + ' ' + res.statusMessage + '. ' + | ||
self.handleError({ | ||
code: 'NON_HTTP_200', | ||
level: 'WARN', | ||
message: 'Consul error: HTTP/' + res.statusCode + ' ' + res.statusMessage + '. ' + | ||
'Possible bad Token, missing or unauthorized prefix: ' + self.config.consulPrefix | ||
); | ||
}); | ||
return; | ||
@@ -228,12 +242,23 @@ } | ||
case 'exit': | ||
console.error('Exiting, Consulea found keys missing: ' + missingKeyList); | ||
process.exit(1); | ||
self.handleError({ | ||
code: 'MISSING_KEY_EXIT', | ||
level: 'FATAL', | ||
message: 'Exiting, Consulea found keys missing: ' + missingKeyList | ||
}); | ||
break; | ||
case 'warn': | ||
console.warn('Warning, Consulea found keys missing: ' + missingKeyList); | ||
self.handleError({ | ||
code: 'MISSING_KEY_WARN', | ||
level: 'WARN', | ||
message: 'Warning, Consulea found keys missing: ' + missingKeyList | ||
}); | ||
break; | ||
case 'skip': | ||
console.warn('Warning, Consulea found keys missing, skipping event call: ' + missingKeyList); | ||
self.handleError({ | ||
code: 'MISSING_KEY_SKIP', | ||
level: 'WARN', | ||
message: 'Warning, Consulea found keys missing, skipping event call: ' + missingKeyList | ||
}); | ||
return; | ||
@@ -245,7 +270,14 @@ | ||
if (self.lastGoodKvData[missingKey] !== undefined) { | ||
console.error('Warning, Consulea found key missing, using old value: ' + missingKey); | ||
self.handleError({ | ||
code: 'MISSING_KEY_USED_PREV_VAL', | ||
level: 'WARN', | ||
message: 'Warning, Consulea found key missing, using old value: ' + missingKey | ||
}); | ||
kvData[missingKey] = self.lastGoodKvData[missingKey]; | ||
} else { | ||
console.error('Exiting, Consulea found key missing with no previous value: ' + missingKey); | ||
process.exit(1); | ||
self.handleError({ | ||
code: 'MISSING_KEY_NO_PREV_VAL', | ||
level: 'FATAL', | ||
message: 'Exiting, Consulea found key missing with no previous value: ' + missingKey | ||
}); | ||
} | ||
@@ -256,4 +288,7 @@ } | ||
default: | ||
console.error('Exiting, Consulea has unknown config: ' + whichRule + '=' + ruleValue); | ||
process.exit(1); | ||
self.handleError({ | ||
code: 'UNKNOWN_CONFIG', | ||
level: 'FATAL', | ||
message: 'Exiting, Consulea has unknown config: ' + whichRule + '=' + ruleValue | ||
}); | ||
} | ||
@@ -280,4 +315,7 @@ } else { | ||
this._watcher.on('error', function (err) { | ||
self.emit('error', err); | ||
console.error('Consul error:', err); | ||
self.handleError({ | ||
code: 'CLIENT_ERR', | ||
level: 'WARN', | ||
message: 'Consul error:' + err | ||
}); | ||
}); | ||
@@ -284,0 +322,0 @@ }; |
{ | ||
"name": "consulea", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Load Consul keys, environment vars, and command line arguments in a predictable, standardized way.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -47,2 +47,7 @@ # Consulea | ||
}); | ||
// Handle errors and warnings as you see fit | ||
consulea.on('error', function (err) { | ||
console.error('Consulea error:', err); | ||
}); | ||
``` | ||
@@ -82,2 +87,3 @@ | ||
| ifMissingKeysOnUpdate | No | Set behavior when required keys are missing on any update after start up (default: exit) | | ||
| suppressErrors | No | Set `true` to prevent Consulea from emitting errors. The emitted error event will still occur | | ||
@@ -84,0 +90,0 @@ ifMissingKeysOnStartUp and ifMissingKeysOnUpdate can be one of the following: |
17578
344
103