Skip to content

Commit dadb066

Browse files
committed
Use a InvalidIngestError subclass to separate invalid input failures
Attempting to ingest invalid stac objects is not a system error so we log it as invo and not error.
1 parent 5a6a93f commit dadb066

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/lib/ingest.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import { isCollection, isItem } from './stac-utils.js'
66

77
const COLLECTIONS_INDEX = process.env['COLLECTIONS_INDEX'] || 'collections'
88

9+
export class InvalidIngestError extends Error {
10+
constructor(message) {
11+
super(message)
12+
this.name = 'InvalidIngestError'
13+
}
14+
}
15+
916
export async function convertIngestObjectToDbObject(
1017
// eslint-disable-next-line max-len
1118
/** @type {{ hasOwnProperty: (arg0: string) => any; type: string, collection: string; links: any[]; id: any; }} */ data
@@ -17,11 +24,16 @@ export async function convertIngestObjectToDbObject(
1724
} else if (isItem(data)) {
1825
index = data.collection
1926
} else {
20-
throw new Error(`Expeccted data.type to be "Collection" or "Feature" not ${data.type}`)
27+
throw new InvalidIngestError(
28+
`Expeccted data.type to be "Collection" or "Feature" not ${data.type}`
29+
)
2130
}
2231

2332
// remove any hierarchy links in a non-mutating way
2433
const hlinks = ['self', 'root', 'parent', 'child', 'collection', 'item', 'items']
34+
if (!data.links) {
35+
throw new InvalidIngestError('Expected a "links" proporty on the stac object')
36+
}
2537
const links = data.links.filter(
2638
(/** @type {{ rel: string; }} */ link) => !hlinks.includes(link.rel)
2739
)
@@ -79,9 +91,7 @@ export async function writeRecordToDb(
7991
// if this isn't a collection check if index exists
8092
const exists = await client.indices.exists({ index })
8193
if (!exists.body) {
82-
const msg = `Index ${index} does not exist, add before ingesting items`
83-
logger.debug(msg)
84-
throw new Error(msg)
94+
throw new InvalidIngestError(`Index ${index} does not exist, add before ingesting items`)
8595
}
8696
}
8797

@@ -117,7 +127,13 @@ export async function writeRecordsInBulkToDb(records) {
117127
function logIngestItemsResults(results) {
118128
results.forEach((result) => {
119129
if (result.error) {
120-
logger.error('Error while ingesting item', result.error)
130+
if (result.error instanceof InvalidIngestError) {
131+
// Attempting to ingest invalid stac objects is not a system error so we
132+
// log it as info and not error
133+
logger.info('Invalid ingest item', result.error)
134+
} else {
135+
logger.error('Error while ingesting item', result.error)
136+
}
121137
} else {
122138
logger.debug('Ingested item %j', result)
123139
}

0 commit comments

Comments
 (0)