From 8ce89f0fe6fd7f7ad53b74d5c22571872237059f Mon Sep 17 00:00:00 2001 From: Jannis R Date: Tue, 17 Apr 2018 18:43:42 +0200 Subject: [PATCH 1/2] formatLocation: more helpful error messages --- format/location.js | 7 ++++--- index.js | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/format/location.js b/format/location.js index 121cd13f..aa7d19d1 100644 --- a/format/location.js +++ b/format/location.js @@ -1,14 +1,15 @@ 'use strict' -const formatLocation = (profile, l) => { +const formatLocation = (profile, l, name = 'location') => { if ('string' === typeof l) return profile.formatStation(l) if ('object' === typeof l && !Array.isArray(l)) { if (l.type === 'station') return profile.formatStation(l.id) if ('string' === typeof l.id) return profile.formatPoi(l) if ('string' === typeof l.address) return profile.formatAddress(l) - throw new Error('invalid location type: ' + l.type) + if (!l.type) throw new Error(`missing ${name}.type`) + throw new Error(`invalid ${name}type: ${l.type}`) } - throw new Error('valid station, address or poi required.') + throw new Error(name + ': valid station, address or poi required.') } module.exports = formatLocation diff --git a/index.js b/index.js index 7d0482b7..7ea60620 100644 --- a/index.js +++ b/index.js @@ -57,8 +57,8 @@ const createClient = (profile, request = _request) => { } const journeys = (from, to, opt = {}) => { - from = profile.formatLocation(profile, from) - to = profile.formatLocation(profile, to) + from = profile.formatLocation(profile, from, 'from') + to = profile.formatLocation(profile, to, 'to') if (('earlierThan' in opt) && ('laterThan' in opt)) { throw new Error('opt.laterThan and opt.laterThan are mutually exclusive.') @@ -94,7 +94,7 @@ const createClient = (profile, request = _request) => { bike: false, // only bike-friendly journeys tickets: false, // return tickets? }, opt) - if (opt.via) opt.via = profile.formatLocation(profile, opt.via) + if (opt.via) opt.via = profile.formatLocation(profile, opt.via, 'opt.via') opt.when = opt.when || new Date() const filters = [ From 47aea604df3d2949840b7f8fcb7750dd2bfb20a4 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Thu, 3 May 2018 10:29:43 +0200 Subject: [PATCH 2/2] fix error message :bug: --- format/location.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/format/location.js b/format/location.js index aa7d19d1..39f0a990 100644 --- a/format/location.js +++ b/format/location.js @@ -7,7 +7,7 @@ const formatLocation = (profile, l, name = 'location') => { if ('string' === typeof l.id) return profile.formatPoi(l) if ('string' === typeof l.address) return profile.formatAddress(l) if (!l.type) throw new Error(`missing ${name}.type`) - throw new Error(`invalid ${name}type: ${l.type}`) + throw new Error(`invalid ${name}.type: ${l.type}`) } throw new Error(name + ': valid station, address or poi required.') }