Error -> TypeError 💥

This commit is contained in:
Jannis R 2019-02-13 17:55:57 +01:00
parent fbde6a171b
commit 16461733e4
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
5 changed files with 33 additions and 33 deletions

View file

@ -5,7 +5,7 @@ const formatCoord = require('./coord')
const formatAddress = (a) => { const formatAddress = (a) => {
if (a.type !== 'location' || !a.latitude || !a.longitude || !a.address) { if (a.type !== 'location' || !a.latitude || !a.longitude || !a.address) {
throw new Error('invalid address') throw new TypeError('invalid address')
} }
const data = { const data = {

View file

@ -8,10 +8,10 @@ const formatLocation = (profile, l, name = 'location') => {
} }
if (l.poi) return profile.formatPoi(l) if (l.poi) return profile.formatPoi(l)
if ('string' === typeof l.address) return profile.formatAddress(l) if ('string' === typeof l.address) return profile.formatAddress(l)
if (!l.type) throw new Error(`missing ${name}.type`) if (!l.type) throw new TypeError(`missing ${name}.type`)
throw new Error(`invalid ${name}.type: ${l.type}`) throw new TypeError(`invalid ${name}.type: ${l.type}`)
} }
throw new Error(name + ': valid station, address or poi required.') throw new TypeError(name + ': valid station, address or poi required.')
} }
module.exports = formatLocation module.exports = formatLocation

View file

@ -5,7 +5,7 @@ const formatCoord = require('./coord')
const formatPoi = (p) => { const formatPoi = (p) => {
if (p.type !== 'location' || !p.latitude || !p.longitude || !p.id || !p.name) { if (p.type !== 'location' || !p.latitude || !p.longitude || !p.id || !p.name) {
throw new Error('invalid POI') throw new TypeError('invalid POI')
} }
return { return {

View file

@ -13,13 +13,13 @@ const createFormatProductsFilter = (profile) => {
} }
const formatProductsFilter = (filter) => { const formatProductsFilter = (filter) => {
if (!isObj(filter)) throw new Error('products filter must be an object') if (!isObj(filter)) throw new TypeError('products filter must be an object')
filter = Object.assign({}, defaultProducts, filter) filter = Object.assign({}, defaultProducts, filter)
let res = 0, products = 0 let res = 0, products = 0
for (let product in filter) { for (let product in filter) {
if (!hasProp(filter, product) || filter[product] !== true) continue if (!hasProp(filter, product) || filter[product] !== true) continue
if (!byProduct[product]) throw new Error('unknown product ' + product) if (!byProduct[product]) throw new TypeError('unknown product ' + product)
products++ products++
for (let bitmask of byProduct[product].bitmasks) res += bitmask for (let bitmask of byProduct[product].bitmasks) res += bitmask
} }

View file

@ -16,13 +16,13 @@ const isNonEmptyString = str => 'string' === typeof str && str.length > 0
const validateLocation = (loc, name = 'location') => { const validateLocation = (loc, name = 'location') => {
if (!isObj(loc)) { if (!isObj(loc)) {
throw new Error(name + ' must be an object.') throw new TypeError(name + ' must be an object.')
} else if (loc.type !== 'location') { } else if (loc.type !== 'location') {
throw new Error('invalid location object.') throw new TypeError('invalid location object.')
} else if ('number' !== typeof loc.latitude) { } else if ('number' !== typeof loc.latitude) {
throw new Error(name + '.latitude must be a number.') throw new TypeError(name + '.latitude must be a number.')
} else if ('number' !== typeof loc.longitude) { } else if ('number' !== typeof loc.longitude) {
throw new Error(name + '.longitude must be a number.') throw new TypeError(name + '.longitude must be a number.')
} }
} }
@ -37,16 +37,16 @@ const createClient = (profile, userAgent, request = _request) => {
validateProfile(profile) validateProfile(profile)
if ('string' !== typeof userAgent) { if ('string' !== typeof userAgent) {
throw new Error('userAgent must be a string'); throw new TypeError('userAgent must be a string');
} }
const _stationBoard = (station, type, parser, opt = {}) => { const _stationBoard = (station, type, parser, opt = {}) => {
if (isObj(station)) station = profile.formatStation(station.id) if (isObj(station)) station = profile.formatStation(station.id)
else if ('string' === typeof station) station = profile.formatStation(station) else if ('string' === typeof station) station = profile.formatStation(station)
else throw new Error('station must be an object or a string.') else throw new TypeError('station must be an object or a string.')
if ('string' !== typeof type || !type) { if ('string' !== typeof type || !type) {
throw new Error('type must be a non-empty string.') throw new TypeError('type must be a non-empty string.')
} }
if (!profile.departuresGetPasslist && ('stopovers' in opt)) { if (!profile.departuresGetPasslist && ('stopovers' in opt)) {
@ -111,27 +111,27 @@ const createClient = (profile, userAgent, request = _request) => {
to = profile.formatLocation(profile, to, 'to') to = profile.formatLocation(profile, to, 'to')
if (('earlierThan' in opt) && ('laterThan' in opt)) { if (('earlierThan' in opt) && ('laterThan' in opt)) {
throw new Error('opt.earlierThan and opt.laterThan are mutually exclusive.') throw new TypeError('opt.earlierThan and opt.laterThan are mutually exclusive.')
} }
if (('departure' in opt) && ('arrival' in opt)) { if (('departure' in opt) && ('arrival' in opt)) {
throw new Error('opt.departure and opt.arrival are mutually exclusive.') throw new TypeError('opt.departure and opt.arrival are mutually exclusive.')
} }
let journeysRef = null let journeysRef = null
if ('earlierThan' in opt) { if ('earlierThan' in opt) {
if (!isNonEmptyString(opt.earlierThan)) { if (!isNonEmptyString(opt.earlierThan)) {
throw new Error('opt.earlierThan must be a non-empty string.') throw new TypeError('opt.earlierThan must be a non-empty string.')
} }
if (('departure' in opt) || ('arrival' in opt)) { if (('departure' in opt) || ('arrival' in opt)) {
throw new Error('opt.earlierThan and opt.departure/opt.arrival are mutually exclusive.') throw new TypeError('opt.earlierThan and opt.departure/opt.arrival are mutually exclusive.')
} }
journeysRef = opt.earlierThan journeysRef = opt.earlierThan
} }
if ('laterThan' in opt) { if ('laterThan' in opt) {
if (!isNonEmptyString(opt.laterThan)) { if (!isNonEmptyString(opt.laterThan)) {
throw new Error('opt.laterThan must be a non-empty string.') throw new TypeError('opt.laterThan must be a non-empty string.')
} }
if (('departure' in opt) || ('arrival' in opt)) { if (('departure' in opt) || ('arrival' in opt)) {
throw new Error('opt.laterThan and opt.departure/opt.arrival are mutually exclusive.') throw new TypeError('opt.laterThan and opt.departure/opt.arrival are mutually exclusive.')
} }
journeysRef = opt.laterThan journeysRef = opt.laterThan
} }
@ -160,10 +160,10 @@ const createClient = (profile, userAgent, request = _request) => {
let when = new Date(), outFrwd = true let when = new Date(), outFrwd = true
if (opt.departure !== undefined && opt.departure !== null) { if (opt.departure !== undefined && opt.departure !== null) {
when = new Date(opt.departure) when = new Date(opt.departure)
if (Number.isNaN(+when)) throw new Error('opt.departure is invalid') if (Number.isNaN(+when)) throw new TypeError('opt.departure is invalid')
} else if (opt.arrival !== undefined && opt.arrival !== null) { } else if (opt.arrival !== undefined && opt.arrival !== null) {
when = new Date(opt.arrival) when = new Date(opt.arrival)
if (Number.isNaN(+when)) throw new Error('opt.arrival is invalid') if (Number.isNaN(+when)) throw new TypeError('opt.arrival is invalid')
outFrwd = false outFrwd = false
} }
@ -254,7 +254,7 @@ const createClient = (profile, userAgent, request = _request) => {
const refreshJourney = (refreshToken, opt = {}) => { const refreshJourney = (refreshToken, opt = {}) => {
if ('string' !== typeof refreshToken || !refreshToken) { if ('string' !== typeof refreshToken || !refreshToken) {
new Error('refreshToken must be a non-empty string.') new TypeError('refreshToken must be a non-empty string.')
} }
opt = Object.assign({ opt = Object.assign({
@ -292,7 +292,7 @@ const createClient = (profile, userAgent, request = _request) => {
const locations = (query, opt = {}) => { const locations = (query, opt = {}) => {
if (!isNonEmptyString(query)) { if (!isNonEmptyString(query)) {
throw new Error('query must be a non-empty string.') throw new TypeError('query must be a non-empty string.')
} }
opt = Object.assign({ opt = Object.assign({
fuzzy: true, // find only exact matches? fuzzy: true, // find only exact matches?
@ -326,7 +326,7 @@ const createClient = (profile, userAgent, request = _request) => {
const stop = (stop, opt = {}) => { const stop = (stop, opt = {}) => {
if ('object' === typeof stop) stop = profile.formatStation(stop.id) if ('object' === typeof stop) stop = profile.formatStation(stop.id)
else if ('string' === typeof stop) stop = profile.formatStation(stop) else if ('string' === typeof stop) stop = profile.formatStation(stop)
else throw new Error('stop must be an object or a string.') else throw new TypeError('stop must be an object or a string.')
opt = Object.assign({ opt = Object.assign({
linesOfStops: false // parse & expose lines at the stop/station? linesOfStops: false // parse & expose lines at the stop/station?
@ -383,10 +383,10 @@ const createClient = (profile, userAgent, request = _request) => {
const trip = (id, lineName, opt = {}) => { const trip = (id, lineName, opt = {}) => {
if (!isNonEmptyString(id)) { if (!isNonEmptyString(id)) {
throw new Error('id must be a non-empty string.') throw new TypeError('id must be a non-empty string.')
} }
if (!isNonEmptyString(lineName)) { if (!isNonEmptyString(lineName)) {
throw new Error('lineName must be a non-empty string.') throw new TypeError('lineName must be a non-empty string.')
} }
opt = Object.assign({ opt = Object.assign({
stopovers: true, // return stations on the way? stopovers: true, // return stations on the way?
@ -430,10 +430,10 @@ const createClient = (profile, userAgent, request = _request) => {
} }
const radar = ({north, west, south, east}, opt) => { const radar = ({north, west, south, east}, opt) => {
if ('number' !== typeof north) throw new Error('north must be a number.') if ('number' !== typeof north) throw new TypeError('north must be a number.')
if ('number' !== typeof west) throw new Error('west must be a number.') if ('number' !== typeof west) throw new TypeError('west must be a number.')
if ('number' !== typeof south) throw new Error('south must be a number.') if ('number' !== typeof south) throw new TypeError('south must be a number.')
if ('number' !== typeof east) throw new Error('east must be a number.') if ('number' !== typeof east) throw new TypeError('east must be a number.')
if (north <= south) throw new Error('north must be larger than south.') if (north <= south) throw new Error('north must be larger than south.')
if (east <= west) throw new Error('east must be larger than west.') if (east <= west) throw new Error('east must be larger than west.')
@ -446,7 +446,7 @@ const createClient = (profile, userAgent, request = _request) => {
polylines: true // return a track shape for each vehicle? polylines: true // return a track shape for each vehicle?
}, opt || {}) }, opt || {})
opt.when = new Date(opt.when || Date.now()) opt.when = new Date(opt.when || Date.now())
if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid') if (Number.isNaN(+opt.when)) throw new TypeError('opt.when is invalid')
const durationPerStep = opt.duration / Math.max(opt.frames, 1) * 1000 const durationPerStep = opt.duration / Math.max(opt.frames, 1) * 1000
return request(profile, userAgent, opt, { return request(profile, userAgent, opt, {
@ -490,7 +490,7 @@ const createClient = (profile, userAgent, request = _request) => {
maxDuration: 20, // maximum travel duration in minutes, pass `null` for infinite maxDuration: 20, // maximum travel duration in minutes, pass `null` for infinite
products: {} products: {}
}, opt) }, opt)
if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid') if (Number.isNaN(+opt.when)) throw new TypeError('opt.when is invalid')
const refetch = () => { const refetch = () => {
return request(profile, userAgent, opt, { return request(profile, userAgent, opt, {