Skip to content

Commit 98e20b8

Browse files
authored
Adding better handling of invalid intersects geometry (#523)
* Adding system test
1 parent 0e25f31 commit 98e20b8

File tree

10 files changed

+1553
-31
lines changed

10 files changed

+1553
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
- Publish ingest results to a post-ingest SNS topic
1313
- Add datetime and bbox attributes to post-ingest SNS messages
1414
- Support for Query Extension operators neq, startsWith, endsWith, and contains.
15+
- Validate intersects geometry before sending to Search + better response parsing.
1516

1617
### Changed
1718

package-lock.json

Lines changed: 420 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"test:system": "./bin/system-tests.sh",
2222
"test:unit": "ava tests/unit/*.[tj]s",
2323
"typecheck": "tsc",
24-
"audit-prod": "npx better-npm-audit audit --production",
24+
"audit-prod": "npx better-npm-audit audit --production -x 1091470",
2525
"deploy": "sls deploy",
2626
"package": "sls package",
2727
"serve": "REQUEST_LOGGING_FORMAT=dev LOG_LEVEL=debug STAC_API_URL=http://localhost:3000 ENABLE_TRANSACTIONS_EXTENSION=true nodemon --esm ./src/lambdas/api/local.ts",
@@ -64,6 +64,7 @@
6464
"@aws-sdk/client-secrets-manager": "^3.341.0",
6565
"@elastic/elasticsearch": "^7.9.0",
6666
"@mapbox/extent": "^0.4.0",
67+
"@mapbox/geojsonhint": "^3.1.1",
6768
"@opensearch-project/opensearch": "^2.2.1",
6869
"aws-os-connection": "^0.2.0",
6970
"better-npm-audit": "^3.7.3",

src/lib/api.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { pickBy, assign, get as getNested } from 'lodash-es'
22
import extent from '@mapbox/extent'
33
import { DateTime } from 'luxon'
44
import AWS from 'aws-sdk'
5+
import geojsonhint from '@mapbox/geojsonhint'
56
import { isIndexNotFoundError } from './database.js'
67
import logger from './logger.js'
78

@@ -617,6 +618,19 @@ const searchItems = async function (collectionId, queryParameters, backend, endp
617618

618619
logger.debug('Search parameters: %j', searchParams)
619620

621+
try { // Attempt to catch invalid geometry before querying Search
622+
const hints = geometry ? geojsonhint.hint(geometry, {}) : []
623+
624+
if (hints.length > 0) {
625+
return {
626+
statusCode: 400,
627+
body: hints.map(({ message }) => ({ message }))
628+
}
629+
}
630+
} catch (e) {
631+
logger.error(e)
632+
}
633+
620634
let esResponse
621635
try {
622636
esResponse = await backend.search(searchParams, page, limit)
@@ -631,7 +645,28 @@ const searchItems = async function (collectionId, queryParameters, backend, endp
631645
results: []
632646
}
633647
} else {
634-
throw error
648+
try {
649+
// @ts-ignore
650+
const e = error['meta']['body']['error']
651+
652+
let errorMessage
653+
if ('caused_by' in e) { // Parse certain types of geometry errors from Search
654+
errorMessage = e['caused_by'].map(({ message }) => ({ message }))
655+
} else if ('root_cause' in e) { // Parse other types of geometry errors from Search
656+
errorMessage = e['root_cause'].map(({ reason }) => ({ reason }))
657+
} else if (JSON.stringify(error).includes('failed to create query')) {
658+
errorMessage = 'Query failed. Please verify a valid query payload.'
659+
} else {
660+
throw error
661+
}
662+
663+
return {
664+
statusCode: 400,
665+
body: errorMessage
666+
}
667+
} catch (_) {
668+
throw error
669+
}
635670
}
636671
}
637672

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"type": "Polygon",
3+
"coordinates": [
4+
[
5+
[100.0, 0.0],
6+
[101.0, 0.0],
7+
[101.0, 1.0],
8+
[100.0, 1.0],
9+
[100.0, 1.0],
10+
[100.0, 0.0]
11+
]
12+
]
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"type": "Polygon",
3+
"coordinates": [
4+
[
5+
[100.0, 0.0],
6+
[101.0, 0.0],
7+
[101.0, 1.0],
8+
[100.0, 1.0],
9+
[100.0, 2.0],
10+
[100.0, 1.0],
11+
[100.0, 0.0]
12+
]
13+
]
14+
}

0 commit comments

Comments
 (0)