Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize error messages #84

Merged
merged 8 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.DEFAULT_GOAL := test

test:
pylint tap_shopify -d missing-docstring
pylint tap_shopify -d missing-docstring,too-many-branches
nosetests tests/unittests
77 changes: 49 additions & 28 deletions tap_shopify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from singer import metadata
from singer import Transformer
from tap_shopify.context import Context
from tap_shopify.exceptions import ShopifyError
import tap_shopify.streams # Load stream objects into Context

REQUIRED_CONFIG_KEYS = ["shop", "api_key"]
Expand Down Expand Up @@ -140,15 +141,32 @@ def sync():
Context.state['bookmarks']['currently_sync_stream'] = stream_id

with Transformer() as transformer:
for rec in stream.sync():
extraction_time = singer.utils.now()
record_schema = catalog_entry['schema']
record_metadata = metadata.to_map(catalog_entry['metadata'])
rec = transformer.transform(rec, record_schema, record_metadata)
singer.write_record(stream_id,
rec,
time_extracted=extraction_time)
Context.counts[stream_id] += 1
try:
for rec in stream.sync():
extraction_time = singer.utils.now()
record_schema = catalog_entry['schema']
record_metadata = metadata.to_map(catalog_entry['metadata'])
rec = transformer.transform(rec, record_schema, record_metadata)
singer.write_record(stream_id,
rec,
time_extracted=extraction_time)
Context.counts[stream_id] += 1
except pyactiveresource.connection.ResourceNotFound as exc:
raise ShopifyError(exc, 'Ensure shop is entered correctly') from exc
except pyactiveresource.connection.UnauthorizedAccess as exc:
raise ShopifyError(exc, 'Invalid access token - Re-authorize the connection') \
from exc
except pyactiveresource.connection.ConnectionError as exc:
msg = ''
try:
body_json = exc.response.body.decode()
body = json.loads(body_json)
msg = body.get('errors')
finally:
raise ShopifyError(exc, msg) from exc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, just to get this down, I wasn't sure this would work, but it seems like any exception thrown from JSON parsing will just get swallowed implicitly, and the true exception will bubble up. Nice.

>>> try:
...     raise Exception("1")
... except Exception as ex:
...     try:
...         raise Exception("2")
...     finally:
...         raise Exception("3") from ex
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
Exception: 1

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
Exception: 3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I was skeptical of it too but it works.

except Exception as exc:
raise ShopifyError(exc) from exc


Context.state['bookmarks'].pop('currently_sync_stream')
singer.write_state(Context.state)
Expand All @@ -158,27 +176,30 @@ def sync():
LOGGER.info('%s: %d', stream_id, stream_count)
LOGGER.info('----------------------')

@utils.handle_top_exception(LOGGER)
dmosorast marked this conversation as resolved.
Show resolved Hide resolved
def main():

# Parse command line arguments
args = utils.parse_args(REQUIRED_CONFIG_KEYS)

# If discover flag was passed, run discovery mode and dump output to stdout
if args.discover:
catalog = discover()
print(json.dumps(catalog, indent=2))
# Otherwise run in sync mode
else:
Context.tap_start = utils.now()
if args.catalog:
Context.catalog = args.catalog.to_dict()
try:
# Parse command line arguments
args = utils.parse_args(REQUIRED_CONFIG_KEYS)

# If discover flag was passed, run discovery mode and dump output to stdout
if args.discover:
catalog = discover()
print(json.dumps(catalog, indent=2))
# Otherwise run in sync mode
else:
Context.catalog = discover()

Context.config = args.config
Context.state = args.state
sync()
Context.tap_start = utils.now()
if args.catalog:
Context.catalog = args.catalog.to_dict()
else:
Context.catalog = discover()

Context.config = args.config
Context.state = args.state
sync()
except Exception as exc:
for line in str(exc).splitlines():
LOGGER.critical(line)
raise exc

if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions tap_shopify/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ShopifyError(Exception):
def __init__(self, error, msg=''):
super().__init__('{}\n{}'.format(error.__class__.__name__, msg))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So will this, e.g., come out like

CRITICAL pyactiveresource.connection.ResourceNotFound
CRITICAL Ensure shop is entered correctly

?

Copy link
Contributor Author

@cosimon cosimon Feb 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The full namespace of the exception is not printed.

CRITICAL ResourceNotFound
CRITICAL Ensure shop is entered correctly