Skip to content

Python: Add custom exception for query errors - #4629

Merged
helderco merged 1 commit into
dagger:mainfrom
helderco:python/error-handling
Feb 23, 2023
Merged

helderco merged 1 commit into
dagger:mainfrom
helderco:python/error-handling

Conversation

@helderco

@helderco helderco commented Feb 21, 2023 •

Copy link
Copy Markdown
Contributor

Related to #3805

Example

With typo in:

.from_("python:alpine")

-      .from_("python:alpine")
+      .from_("python:alpines")

Before

...
    raise TransportQueryError(
gql.transport.exceptions.TransportQueryError: {'message': 'docker.io/library/python:alpines: not found', 'locations': [{'line': 3, 'column': 5}], 'path': ['container', 'from']}

After

...
    raise QueryError(
dagger.exceptions.QueryError: docker.io/library/python:alpines: not found

Capture exception

Exception can be captured easily from the dagger package:

try:
    anyio.run(main)
except dagger.QueryError as e:
    # handle error

Or on a specific query:

try:
    result = await ctr.stdout()
except dagger.QueryError as e:
    # handle error

IDE

Added a raises documentation block to the API methods where this error can be raised:
Screenshot 2023-02-22 at 12 25 38

Debug

If it helps, you can also inspect the query that was sent, with the error path and location.

try:
    anyio.run(main, sys.argv[1:])
except dagger.QueryError as e:
    console.print(f"ERROR in {e.path}: {e}", style="red")
    console.print(e.debug_query())
    sys.exit(1)

Screenshot 2023-02-22 at 12 19 11

Signed-off-by: Helder Correia

@helderco
helderco force-pushed the python/error-handling branch 2 times, most recently from 874b1b0 to 9525a5e Compare February 22, 2023 12:51
@helderco
helderco marked this pull request as ready for review February 22, 2023 13:30
@helderco
helderco requested a review from grouville February 22, 2023 13:30
@helderco
helderco force-pushed the python/error-handling branch from 9525a5e to d9a4d7c Compare February 22, 2023 17:45
@helderco
helderco merged commit 82c4e16 into dagger:main Feb 23, 2023
@helderco
helderco deleted the python/error-handling branch February 23, 2023 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants