1
0
Fork 0

improved logging and added bad input handling

master
Henry Jameson vor 6 Jahren
Ursprung c8b7ee4732
Commit 45f9bc3717
  1. 2
      lib/CustomerFinder.js
  2. 13
      lib/CustomerListFromURL.js
  3. 24
      lib/CustomerListFromURL.test.js

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

@ -24,7 +24,18 @@ class CustomerListFromURL {
.split('\n') // splitting by newlines
.map(_ => _.trim()) // trimming strings
.filter(_ => _) // filter out falsy items (undefineds, empty strings)
.map(_ => JSON.parse(_))
.map(_ => {
try {
return JSON.parse(_)
} catch(e) {
console.error('----')
console.error('[WARN] Failed parsing customer line, ignoring...')
console.error('Line was: <BOL>' + _ + '<EOL>')
console.error(e.message)
return undefined
}
})
.filter(_ => _) // filter out faulty items
)
}
}

@ -2,7 +2,7 @@ require('chai').should()
describe('CustomerListFromURL', function () {
let customerListFromURL
beforeEach(function () {
it('getList should correctly parse the input', async function () {
customerListFromURL = new (require('./CustomerListFromURL.js'))('')
// We mock the actual fetching of data.
customerListFromURL._fetch = () => Promise.resolve(`
@ -11,9 +11,7 @@ describe('CustomerListFromURL', function () {
{"latitude": "51.8856167", "user_id": 2, "name": "Ian McArdle", "longitude": "-10.4240951"}
{"latitude": "52.3191841", "user_id": 3, "name": "Jack Enright", "longitude": "-8.5072391"}
`)
})
it('getList should correctly parse the input', async function () {
const result = await customerListFromURL.getList()
result.should.be.an('array')
result.should.be.have.lengthOf(4)
@ -24,4 +22,24 @@ describe('CustomerListFromURL', function () {
{latitude: '52.3191841', user_id: 3, name: 'Jack Enright', longitude: '-8.5072391'}
])
})
it('getList should ignore bad items', async function () {
customerListFromURL = new (require('./CustomerListFromURL.js'))('')
// We mock the actual fetching of data.
customerListFromURL._fetch = () => Promise.resolve(`
{"latitude": "52.986375", "user_id": 12, "name": "Christina McArdle", "longitude": "-6.043701"}
"latitude": "51.92893", "user_id": 1, "name": "Alice Cahill", "longitude": "-10.27699"}
{"latitude": "51.8856167", "user_id": 2, "name": "Ian McArdle", "longitude": "-10.4240951}
undefined
{"latitude": "52.3191841", "user_id": 3, "name": "Jack Enright", "longitude": "-8.5072391"}
`)
const result = await customerListFromURL.getList()
result.should.be.an('array')
result.should.be.have.lengthOf(2)
result.should.have.deep.members([
{latitude: '52.986375', user_id: 12, name: 'Christina McArdle', longitude: '-6.043701'},
{latitude: '52.3191841', user_id: 3, name: 'Jack Enright', longitude: '-8.5072391'}
])
})
})

Laden…
Abbrechen
Speichern