mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
merge #45 from format-location-err-msg
formatLocation: more helpful error messages
This commit is contained in:
commit
c5aab72e71
2 changed files with 7 additions and 6 deletions
|
@ -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
|
||||
|
|
6
index.js
6
index.js
|
@ -58,8 +58,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.')
|
||||
|
@ -96,7 +96,7 @@ const createClient = (profile, request = _request) => {
|
|||
tickets: false, // return tickets?
|
||||
polylines: false // return leg shapes?
|
||||
}, 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 = new Date(opt.when || Date.now())
|
||||
if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid')
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue