Skip to content

Commit 4b84017

Browse files
author
Phil Varner
authored
/ and /collections returns 500 if create_indices has not been run (#663)
* /collections returns 404 if create_indices has not been run * return server error 500 if server has not been initialized
1 parent 53f4dc4 commit 4b84017

File tree

5 files changed

+43
-22
lines changed

5 files changed

+43
-22
lines changed

.vscode/launch.json

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
{
2-
// Use IntelliSense to learn about possible attributes.
3-
// Hover to view descriptions of existing attributes.
4-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
52
"version": "0.2.0",
63
"configurations": [
74
{
85
"type": "node",
96
"request": "launch",
10-
"name": "Debug AVA test file",
11-
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/ava",
12-
"runtimeArgs": [
13-
"${file}"
7+
"name": "run api get collections",
8+
"skipFiles": [
9+
"<node_internals>/**"
10+
],
11+
"program": "${workspaceFolder}/node_modules/ava/entrypoints/cli.mjs",
12+
"args": [
13+
"${workspaceFolder}/tests/system/test-api-get-collections.js"
1414
],
1515
"outputCapture": "std",
16-
"skipFiles": [
17-
"<node_internals>/**/*.js"
18-
]
16+
"console": "integratedTerminal"
1917
}
2018
]
2119
}

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased] - TBD
9+
10+
### Changed
11+
12+
- Landing page (/) and collections endpoint (/collections) now return a 500 if
13+
create_indices has not been run or cannot connect to database.
14+
815
## [3.3.0] - 2023-12-04
916

1017
### Added
@@ -412,7 +419,7 @@ Initial release, forked from [sat-api](https://github.com/sat-utils/sat-api/tree
412419

413420
Compliant with STAC 0.9.0
414421

415-
<!-- [Unreleased]: https://github.com/stac-utils/stac-api/compare/v2.4.0...main -->
422+
[Unreleased]: https://github.com/stac-utils/stac-api/compare/v3.3.0...main
416423
[3.3.0]: https://github.com/stac-utils/stac-api/compare/v3.2.0...v3.3.0
417424
[3.2.0]: https://github.com/stac-utils/stac-api/compare/v3.1.0...v3.2.0
418425
[3.1.0]: https://github.com/stac-utils/stac-api/compare/v3.0.0...v3.1.0

src/lambdas/api/app.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ app.use(addEndpoint)
4343

4444
app.get('/', async (req, res, next) => {
4545
try {
46-
res.json(await api.getCatalog(txnEnabled, database, req.endpoint))
46+
const response = await api.getCatalog(txnEnabled, database, req.endpoint)
47+
if (response instanceof Error) next(createError(500, response.message))
48+
else res.json(response)
4749
} catch (error) {
4850
next(error)
4951
}
@@ -143,7 +145,9 @@ app.get('/aggregations', async (req, res, next) => {
143145

144146
app.get('/collections', async (req, res, next) => {
145147
try {
146-
res.json(await api.getCollections(database, req.endpoint))
148+
const response = await api.getCollections(database, req.endpoint)
149+
if (response instanceof Error) next(createError(500, response.message))
150+
else res.json(response)
147151
} catch (error) {
148152
next(error)
149153
}
@@ -445,7 +449,7 @@ app.use(
445449
break
446450
default:
447451
logger.error(err)
448-
res.json({ code: 'InternalServerError', description: 'Internal Server Error' })
452+
res.json({ code: 'InternalServerError', description: err.message })
449453
break
450454
}
451455
})

src/lib/api.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,15 @@ const getGlobalAggregations = async (endpoint = '') => {
976976
}
977977

978978
const getCatalog = async function (txnEnabled, backend, endpoint = '') {
979+
const collectionsOrError = await backend.getCollections(1, COLLECTION_LIMIT)
980+
if (collectionsOrError instanceof Error) {
981+
return collectionsOrError
982+
}
983+
984+
const catalog = collectionsToCatalogLinks(collectionsOrError, endpoint)
985+
986+
catalog.conformsTo = (await getConformance(txnEnabled)).conformsTo
987+
979988
const links = [
980989
{
981990
rel: 'self',
@@ -1046,10 +1055,7 @@ const getCatalog = async function (txnEnabled, backend, endpoint = '') {
10461055
})
10471056
}
10481057

1049-
const collections = await backend.getCollections(1, COLLECTION_LIMIT)
1050-
const catalog = collectionsToCatalogLinks(collections, endpoint)
10511058
catalog.links = links.concat(catalog.links)
1052-
catalog.conformsTo = (await getConformance(txnEnabled)).conformsTo
10531059

10541060
return catalog
10551061
}
@@ -1063,14 +1069,18 @@ const deleteUnusedFields = (collection) => {
10631069
const getCollections = async function (backend, endpoint = '') {
10641070
// TODO: implement proper pagination, as this will only return up to
10651071
// COLLECTION_LIMIT collections
1066-
const collections = await backend.getCollections(1, COLLECTION_LIMIT)
1067-
for (const collection of collections) {
1072+
const collectionsOrError = await backend.getCollections(1, COLLECTION_LIMIT)
1073+
if (collectionsOrError instanceof Error) {
1074+
return collectionsOrError
1075+
}
1076+
1077+
for (const collection of collectionsOrError) {
10681078
deleteUnusedFields(collection)
10691079
}
10701080

1071-
const linkedCollections = addCollectionLinks(collections, endpoint)
1081+
const linkedCollections = addCollectionLinks(collectionsOrError, endpoint)
10721082
const resp = {
1073-
collections,
1083+
collections: collectionsOrError,
10741084
links: [
10751085
{
10761086
rel: 'self',

src/lib/database.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,10 @@ async function getCollections(page = 1, limit = 100) {
417417
return response.body.hits.hits.map((r) => (r._source))
418418
} catch (e) {
419419
logger.error('Failure getting collections, maybe none exist?', e)
420+
return new Error('Collections not found. This is likely '
421+
+ 'because the server has not been initialized with create_indices, '
422+
+ 'cannot connect to the database, or cannot authenticate to the database.')
420423
}
421-
return []
422424
}
423425

424426
async function populateCollectionToIndexMapping() {

0 commit comments

Comments
 (0)