From 2dfa0d755eb6edcbd433a21e813129c7586877d9 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Wed, 5 Aug 2026 14:08:05 -0400 Subject: [PATCH] fix(import): Catch warning from unsupported hierarchy entries During an import I noticed an error like the following: error TypeError: Cannot read properties of undefined (reading 'name') at /code/import/source/whosonfirst/map/hierarchies.js:26:62 at Array.forEach () at Object.mapper [as hierarchies] (/code/import/source/whosonfirst/map/hierarchies.js:24:15) at mapper (/code/import/source/whosonfirst/map/place.js:39:7) at DestroyableTransform._transform (/code/import/mapperStream.js:7:21) at Transform._read (/code/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:166:10) at Transform._write (/code/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:155:83) at doWrite (/code/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:390:139) at writeOrBuffer (/code/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:381:5) at Writable.write (/code/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:302:11) After some investigation these errors don't appar to be hurting anything, they happen when a record in a layer we don't currently support is imported. In this case it was the `postalregion` layer. We could consider supporting that layer in the future, which I think is also easy, but in the meantime we should at least silence these errors. --- import/source/whosonfirst/map/hierarchies.js | 8 +++++-- .../whosonfirst/map/hierarchies.test.js | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/import/source/whosonfirst/map/hierarchies.js b/import/source/whosonfirst/map/hierarchies.js index 1df7006..1de153c 100644 --- a/import/source/whosonfirst/map/hierarchies.js +++ b/import/source/whosonfirst/map/hierarchies.js @@ -18,12 +18,16 @@ function sortHierarchy (hierarchy) { function mapper (place, properties) { const hierarchies = _.get(properties, 'wof:hierarchy', []) - const pt = spec.names.get(place.ontology.type) const id = parseInt(place.identity.id, 10) hierarchies.forEach((hierarchy, o) => { // sort hierarchy and ensure self-reference exists - const sorted = sortHierarchy({ ...hierarchy, ...{ [`${pt.name}_id`]: id } }) + // note: place.ontology.type is used directly (rather than looking it up + // via spec.names) so that records with a placetype not present in the + // spec file (eg. a WOF placetype spatial doesn't support yet) still get + // a self-reference instead of crashing - sortHierarchy() already treats + // unrecognised placetypes as rank 0. + const sorted = sortHierarchy({ ...hierarchy, ...{ [`${place.ontology.type}_id`]: id } }) let depth = 0 for (const key in sorted) { diff --git a/import/source/whosonfirst/map/hierarchies.test.js b/import/source/whosonfirst/map/hierarchies.test.js index 8554f24..2086c55 100644 --- a/import/source/whosonfirst/map/hierarchies.test.js +++ b/import/source/whosonfirst/map/hierarchies.test.js @@ -138,6 +138,28 @@ tap.test('mapper: key ordering', (t) => { t.end() }) +tap.test('mapper: record\'s own placetype not in spec (eg. postalregion) should not throw, ' + + 'and should still get a self-reference', (t) => { + const p = new Place(new Identity('wof', '1864205377'), new Ontology('admin', 'postalregion')) + map(p, { + 'wof:hierarchy': [ + { + continent_id: 102191581, + country_id: 85633159, + empire_id: 136253055 + } + ] + }) + + t.same([ + new Hierarchy(p.identity, new Identity('wof', '85633159'), 'wof:0', 0), + new Hierarchy(p.identity, new Identity('wof', '136253055'), 'wof:0', 1), + new Hierarchy(p.identity, new Identity('wof', '102191581'), 'wof:0', 2), + new Hierarchy(p.identity, p.identity, 'wof:0', 3) + ], p.hierarchy) + + t.end() +}) tap.test('mapper: sort hierarchy - unknown placetype', (t) => { const p = new Place(new Identity('wof', '1729339019'), new Ontology('admin', 'locality')) map(p, {