Skip to content

Commit aa2fa51

Browse files
authored
Improve /search sortby error message (#1021)
* improve sortby error message * update changelog * upgrade packages to satisfy audit * syntax error after merge conflict
1 parent 5b86d83 commit aa2fa51

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1313
- Comprehensive documentation now organized into Getting Started, Guides, Reference, and About sections
1414
- Removed top-level markdown files (ARCHITECTURE.md, USAGE.md, DEPLOYMENT.md, CONFIGURATION.md, CONTRIBUTING.md, SECURITY.md) - all content migrated to docs/
1515
- Updated README.md to serve as GitHub landing page with links to full documentation
16+
- If an invalid sortby parameter is supplied, a 400 status is returned (instead of 500) with
17+
a helpful error message.
1618
- Bbox queries outside of [-180, -90, 180, 90] return a 400 error
1719

1820
## [4.5.0]

src/lib/api.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,15 @@ const searchItems = async function (
718718
let errorMessage
719719
if ('caused_by' in e) {
720720
errorMessage = JSON.stringify(e?.caused_by?.reason)
721+
} else if ('root_cause' in e) {
722+
const reason = e?.root_cause[0]?.reason
723+
errorMessage = reason
724+
if (reason.includes('No mapping found for')
725+
&& reason.includes('to sort on')) {
726+
errorMessage += '. (Hint: `sortby` requires fully '
727+
+ 'qualified identifiers, e.g. `properties.datetime` '
728+
+ 'instead of `datetime`)'
729+
}
721730
} else if (JSON.stringify(e).includes('failed to create query')) {
722731
errorMessage = `Query failed with invalid parameters: ${JSON.stringify(e)}`
723732
} else {

tests/system/test-api-search-get.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,15 @@ test('GET /search with filter restriction returns filtered results', async (t) =
386386
}
387387
})
388388

389+
test('/search sort unqualified field names fails', async (t) => {
390+
const error = await t.throwsAsync(async () => t.context.api.client.get('search', {
391+
searchParams: { sortby: '-datetime' }
392+
}))
393+
394+
t.is(error.response.statusCode, 400)
395+
t.truthy(error.response.body.description.includes('Hint: `sortby` requires fully qualified identifiers'))
396+
})
397+
389398
test('/search invalid bbox throws error', async (t) => {
390399
const error = await t.throwsAsync(async () => t.context.api.client.get('search', {
391400
searchParams: {

tests/system/test-api-search-post.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,20 @@ test('/search sort', async (t) => {
150150
t.is(response.features[0].id, 'LC80100102015082LGN00')
151151
})
152152

153+
test('/search sort unqualified field names fails', async (t) => {
154+
const error = await t.throwsAsync(async () => t.context.api.client.post('search', {
155+
json: {
156+
sortby: [{
157+
field: 'datetime',
158+
direction: 'desc'
159+
}]
160+
}
161+
}))
162+
163+
t.is(error.response.statusCode, 400)
164+
t.truthy(error.response.body.description.includes('Hint: `sortby` requires fully qualified identifiers'))
165+
})
166+
153167
test('/search flattened collection properties', async (t) => {
154168
let response = await t.context.api.client.post('search', {
155169
json: {

0 commit comments

Comments
 (0)