Skip to content

Commit

Permalink
/ and /collections returns 500 if create_indices has not been run (#663)
Browse files Browse the repository at this point in the history
* /collections returns 404 if create_indices has not been run

* return server error 500 if server has not been initialized
  • Loading branch information
Phil Varner authored Dec 15, 2023
1 parent 53f4dc4 commit 4b84017
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
18 changes: 8 additions & 10 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug AVA test file",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/ava",
"runtimeArgs": [
"${file}"
"name": "run api get collections",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/node_modules/ava/entrypoints/cli.mjs",
"args": [
"${workspaceFolder}/tests/system/test-api-get-collections.js"
],
"outputCapture": "std",
"skipFiles": [
"<node_internals>/**/*.js"
]
"console": "integratedTerminal"
}
]
}
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased] - TBD

### Changed

- Landing page (/) and collections endpoint (/collections) now return a 500 if
create_indices has not been run or cannot connect to database.

## [3.3.0] - 2023-12-04

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

Compliant with STAC 0.9.0

<!-- [Unreleased]: https://github.com/stac-utils/stac-api/compare/v2.4.0...main -->
[Unreleased]: https://github.com/stac-utils/stac-api/compare/v3.3.0...main
[3.3.0]: https://github.com/stac-utils/stac-api/compare/v3.2.0...v3.3.0
[3.2.0]: https://github.com/stac-utils/stac-api/compare/v3.1.0...v3.2.0
[3.1.0]: https://github.com/stac-utils/stac-api/compare/v3.0.0...v3.1.0
Expand Down
10 changes: 7 additions & 3 deletions src/lambdas/api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ app.use(addEndpoint)

app.get('/', async (req, res, next) => {
try {
res.json(await api.getCatalog(txnEnabled, database, req.endpoint))
const response = await api.getCatalog(txnEnabled, database, req.endpoint)
if (response instanceof Error) next(createError(500, response.message))
else res.json(response)
} catch (error) {
next(error)
}
Expand Down Expand Up @@ -143,7 +145,9 @@ app.get('/aggregations', async (req, res, next) => {

app.get('/collections', async (req, res, next) => {
try {
res.json(await api.getCollections(database, req.endpoint))
const response = await api.getCollections(database, req.endpoint)
if (response instanceof Error) next(createError(500, response.message))
else res.json(response)
} catch (error) {
next(error)
}
Expand Down Expand Up @@ -445,7 +449,7 @@ app.use(
break
default:
logger.error(err)
res.json({ code: 'InternalServerError', description: 'Internal Server Error' })
res.json({ code: 'InternalServerError', description: err.message })
break
}
})
Expand Down
24 changes: 17 additions & 7 deletions src/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,15 @@ const getGlobalAggregations = async (endpoint = '') => {
}

const getCatalog = async function (txnEnabled, backend, endpoint = '') {
const collectionsOrError = await backend.getCollections(1, COLLECTION_LIMIT)
if (collectionsOrError instanceof Error) {
return collectionsOrError
}

const catalog = collectionsToCatalogLinks(collectionsOrError, endpoint)

catalog.conformsTo = (await getConformance(txnEnabled)).conformsTo

const links = [
{
rel: 'self',
Expand Down Expand Up @@ -1046,10 +1055,7 @@ const getCatalog = async function (txnEnabled, backend, endpoint = '') {
})
}

const collections = await backend.getCollections(1, COLLECTION_LIMIT)
const catalog = collectionsToCatalogLinks(collections, endpoint)
catalog.links = links.concat(catalog.links)
catalog.conformsTo = (await getConformance(txnEnabled)).conformsTo

return catalog
}
Expand All @@ -1063,14 +1069,18 @@ const deleteUnusedFields = (collection) => {
const getCollections = async function (backend, endpoint = '') {
// TODO: implement proper pagination, as this will only return up to
// COLLECTION_LIMIT collections
const collections = await backend.getCollections(1, COLLECTION_LIMIT)
for (const collection of collections) {
const collectionsOrError = await backend.getCollections(1, COLLECTION_LIMIT)
if (collectionsOrError instanceof Error) {
return collectionsOrError
}

for (const collection of collectionsOrError) {
deleteUnusedFields(collection)
}

const linkedCollections = addCollectionLinks(collections, endpoint)
const linkedCollections = addCollectionLinks(collectionsOrError, endpoint)
const resp = {
collections,
collections: collectionsOrError,
links: [
{
rel: 'self',
Expand Down
4 changes: 3 additions & 1 deletion src/lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,10 @@ async function getCollections(page = 1, limit = 100) {
return response.body.hits.hits.map((r) => (r._source))
} catch (e) {
logger.error('Failure getting collections, maybe none exist?', e)
return new Error('Collections not found. This is likely '
+ 'because the server has not been initialized with create_indices, '
+ 'cannot connect to the database, or cannot authenticate to the database.')
}
return []
}

async function populateCollectionToIndexMapping() {
Expand Down

0 comments on commit 4b84017

Please sign in to comment.