Skip to content

Commit cfb8d37

Browse files
authored
Beautify Errors (#177)
* Beautify errors * Beautify errors * Beautify errors * Beautify errors
1 parent b3d010d commit cfb8d37

File tree

11 files changed

+36
-24
lines changed

11 files changed

+36
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
=======
44

5+
# 1.9.3 (2023-06-27)
6+
- Beautified error messages
7+
58
# 1.9.2 (2023-06-21)
69
- Use Bionic dist instead of Xenial for Travis build
710

mapbox_tilesets/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""mapbox_tilesets package"""
22

3-
__version__ = "1.9.0"
3+
__version__ = "1.9.3"

mapbox_tilesets/errors.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Error handling for the tilesets CLI"""
2+
from click import ClickException
23

34

4-
class TilesetsError(Exception):
5+
class TilesetsError(ClickException):
56
"""Base Tilesets error
67
Deriving errors from this base isolates module development
78
problems from Python usage problems.

mapbox_tilesets/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import numpy as np
66

7-
from jsonschema import validate
7+
from click import ClickException
8+
from jsonschema import validate, ValidationError
89
from requests import Session
910

1011
import mapbox_tilesets
@@ -126,7 +127,10 @@ def validate_geojson(index, feature):
126127
},
127128
},
128129
}
129-
validate(instance=feature, schema=schema)
130+
try:
131+
validate(instance=feature, schema=schema)
132+
except ValidationError as e:
133+
raise ClickException(e)
130134
geojson_validate(index, feature)
131135

132136

tests/test_cli_delete.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from unittest import mock
66

77
from mapbox_tilesets.scripts.cli import delete
8-
from mapbox_tilesets.errors import TilesetsError
98

109

1110
class MockResponse:
@@ -82,4 +81,4 @@ def test_cli_delete_fail(mock_request_delete):
8281
"https://api.mapbox.com/tilesets/v1/test.id?access_token=pk.eyJ1IjoidGVzdC11c2VyIn0K"
8382
)
8483
assert result.exit_code == 1
85-
assert isinstance(result.exception, TilesetsError)
84+
assert isinstance(result.exception, SystemExit)

tests/test_cli_estimate_area.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from click.testing import CliRunner
22

33
from mapbox_tilesets.scripts.cli import estimate_area
4+
from utils import clean_runner_output
45

56

67
# rainy day scenarios
@@ -11,7 +12,7 @@ def test_cli_estimate_area_features_from_invalid_stdin_geojson():
1112
estimate_area, ["--precision", "10m"], input="invalidGeoJson input"
1213
)
1314
assert invalidated_result.exit_code == 1
14-
assert str(invalidated_result.exception) == message
15+
assert clean_runner_output(invalidated_result.output) == message
1516

1617

1718
def test_cli_estimate_area_features_from_invalid_geojson_content():
@@ -21,7 +22,7 @@ def test_cli_estimate_area_features_from_invalid_geojson_content():
2122
estimate_area,
2223
["tests/fixtures/invalid-geojson.ldgeojson", "--precision", "1m"],
2324
)
24-
assert message in str(invalidated_result.exception)
25+
assert message in str(invalidated_result.output)
2526
assert invalidated_result.exit_code == 1
2627

2728

@@ -32,7 +33,7 @@ def test_cli_estimate_area_features_from_nonexistent_geojson_file():
3233
estimate_area,
3334
["tests/fixtures/nonexistent-geojson.ldgeojson", "--precision", "1m"],
3435
)
35-
assert str(invalidated_result.exception) == message
36+
assert clean_runner_output(invalidated_result.output) == message
3637
assert invalidated_result.exit_code == 1
3738

3839

@@ -64,7 +65,7 @@ def test_cli_estimate_area_1cm_precision_without_flag():
6465
["tests/fixtures/valid.ldgeojson", "-p", "1cm"],
6566
)
6667
assert invalidated_result.exit_code == 1
67-
assert str(invalidated_result.exception) == message
68+
assert clean_runner_output(invalidated_result.output) == message
6869

6970

7071
def test_cli_estimate_area_invalid_1cm_precision_flag():
@@ -75,7 +76,7 @@ def test_cli_estimate_area_invalid_1cm_precision_flag():
7576
["tests/fixtures/valid.ldgeojson", "-p", "1m", "--force-1cm"],
7677
)
7778
assert invalidated_result.exit_code == 1
78-
assert str(invalidated_result.exception) == message
79+
assert clean_runner_output(invalidated_result.output) == message
7980

8081

8182
# sunny day scenarios

tests/test_cli_sources.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
validate_source,
1515
list_sources,
1616
)
17+
from utils import clean_runner_output
1718

1819

1920
@pytest.mark.usefixtures("token_environ")
@@ -73,7 +74,7 @@ def test_cli_add_source_wrong_username(
7374
assert validated_result.exit_code == 1
7475

7576
assert (
76-
str(validated_result.exception)
77+
clean_runner_output(validated_result.output)
7778
== "Token username wrong-user does not match username test-user-wrong"
7879
)
7980

@@ -91,7 +92,7 @@ def test_cli_add_source_no_token():
9192
assert unauthenticated_result.exit_code == 1
9293

9394
assert (
94-
str(unauthenticated_result.exception)
95+
clean_runner_output(unauthenticated_result.output)
9596
== """No access token provided. Please set the MAPBOX_ACCESS_TOKEN environment variable or use the --token flag."""
9697
)
9798

@@ -116,7 +117,7 @@ def test_cli_add_source_no_validation(mock_request_post, MockResponse):
116117
assert no_validation_result.exit_code == 1
117118

118119
assert (
119-
no_validation_result.exception.message
120+
clean_runner_output(no_validation_result.output)
120121
== '{"message": "Invalid file format. Only GeoJSON features are allowed."}'
121122
)
122123

@@ -254,7 +255,7 @@ def side_effect(fields):
254255
assert validated_result.exit_code == 1
255256

256257
assert (
257-
str(validated_result.exception)
258+
clean_runner_output(validated_result.output)
258259
== "Error in feature number 0: Each linear ring must end where it started"
259260
)
260261

tests/test_cli_status.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from unittest import mock
66

77
from mapbox_tilesets.scripts.cli import status
8-
from mapbox_tilesets.errors import TilesetsError
8+
from utils import clean_runner_output
99

1010

1111
@pytest.mark.usefixtures("token_environ")
@@ -63,5 +63,5 @@ def test_cli_status_error(mock_request_get, MockResponse):
6363
"https://api.mapbox.com/tilesets/v1/test.id/jobs?limit=1&access_token=pk.eyJ1IjoidGVzdC11c2VyIn0K"
6464
)
6565
assert result.exit_code == 1
66-
assert isinstance(result.exception, TilesetsError)
67-
assert result.exception.message == '{"message": "test.id has no jobs."}'
66+
assert isinstance(result.exception, SystemExit)
67+
assert clean_runner_output(result.output) == '{"message": "test.id has no jobs."}'

tests/test_cli_tilejson.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from unittest import mock
44
from click.testing import CliRunner
55
from mapbox_tilesets.scripts.cli import tilejson
6-
from mapbox_tilesets.errors import TilesetsError, TilesetNameError
6+
from utils import clean_runner_output
77

88

99
class MockResponse:
@@ -94,7 +94,7 @@ def test_cli_tilejson_error(mock_request_get, MockResponse):
9494
"https://api.mapbox.com/v4/test.id,test.another.json?access_token=pk.eyJ1IjoidGVzdC11c2VyIn0K"
9595
)
9696
assert result.exit_code == 1
97-
assert isinstance(result.exception, TilesetsError)
97+
assert isinstance(result.exception, SystemExit)
9898

9999

100100
@pytest.mark.usefixtures("token_environ")
@@ -104,5 +104,5 @@ def test_cli_tilejson_invalid_tileset_id():
104104
# sends expected request
105105
result = runner.invoke(tilejson, ["invalid@@id"])
106106
assert result.exit_code == 1
107-
assert isinstance(result.exception, TilesetNameError)
108-
assert "invalid@@id" in str(result.exception)
107+
assert isinstance(result.exception, SystemExit)
108+
assert clean_runner_output(result.output) == "Invalid Tileset ID"

tests/test_cli_view_recipe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from unittest import mock
66

77
from mapbox_tilesets.scripts.cli import view_recipe
8+
from utils import clean_runner_output
89

910

1011
@pytest.mark.usefixtures("token_environ")
@@ -50,5 +51,4 @@ def test_cli_view_recipe_raises(mock_request_get, MockResponse):
5051
"https://api.mapbox.com/tilesets/v1/test.id/recipe?access_token=pk.eyJ1IjoidGVzdC11c2VyIn0K"
5152
)
5253
assert result.exit_code == 1
53-
54-
assert result.exception.message == '"not found"'
54+
assert clean_runner_output(result.output) == '"not found"'

0 commit comments

Comments
 (0)