Skip to content

Commit 4b8e671

Browse files
committed
disableError()
1 parent 70146a8 commit 4b8e671

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,30 @@ if (process.env.NODE_ENV === 'production') {
122122
}
123123
```
124124

125+
### env.disableErrors()
126+
127+
`getenv` won't throw any error. If a fallback value is provided, that will be returned, else `undefined` is returned.
128+
129+
```javascript
130+
getenv.disableErrors();
131+
console.log(getenv("RANDOM"));
132+
// undefined
133+
```
134+
135+
### env.enableErrors()
136+
137+
Revert the effect of `disableErrors()`.
138+
139+
```javascript
140+
getenv.disableErrors();
141+
console.log(getenv("RANDOM"));
142+
// undefined
143+
144+
getenv.enableErrors();
145+
console.log(getenv("RANDOM"));
146+
// Error: GetEnv.Nonexistent: RANDOM does not exist and no fallback value provided.
147+
```
148+
125149
## Changelog
126150

127151
### v0.6.0
@@ -149,6 +173,7 @@ if (process.env.NODE_ENV === 'production') {
149173
- Jonas Dohse <[email protected]>
150174
- Jan Lehnardt (@janl): `getenv.multi()` support.
151175
- Tim Ruffles <[email protected]>: `disableFallbacks()`, `url()`
176+
- Ashwani Agarwal <[email protected]>: `disableErrors()`, `enableErrors()`
152177

153178
## License
154179

lib/getenv.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ var util = require("util");
22
var url = require("url");
33

44
var fallbacksDisabled = false;
5+
var throwError = true;
56

67
function _value(varName, fallback) {
78
var value = process.env[varName];
89
if (value === undefined) {
10+
if (fallback === undefined && !throwError) {
11+
return value;
12+
}
913
if (fallback === undefined) {
1014
throw new Error('GetEnv.Nonexistent: ' + varName + ' does not exist ' +
1115
'and no fallback value provided.');
@@ -127,4 +131,12 @@ getenv.enableFallbacks = function() {
127131
fallbacksDisabled = false;
128132
};
129133

134+
getenv.disableErrors = function() {
135+
throwError = false;
136+
};
137+
138+
getenv.enableErrors = function() {
139+
throwError = true;
140+
};
141+
130142
module.exports = getenv;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"Moritz von Hase",
77
"Jonas Dohse <[email protected]>",
88
"Jan Lehnardt",
9-
"Tim Ruffles <[email protected]>"
9+
"Tim Ruffles <[email protected]>",
10+
"Ashwani Agarwal <[email protected]>"
1011
],
1112
"version": "0.6.0",
1213
"homepage": "https://github.com/ctavan/node-getenv",

test/disableErrors.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var assert = require('assert');
2+
3+
var getenv = require('../lib/getenv');
4+
5+
var tests = {};
6+
7+
tests['getenv.disableErrors() should disable any errors'] = function() {
8+
getenv.disableErrors();
9+
assert.strictEqual(getenv.string("url", "http://localhost"), 'http://localhost');
10+
assert(getenv.string("url"), undefined);
11+
};
12+
13+
tests['getenv.enableErrors() should enable errors'] = function () {
14+
getenv.enableErrors();
15+
assert.throws(function() {
16+
getenv.string("url");
17+
});
18+
assert.strictEqual(getenv.string("url", "http://localhost"), 'http://localhost');
19+
}
20+
21+
Object.keys(tests).forEach(function(key) {
22+
console.log('Test: %s', key);
23+
tests[key]();
24+
});

test/fallbacks.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ tests['getenv.disableFallbacks() makes relying on fallbacks an error'] = functio
1313
getenv.enableFallbacks();
1414
};
1515

16-
1716
Object.keys(tests).forEach(function(key) {
1817
console.log('Test: %s', key);
1918
tests[key]();

0 commit comments

Comments
 (0)