Skip to content

Commit 6b87c39

Browse files
authored
Merge pull request #25 from sparkgeo/dev
Better error handling
2 parents 90a51c5 + 0ea6aa3 commit 6b87c39

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

.circleci/config.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: 2
2+
jobs:
3+
build:
4+
branches:
5+
only:
6+
- dev
7+
- master
8+
- ci
9+
working_directory: ~/circleci/stac-validator
10+
docker:
11+
- image: circleci/python:3.6.4
12+
steps:
13+
- checkout
14+
- run: sudo chown -R circleci:circleci /usr/local/bin
15+
- run: sudo chown -R circleci:circleci /usr/local/lib/python3.6/site-packages
16+
- restore_cache:
17+
key: v1-python-requirements-{{ checksum "requirements.txt" }}
18+
- run:
19+
command: |
20+
pip install -r requirements.txt
21+
- save_cache:
22+
key: v1-python-requirements-{{ checksum "requirements.txt" }}
23+
paths:
24+
- "~/.cache/pip"
25+
- "/usr/local/lib/python3.6/site-packages"
26+
- run:
27+
command: |
28+
python -m pytest

stac_validator.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,9 @@ async def _validate_child(self, child_url, messages):
160160

161161
messages.append(stac.message)
162162

163-
if "valid_json" in stac.message and not stac.message["valid_json"]:
164-
stac.message.pop("valid_json", None)
165-
stac.status.pop("valid_json", None)
166-
pass
163+
if "error_type" in stac.message:
164+
stac.message.pop("error_type", None)
165+
stac.status.pop("error_type", None)
167166
else:
168167
self.status["catalogs"]["valid"] += stac.status["catalogs"]["valid"]
169168
self.status["catalogs"]["invalid"] += stac.status["catalogs"]["invalid"]
@@ -221,10 +220,15 @@ async def run(self):
221220
self.stac_file = data
222221
except JSONDecodeError as e:
223222
self.message["valid_stac"] = False
224-
self.message["valid_json"] = False
223+
self.message["error_type"] = "InvalidJSON"
225224
self.message["error"] = f"{self.stac_file} is not Valid JSON"
226225
self.status = self.message
227-
return json.dumps(self.message)
226+
# return json.dumps(self.message)
227+
except FileNotFoundError as e:
228+
self.message["valid_stac"] = False
229+
self.message["error_type"] = "FileNotFoundError"
230+
self.message["error"] = f"{self.stac_file} cannot be found"
231+
self.status = self.message
228232

229233
# Check STAC Type
230234
if "catalog" in self.fpath.stem:
@@ -239,7 +243,9 @@ async def run(self):
239243
else:
240244
self.status["catalogs"]["invalid"] += 1
241245
self.message["children"] = await self.validate_catalog_contents()
242-
elif any(field in Collections_Fields for field in self.stac_file.keys()):
246+
elif (self.stac_file) is dict and (
247+
field in Collections_Fields for field in self.stac_file.keys()
248+
):
243249
# Congratulations, It's a Collection!
244250
# Collections will validate as catalog.
245251
self.message["asset_type"] = "collection"
@@ -252,6 +258,10 @@ async def run(self):
252258
else:
253259
self.status["collections"]["invalid"] += 1
254260
self.message["children"] = await self.validate_catalog_contents()
261+
elif "error_type" in self.message:
262+
self.message.pop("error_type", None)
263+
self.status.pop("error_type", None)
264+
255265
else:
256266
# Congratulations, It's an Item!
257267
self.message["asset_type"] = "item"

tests/test_stac_validator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ def test_bad_url():
138138
)
139139
assert stac.status == {
140140
"valid_stac": False,
141-
"valid_json": False,
142141
"error": "https://s3.amazonaws.com/spacenet-stac/spacenet-dataset/AOI_4_Shanghai_MUL-PanSharpen_Cloud is not Valid JSON",
142+
"path": "https:/s3.amazonaws.com/spacenet-stac/spacenet-dataset/AOI_4_Shanghai_MUL-PanSharpen_Cloud"
143143
}
144144
assert stac.message == {
145145
"valid_stac": False,
146-
"valid_json": False,
147146
"error": "https://s3.amazonaws.com/spacenet-stac/spacenet-dataset/AOI_4_Shanghai_MUL-PanSharpen_Cloud is not Valid JSON",
147+
"path": "https:/s3.amazonaws.com/spacenet-stac/spacenet-dataset/AOI_4_Shanghai_MUL-PanSharpen_Cloud"
148148
}

0 commit comments

Comments
 (0)