Skip to content

Commit

Permalink
[#74] Remove raiseError accessor param. (#75)
Browse files Browse the repository at this point in the history
* [#74] Remove `raiseError` accessor param.

If an accessor cannot process an input value, it should just throw an exception instead.

There is a trade-off to this approach, in that the stacktrace for the accessor-thrown error is not preserved (replaced by `_raiseError()`), but accessors tend to be fairly simple in implementation, so it shouldn' be too difficult to locate the source of an error.

* [#74] Refactored now-unnecessarily-complex function 🍃
  • Loading branch information
todofixthis authored and evanshortiss committed Sep 3, 2019
1 parent 7c0eba5 commit be95f31
Show file tree
Hide file tree
Showing 21 changed files with 59 additions and 61 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,9 @@ and populate it with code following this structure:
```js
/**
* Validate that the environment value is an integer and equals zero.
* @param {Function} raiseError use this to raise a cleanly formatted error
* @param {String} environmentValue this is the string from process.env
*/
module.exports = function numberZero (raiseError, environmentValue) {
module.exports = function numberZero (environmentValue) {

// Your custom code should go here...below code is an example

Expand All @@ -333,7 +332,7 @@ module.exports = function numberZero (raiseError, environmentValue) {
if (val === 0) {
return ret;
} else {
raiseError('should be zero')
throw new Error('should be zero')
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions lib/accessors/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

const asString = require('./string')

module.exports = function asArray (raiseError, value, delimiter) {
module.exports = function asArray (value, delimiter) {
delimiter = delimiter || ','

if (!value.length) {
return []
} else {
return asString(raiseError, value).split(delimiter).filter(Boolean)
return asString(value).split(delimiter).filter(Boolean)
}
}
4 changes: 2 additions & 2 deletions lib/accessors/bool-strict.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

module.exports = function asBoolStrict (raiseError, value) {
module.exports = function asBoolStrict (value) {
const val = value.toLowerCase()

if ((val !== 'false') && (val !== 'true')) {
raiseError('should be either "true", "false", "TRUE", or "FALSE"')
throw new Error('should be either "true", "false", "TRUE", or "FALSE"')
}

return val !== 'false'
Expand Down
4 changes: 2 additions & 2 deletions lib/accessors/bool.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

module.exports = function asBool (raiseError, value) {
module.exports = function asBool (value) {
const val = value.toLowerCase()

const allowedValues = [
Expand All @@ -11,7 +11,7 @@ module.exports = function asBool (raiseError, value) {
]

if (allowedValues.indexOf(val) === -1) {
raiseError('should be either "true", "false", "TRUE", "FALSE", 1, or 0')
throw new Error('should be either "true", "false", "TRUE", "FALSE", 1, or 0')
}

return !(((val === '0') || (val === 'false')))
Expand Down
6 changes: 3 additions & 3 deletions lib/accessors/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const asString = require('./string')

module.exports = function asEnum (raiseError, value, validValues) {
const valueString = asString(raiseError, value)
module.exports = function asEnum (value, validValues) {
const valueString = asString(value)

if (validValues.indexOf(valueString) < 0) {
raiseError(`should be a one of [${validValues.join(', ')}]`)
throw new Error(`should be one of [${validValues.join(', ')}]`)
}

return valueString
Expand Down
6 changes: 3 additions & 3 deletions lib/accessors/float-negative.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const asFloat = require('./float')

module.exports = function floatNegative (raiseError, value) {
const ret = asFloat(raiseError, value)
module.exports = function floatNegative (value) {
const ret = asFloat(value)

if (ret > 0) {
raiseError('should be a negative float')
throw new Error('should be a negative float')
}

return ret
Expand Down
6 changes: 3 additions & 3 deletions lib/accessors/float-positive.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const asFloat = require('./float')

module.exports = function floatPositive (raiseError, value) {
const ret = asFloat(raiseError, value)
module.exports = function floatPositive (value) {
const ret = asFloat(value)

if (ret < 0) {
raiseError('should be a positive float')
throw new Error('should be a positive float')
}

return ret
Expand Down
4 changes: 2 additions & 2 deletions lib/accessors/float.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

module.exports = function asFloat (raiseError, value) {
module.exports = function asFloat (value) {
const n = parseFloat(value)

if (isNaN(n)) {
raiseError('should be a valid float')
throw new Error('should be a valid float')
}

return n
Expand Down
6 changes: 3 additions & 3 deletions lib/accessors/int-negative.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const asInt = require('./int')

module.exports = function intNegative (raiseError, value) {
const ret = asInt(raiseError, value)
module.exports = function intNegative (value) {
const ret = asInt(value)

if (ret > 0) {
raiseError('should be a negative integer')
throw new Error('should be a negative integer')
}

return ret
Expand Down
6 changes: 3 additions & 3 deletions lib/accessors/int-positive.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const asInt = require('./int')

module.exports = function intPositive (raiseError, value) {
const ret = asInt(raiseError, value)
module.exports = function intPositive (value) {
const ret = asInt(value)

if (ret < 0) {
raiseError('should be a positive integer')
throw new Error('should be a positive integer')
}

return ret
Expand Down
4 changes: 2 additions & 2 deletions lib/accessors/int.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

module.exports = function asInt (raiseError, value) {
module.exports = function asInt (value) {
const n = parseInt(value, 10)

if (isNaN(n) || value.toString().indexOf('.') !== -1) {
raiseError('should be a valid integer')
throw new Error('should be a valid integer')
}

return n
Expand Down
6 changes: 3 additions & 3 deletions lib/accessors/json-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const asJson = require('./json')

module.exports = function asJsonArray (raiseError, value) {
var ret = asJson(raiseError, value)
module.exports = function asJsonArray (value) {
var ret = asJson(value)

if (!Array.isArray(ret)) {
raiseError('should be a parseable JSON Array')
throw new Error('should be a parseable JSON Array')
}

return ret
Expand Down
6 changes: 3 additions & 3 deletions lib/accessors/json-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const asJson = require('./json')

module.exports = function asJsonArray (raiseError, value) {
var ret = asJson(raiseError, value)
module.exports = function asJsonArray (value) {
var ret = asJson(value)

if (Array.isArray(ret)) {
raiseError('should be a parseable JSON Object')
throw new Error('should be a parseable JSON Object')
}

return ret
Expand Down
4 changes: 2 additions & 2 deletions lib/accessors/json.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

module.exports = function asJson (raiseError, value) {
module.exports = function asJson (value) {
try {
return JSON.parse(value)
} catch (e) {
raiseError('should be valid (parseable) JSON')
throw new Error('should be valid (parseable) JSON')
}
}
6 changes: 3 additions & 3 deletions lib/accessors/port.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const asIntPositive = require('./int-positive')

module.exports = function asPortNumber (raiseError, value) {
var ret = asIntPositive(raiseError, value)
module.exports = function asPortNumber (value) {
var ret = asIntPositive(value)

if (ret > 65535) {
raiseError('cannot assign a port number greater than 65535')
throw new Error('cannot assign a port number greater than 65535')
}

return ret
Expand Down
2 changes: 1 addition & 1 deletion lib/accessors/string.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'

module.exports = function (raiseException, value) {
module.exports = function (value) {
return value
}
6 changes: 3 additions & 3 deletions lib/accessors/url-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
const URL = require('url').URL
const asString = require('./string')

module.exports = function asUrlObject (raiseError, value) {
const ret = asString(raiseError, value)
module.exports = function asUrlObject (value) {
const ret = asString(value)

try {
return new URL(ret)
} catch (e) {
raiseError('should be a valid URL')
throw new Error('should be a valid URL')
}
}
4 changes: 2 additions & 2 deletions lib/accessors/url-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

const urlObject = require('./url-object')

module.exports = function (raiseError, value) {
return urlObject(raiseError, value).toString()
module.exports = function (value) {
return urlObject(value).toString()
}
2 changes: 1 addition & 1 deletion lib/env-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const inherits = require('util').inherits

/**
* Creates a cutom error class that can be used to identify errors generated
* Creates a custom error class that can be used to identify errors generated
* by the module
*/
function EnvVarError (message) {
Expand Down
27 changes: 13 additions & 14 deletions lib/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ module.exports = function getVariableAccessors (container, varName, defValue) {
let isBase64 = false

/**
* Generate a function to throw an error
* Throw an error with a consistent type/format.
* @param {String} value
*/
function generateRaiseError (value) {
return function _raiseError (msg) {
throw new EnvVarError(`"${varName}" ${msg}, but was "${value}"`)
}
function raiseError (value, msg) {
throw new EnvVarError(`"${varName}" ${msg}, but was "${value}"`)
}

/**
Expand All @@ -43,21 +41,22 @@ module.exports = function getVariableAccessors (container, varName, defValue) {

if (isBase64) {
if (!value.match(base64Regex)) {
generateRaiseError(value)('should be a valid base64 string if using convertFromBase64')
raiseError(value, 'should be a valid base64 string if using convertFromBase64')
}

value = Buffer.from(value, 'base64').toString()
}

const args = [
generateRaiseError(value),
value
].concat(Array.prototype.slice.call(arguments))
const args = [value].concat(Array.prototype.slice.call(arguments))

return accessor.apply(
accessor,
args
)
try {
return accessor.apply(
accessor,
args
)
} catch (error) {
raiseError(value, error.message)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('env-var', function () {
it('should throw when value is not expected', function () {
expect(() => {
expect(mod.get('ENUM').asEnum(['INVALID']))
}).to.throw('env-var: "ENUM" should be a one of [INVALID], but was "VALID"')
}).to.throw('env-var: "ENUM" should be one of [INVALID], but was "VALID"')
})
})

Expand Down

0 comments on commit be95f31

Please sign in to comment.