Browse Source

improved logging and added bad input handling

Henry Jameson 4 years ago
parent
commit
45f9bc3717
3 changed files with 34 additions and 5 deletions
  1. 1
    1
      lib/CustomerFinder.js
  2. 12
    1
      lib/CustomerListFromURL.js
  3. 21
    3
      lib/CustomerListFromURL.test.js

+ 1
- 1
lib/CustomerFinder.js View File

@@ -32,7 +32,7 @@ class CustomerFinder {
32 32
         ? this._getCustomerDistance(customer, true)
33 33
         : distance)
34 34
     // probably should fall back for all following queries depending on response
35
-      .catch(e => console.error(`Failed fetching length via ${dr.constructor.name}: ${e.message}, falling back... to simple calculation`) ||
35
+      .catch(e => console.error(`[WARN] Failed fetching length via ${dr.constructor.name}: ${e.message}, falling back... to simple calculation`) ||
36 36
              this._getCustomerDistance(customer, true))
37 37
   }
38 38
 

+ 12
- 1
lib/CustomerListFromURL.js View File

@@ -24,7 +24,18 @@ class CustomerListFromURL {
24 24
         .split('\n') // splitting by newlines
25 25
         .map(_ => _.trim()) // trimming strings
26 26
         .filter(_ => _) // filter out falsy items (undefineds, empty strings)
27
-        .map(_ => JSON.parse(_))
27
+            .map(_ => {
28
+              try {
29
+                return JSON.parse(_)
30
+              } catch(e) {
31
+                console.error('----')
32
+                console.error('[WARN] Failed parsing customer line, ignoring...')
33
+                console.error('Line was: <BOL>' + _ + '<EOL>')
34
+                console.error(e.message)
35
+                return undefined
36
+              }
37
+            })
38
+            .filter(_ => _) // filter out faulty items
28 39
       )
29 40
   }
30 41
 }

+ 21
- 3
lib/CustomerListFromURL.test.js View File

@@ -2,7 +2,7 @@ require('chai').should()
2 2
 describe('CustomerListFromURL', function () {
3 3
   let customerListFromURL
4 4
 
5
-  beforeEach(function () {
5
+  it('getList should correctly parse the input', async function () {
6 6
     customerListFromURL = new (require('./CustomerListFromURL.js'))('')
7 7
     // We mock the actual fetching of data.
8 8
     customerListFromURL._fetch = () => Promise.resolve(`
@@ -11,9 +11,7 @@ describe('CustomerListFromURL', function () {
11 11
       {"latitude": "51.8856167", "user_id": 2, "name": "Ian McArdle", "longitude": "-10.4240951"}
12 12
       {"latitude": "52.3191841", "user_id": 3, "name": "Jack Enright", "longitude": "-8.5072391"}
13 13
     `)
14
-  })
15 14
 
16
-  it('getList should correctly parse the input', async function () {
17 15
     const result = await customerListFromURL.getList()
18 16
     result.should.be.an('array')
19 17
     result.should.be.have.lengthOf(4)
@@ -24,4 +22,24 @@ describe('CustomerListFromURL', function () {
24 22
       {latitude: '52.3191841', user_id: 3, name: 'Jack Enright', longitude: '-8.5072391'}
25 23
     ])
26 24
   })
25
+
26
+  it('getList should ignore bad items', async function () {
27
+    customerListFromURL = new (require('./CustomerListFromURL.js'))('')
28
+    // We mock the actual fetching of data.
29
+    customerListFromURL._fetch = () => Promise.resolve(`
30
+      {"latitude": "52.986375", "user_id": 12, "name": "Christina McArdle", "longitude": "-6.043701"}
31
+      "latitude": "51.92893", "user_id": 1, "name": "Alice Cahill", "longitude": "-10.27699"}
32
+      {"latitude": "51.8856167", "user_id": 2, "name": "Ian McArdle", "longitude": "-10.4240951}
33
+      undefined
34
+      {"latitude": "52.3191841", "user_id": 3, "name": "Jack Enright", "longitude": "-8.5072391"}
35
+    `)
36
+
37
+    const result = await customerListFromURL.getList()
38
+    result.should.be.an('array')
39
+    result.should.be.have.lengthOf(2)
40
+    result.should.have.deep.members([
41
+      {latitude: '52.986375', user_id: 12, name: 'Christina McArdle', longitude: '-6.043701'},
42
+      {latitude: '52.3191841', user_id: 3, name: 'Jack Enright', longitude: '-8.5072391'}
43
+    ])
44
+  })
27 45
 })