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

Refactoring: exceptions #84

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 2 additions & 0 deletions clairvoyance/entities/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class EndpointError(Exception):
pass
16 changes: 8 additions & 8 deletions clairvoyance/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def get_path_from_root(
path_from_root: List[str] = []

if name not in self.types:
raise Exception(f'Type \'{name}\' not in schema!')
raise ValueError(f'Type \'{name}\' not in schema!')

roots = [
self._schema['queryType']['name'] if self._schema['queryType'] else '',
Expand All @@ -119,7 +119,7 @@ def get_path_from_root(
found = True
if not found:
log().debug('get_path_from_root: Ran an iteration with no matches found')
raise Exception(f'Could not find path from root to \'{initial_name}\' \nCurrent path: {path_from_root}')
raise ValueError(f'Could not find path from root to \'{initial_name}\' \nCurrent path: {path_from_root}')

# Prepend queryType or mutationType
path_from_root.insert(0, name)
Expand Down Expand Up @@ -158,7 +158,7 @@ def convert_path_to_document(
elif self._schema['subscriptionType'] and path[0] == self._schema['subscriptionType']['name']:
doc = f'subscription {{ {doc} }}'
else:
raise Exception('Unknown operation type')
raise ValueError('Unknown operation type')

return doc

Expand All @@ -173,7 +173,7 @@ def __init__(
non_null: bool = False,
) -> None:
if not is_list and non_null_item:
raise Exception('elements can\'t be NON_NULL if TypeRef is not LIST')
raise ValueError('Elements can\'t be NON_NULL if TypeRef is not LIST')

self.name = name
self.kind = kind
Expand Down Expand Up @@ -266,7 +266,7 @@ def field_or_arg_type_from_json(_json: Dict[str, Any]) -> 'TypeRef':
is_list=True,
)
else:
raise Exception(f'Unexpected type.kind: {_json["kind"]}')
raise ValueError(f'Unexpected type.kind: {_json["kind"]}')
elif not _json['ofType']['ofType']['ofType']:
actual_type = _json['ofType']['ofType']

Expand All @@ -286,7 +286,7 @@ def field_or_arg_type_from_json(_json: Dict[str, Any]) -> 'TypeRef':
non_null_item=True,
)
else:
raise Exception(f'Unexpected type.kind: {_json["kind"]}')
raise ValueError(f'Unexpected type.kind: {_json["kind"]}')
elif not _json['ofType']['ofType']['ofType']['ofType']:
actual_type = _json['ofType']['ofType']['ofType']
typ = TypeRef(
Expand All @@ -297,7 +297,7 @@ def field_or_arg_type_from_json(_json: Dict[str, Any]) -> 'TypeRef':
non_null=True,
)
else:
raise Exception('Invalid field or arg (too many \'ofType\')')
raise ValueError('Invalid field or arg (too many \'ofType\')')

return typ

Expand All @@ -310,7 +310,7 @@ def __init__(
args: List[InputValue] = None,
):
if not typeref:
raise Exception(f'Can\'t create {name} Field from {typeref} TypeRef.')
raise ValueError(f'Can\'t create {name} Field from {typeref} TypeRef.')

self.name = name
self.type = typeref
Expand Down
10 changes: 4 additions & 6 deletions clairvoyance/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import asyncio
import re
import sys
import time
from typing import Any, Dict, List, Optional, Set, Tuple

from clairvoyance import graphql
from clairvoyance.entities import GraphQLPrimitive
from clairvoyance.entities.errors import EndpointError
from clairvoyance.entities.context import client, config, log
from clairvoyance.entities.oracle import FuzzingContext
from clairvoyance.utils import track
Expand Down Expand Up @@ -421,11 +421,9 @@ async def __probation(document: str) -> Optional[graphql.TypeRef]:
typeref = result

if not typeref and context != FuzzingContext.ARGUMENT:
try:
raise Exception(f"""Unable to get TypeRef for {documents} in context {context}.
It is very likely that Field Suggestion is not fully enabled on this endpoint.""")
except Exception as e:
raise Exception(e) from e
error_message = f'Unable to get TypeRef for {documents} in context {context}. '
error_message += 'It is very likely that Field Suggestion is not fully enabled on this endpoint.'
raise EndpointError(error_message)

return typeref

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "Clairvoyance"
version = "2.5.2"
version = "2.5.3"
description = "Obtain GraphQL API Schema even if the introspection is not enabled"
authors = [
"Nikita Stupin <[email protected]>",
Expand Down
Loading