Skip to content

Commit

Permalink
Raise more informative errors from CLI (#554)
Browse files Browse the repository at this point in the history
* Raise more informative errors from CLI

* Fix error type

* Update changelog

---------

Co-authored-by: Pete Gadomski <pete.gadomski@gmail.com>
  • Loading branch information
jsignell and gadomski authored Aug 10, 2023
1 parent 51f76af commit 1454437
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 42 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Changed

- Raise more informative errors from CLI [#554](https://github.com/stac-utils/pystac-client/pull/554)

### Fixed

- Updated `get_items` signatures for PySTAC v1.8 [#559](https://github.com/stac-utils/pystac-client/pull/559)
Expand Down
75 changes: 33 additions & 42 deletions pystac_client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import warnings
from typing import Any, Dict, List, Optional

from pystac import STACTypeError
from pystac import STACError, STACTypeError

from .client import Client
from .conformance import ConformanceClasses
Expand All @@ -33,27 +33,25 @@ def search(
) -> int:
"""Main function for performing a search"""

try:
# https://github.com/python/mypy/issues/4441
# the type: ignore is to silence the mypy error
# error: Argument 2 to "search" of "Client" has incompatible
# type "**Dict[str, Dict[str, Any]]"; expected "Optional[int]" [arg-type]
result = client.search(method=method, **kwargs) # type: ignore[arg-type]

if matched:
print(f"{result.matched()} items matched")
else:
feature_collection = result.item_collection_as_dict()
if save:
with open(save, "w") as f:
f.write(json.dumps(feature_collection))
else:
print(json.dumps(feature_collection))
return 0
# https://github.com/python/mypy/issues/4441
# the type: ignore is to silence the mypy error
# error: Argument 2 to "search" of "Client" has incompatible
# type "**Dict[str, Dict[str, Any]]"; expected "Optional[int]" [arg-type]
result = client.search(method=method, **kwargs) # type: ignore[arg-type]

except Exception as e:
print(f"ERROR: {e}")
return 1
if matched:
if nmatched := result.matched() is not None:
print(f"{nmatched} items matched")
else:
raise KeyError("'matched' is not supported for this catalog")
else:
feature_collection = result.item_collection_as_dict()
if save:
with open(save, "w") as f:
f.write(json.dumps(feature_collection))
else:
print(json.dumps(feature_collection))
return 0


def collections(client: Client, save: Optional[str] = None) -> int:
Expand All @@ -65,17 +63,12 @@ def collections(client: Client, save: Optional[str] = None) -> int:
f.write(json.dumps(collections_dicts))
else:
print(json.dumps(collections_dicts))
return 0
except STACTypeError as e:
print(f"ERROR: {e}")
print(
raise STACError(
f"The client at {client.self_href} is OK, but one or more of the "
"collections is invalid."
)
return 1
except Exception as e:
print(f"ERROR: {e}")
return 1
) from e
return 0


def add_warnings_behavior(parser: argparse.ArgumentParser) -> None:
Expand Down Expand Up @@ -337,20 +330,18 @@ def cli() -> int:
args.pop("add_conforms_to", None),
)

if cmd == "search":
return search(client, **args)
elif cmd == "collections":
return collections(client, **args)
else:
logger.error(
f"Command '{cmd}' is not a valid command. "
"must be 'search' or 'collections'",
)
return 1
except Exception as e:
print(f"ERROR: {e}", file=sys.stderr)
return 1

if cmd == "search":
return search(client, **args)
elif cmd == "collections":
return collections(client, **args)
else:
print(
f"Command '{cmd}' is not a valid command. "
"must be 'search' or 'collections'",
file=sys.stderr,
)
logger.error(e, exc_info=True)
return 1


Expand Down

0 comments on commit 1454437

Please sign in to comment.