@@ -6,6 +6,13 @@ import { isCollection, isItem } from './stac-utils.js'
6
6
7
7
const COLLECTIONS_INDEX = process . env [ 'COLLECTIONS_INDEX' ] || 'collections'
8
8
9
+ export class InvalidIngestError extends Error {
10
+ constructor ( message ) {
11
+ super ( message )
12
+ this . name = 'InvalidIngestError'
13
+ }
14
+ }
15
+
9
16
export async function convertIngestObjectToDbObject (
10
17
// eslint-disable-next-line max-len
11
18
/** @type {{ hasOwnProperty: (arg0: string) => any; type: string, collection: string; links: any[]; id: any; } } */ data
@@ -17,11 +24,16 @@ export async function convertIngestObjectToDbObject(
17
24
} else if ( isItem ( data ) ) {
18
25
index = data . collection
19
26
} 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
+ )
21
30
}
22
31
23
32
// remove any hierarchy links in a non-mutating way
24
33
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
+ }
25
37
const links = data . links . filter (
26
38
( /** @type {{ rel: string; } } */ link ) => ! hlinks . includes ( link . rel )
27
39
)
@@ -79,9 +91,7 @@ export async function writeRecordToDb(
79
91
// if this isn't a collection check if index exists
80
92
const exists = await client . indices . exists ( { index } )
81
93
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` )
85
95
}
86
96
}
87
97
@@ -117,7 +127,13 @@ export async function writeRecordsInBulkToDb(records) {
117
127
function logIngestItemsResults ( results ) {
118
128
results . forEach ( ( result ) => {
119
129
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
+ }
121
137
} else {
122
138
logger . debug ( 'Ingested item %j' , result )
123
139
}
0 commit comments