Repository: ViacomInc/data-point
Pull request #4 (2017-10-01 16:47:58)

fix(entity-hash): fixes unclear error message
logs value, type and documentation url for reference

#3


Patch:
diff --git a/lib/entity-types/entity-hash/reducer.js b/lib/entity-types/entity-hash/reducer.js
index d548bdb..6a28957 100644
--- a/lib/entity-types/entity-hash/reducer.js
+++ b/lib/entity-types/entity-hash/reducer.js
@@ -95,11 +95,11 @@ function validateAsObject (acc) {
   return !_.isPlainObject(acc.value)
     ? Promise.reject(
         new Error(
-          `Entity "${entity.id}" received a context.value = ${(acc.value + ''
-          ).substr(
-            0,
-            15
-          )} that does not resolve to a plain object, this entity can only process Objects.`
+          `"${entity.id}" received acc.value = ${JSON.stringify(
+            acc.value
+          ).substr(0, 15)} of type ${utils.typeOf(
+            acc.value
+          )} this entity only process plain Objects. More info https://github.com/ViacomInc/data-point#hash-entity`
         )
       )
     : acc
diff --git a/lib/utils/index.js b/lib/utils/index.js
index 1f7ae7e..e5a623d 100644
--- a/lib/utils/index.js
+++ b/lib/utils/index.js
@@ -25,3 +25,18 @@ function getUID () {
   return uid
 }
 module.exports.getUID = getUID
+
+/**
+ * source: https://stackoverflow.com/a/28475765
+ * This method has not been tested for performance
+ * and could be flawed its only purpose is for error messages
+ * @param {*} value
+ */
+function typeOf (value) {
+  return {}.toString
+    .call(value)
+    .split(' ')[1]
+    .slice(0, -1)
+    .toLowerCase()
+}
+module.exports.typeOf = typeOf


Test patch:
diff --git a/lib/entity-types/entity-hash/reducer.test.js b/lib/entity-types/entity-hash/reducer.test.js
index 6b19b1a..2b0a7d7 100644
--- a/lib/entity-types/entity-hash/reducer.test.js
+++ b/lib/entity-types/entity-hash/reducer.test.js
@@ -38,6 +38,8 @@ describe('entity.hash.resolve', () => {
       })
       .then(result => {
         expect(result).toBeInstanceOf(Error)
+        expect(result.message).toContain('[1,2,3] of type array')
+        expect(result.message).toContain('More info https://')
       })
   })
 
diff --git a/lib/utils/utils.test.js b/lib/utils/utils.test.js
index a585a16..e6654b7 100644
--- a/lib/utils/utils.test.js
+++ b/lib/utils/utils.test.js
@@ -39,3 +39,28 @@ describe('utils.getUID', () => {
     expect(r).toBeGreaterThan(0)
   })
 })
+
+describe('typeOf', () => {
+  test('It should get undefined', () => {
+    expect(utils.typeOf()).toEqual('undefined')
+  })
+  test('It should get null', () => {
+    expect(utils.typeOf(null)).toEqual('null')
+  })
+  test('It should get string', () => {
+    expect(utils.typeOf('s')).toEqual('string')
+  })
+  test('It should get boolean', () => {
+    expect(utils.typeOf(false)).toEqual('boolean')
+    expect(utils.typeOf(true)).toEqual('boolean')
+  })
+  test('It should get array', () => {
+    expect(utils.typeOf([])).toEqual('array')
+  })
+  test('It should get object', () => {
+    expect(utils.typeOf({})).toEqual('object')
+  })
+  test('It should get function', () => {
+    expect(utils.typeOf(() => 1)).toEqual('function')
+  })
+})