| 1 |
from typing import cast, List, Union |
| 2 |
|
| 3 |
from pytest import raises |
| 4 |
|
| 5 |
from graphql.error import GraphQLError |
| 6 |
from graphql.language import ( |
| 7 |
parse, |
| 8 |
Node, |
| 9 |
OperationDefinitionNode, |
| 10 |
ObjectTypeDefinitionNode, |
| 11 |
Source, |
| 12 |
) |
| 13 |
|
| 14 |
from ..utils import dedent |
| 15 |
|
| 16 |
source = Source(dedent(""" |
| 17 |
{ |
| 18 |
field |
| 19 |
} |
| 20 |
""")) |
| 21 |
|
| 22 |
ast = parse(source) |
| 23 |
operation_node = ast.definitions[0] |
| 24 |
operation_node = cast(OperationDefinitionNode, operation_node) |
| 25 |
assert operation_node and operation_node.kind == "operation_definition" |
| 26 |
field_node = operation_node.selection_set.selections[0] |
| 27 |
assert field_node |
| 28 |
|
| 29 |
|
| 30 |
def describe_graphql_error(): |
| 31 |
def is_a_class_and_is_a_subclass_of_exception(): |
| 32 |
assert type(GraphQLError) is type |
| 33 |
assert issubclass(GraphQLError, Exception) |
| 34 |
assert isinstance(GraphQLError("str"), Exception) |
| 35 |
assert isinstance(GraphQLError("str"), GraphQLError) |
| 36 |
|
| 37 |
def has_a_name_message_extensions_and_stack_trace(): |
| 38 |
e = GraphQLError("msg") |
| 39 |
assert e.__class__.__name__ == "GraphQLError" |
| 40 |
assert e.message == "msg" |
| 41 |
assert e.extensions == {} |
| 42 |
assert e.__traceback__ is None |
| 43 |
assert str(e) == "msg" |
| 44 |
|
| 45 |
def can_pass_positional_and_keyword_arguments(): |
| 46 |
e1 = GraphQLError( |
| 47 |
"msg", |
| 48 |
[field_node], |
| 49 |
source, |
| 50 |
[1, 2, 3], |
| 51 |
["a", "b", "c"], |
| 52 |
Exception("test"), |
| 53 |
{"foo": "bar"}, |
| 54 |
) |
| 55 |
e2 = GraphQLError( |
| 56 |
message="msg", |
| 57 |
nodes=[field_node], |
| 58 |
source=source, |
| 59 |
positions=[1, 2, 3], |
| 60 |
path=["a", "b", "c"], |
| 61 |
original_error=Exception("test"), |
| 62 |
extensions={"foo": "bar"}, |
| 63 |
) |
| 64 |
assert e1 == e2 |
| 65 |
|
| 66 |
def formatted_dict_has_only_keys_prescribed_in_the_spec(): |
| 67 |
e = GraphQLError( |
| 68 |
"msg", |
| 69 |
[field_node], |
| 70 |
source, |
| 71 |
[1, 2, 3], |
| 72 |
["a", "b", "c"], |
| 73 |
Exception("test"), |
| 74 |
{"foo": "bar"}, |
| 75 |
) |
| 76 |
assert set(e.formatted) == {"message", "path", "locations", "extensions"} |
| 77 |
|
| 78 |
def uses_the_stack_of_an_original_error(): |
| 79 |
try: |
| 80 |
raise RuntimeError("original") |
| 81 |
except RuntimeError as runtime_error: |
| 82 |
original = runtime_error |
| 83 |
e = GraphQLError("msg", original_error=original) |
| 84 |
assert e.__class__.__name__ == "GraphQLError" |
| 85 |
assert e.__traceback__ is original.__traceback__ |
| 86 |
assert e.message == "msg" |
| 87 |
assert e.original_error is original |
| 88 |
assert str(e.original_error) == "original" |
| 89 |
|
| 90 |
def passes_the_context_of_an_original_error(): |
| 91 |
context = ValueError("cause") |
| 92 |
try: |
| 93 |
raise context |
| 94 |
except ValueError: |
| 95 |
try: |
| 96 |
raise RuntimeError("effect") |
| 97 |
except RuntimeError as runtime_error: |
| 98 |
original = runtime_error |
| 99 |
e = GraphQLError("msg", original_error=original) |
| 100 |
assert e.__context__ is context |
| 101 |
|
| 102 |
def passes_the_cause_of_an_original_error(): |
| 103 |
cause = ValueError("cause") |
| 104 |
try: |
| 105 |
raise RuntimeError("effect") from cause |
| 106 |
except RuntimeError as runtime_error: |
| 107 |
original = runtime_error |
| 108 |
e = GraphQLError("msg", original_error=original) |
| 109 |
assert e.__cause__ is cause |
| 110 |
|
| 111 |
def creates_new_stack_if_original_error_has_no_stack(): |
| 112 |
try: |
| 113 |
raise RuntimeError |
| 114 |
except RuntimeError as original_with_traceback: |
| 115 |
original_traceback = original_with_traceback.__traceback__ |
| 116 |
original = RuntimeError("original") |
| 117 |
e = GraphQLError("msg", original_error=original) |
| 118 |
assert e.__class__.__name__ == "GraphQLError" |
| 119 |
assert original.__traceback__ is None |
| 120 |
assert original_traceback is not None |
| 121 |
assert e.__traceback__ is original_traceback |
| 122 |
assert e.message == "msg" |
| 123 |
assert e.original_error is original |
| 124 |
assert str(e.original_error) == "original" |
| 125 |
|
| 126 |
def converts_nodes_to_positions_and_locations(): |
| 127 |
e = GraphQLError("msg", [field_node]) |
| 128 |
assert e.nodes == [field_node] |
| 129 |
assert e.source is source |
| 130 |
assert e.positions == [4] |
| 131 |
assert e.locations == [(2, 3)] |
| 132 |
|
| 133 |
def converts_single_node_to_positions_and_locations(): |
| 134 |
e = GraphQLError("msg", field_node) # Non-array value. |
| 135 |
assert e.nodes == [field_node] |
| 136 |
assert e.source is source |
| 137 |
assert e.positions == [4] |
| 138 |
assert e.locations == [(2, 3)] |
| 139 |
|
| 140 |
def converts_node_with_loc_start_zero_to_positions_and_locations(): |
| 141 |
e = GraphQLError("msg", operation_node) |
| 142 |
assert e.nodes == [operation_node] |
| 143 |
assert e.source is source |
| 144 |
assert e.positions == [0] |
| 145 |
assert e.locations == [(1, 1)] |
| 146 |
|
| 147 |
def converts_node_without_location_to_source_positions_and_locations_as_none(): |
| 148 |
document_node = parse("{ foo }", no_location=True) |
| 149 |
|
| 150 |
e = GraphQLError("msg", document_node) |
| 151 |
assert e.nodes == [document_node] |
| 152 |
assert e.source is None |
| 153 |
assert e.positions is None |
| 154 |
assert e.locations is None |
| 155 |
|
| 156 |
def converts_source_and_positions_to_locations(): |
| 157 |
e = GraphQLError("msg", None, source, [6]) |
| 158 |
assert e.nodes is None |
| 159 |
assert e.source is source |
| 160 |
assert e.positions == [6] |
| 161 |
assert e.locations == [(2, 5)] |
| 162 |
|
| 163 |
def defaults_to_original_error_extension_only_if_arg_is_not_passed(): |
| 164 |
original_extensions = {"original": "extensions"} |
| 165 |
original_error = GraphQLError("original", extensions=original_extensions) |
| 166 |
inherited_error = GraphQLError("InheritedError", original_error=original_error) |
| 167 |
assert inherited_error.message == "InheritedError" |
| 168 |
assert inherited_error.original_error is original_error |
| 169 |
assert inherited_error.extensions is original_extensions |
| 170 |
|
| 171 |
own_extensions = {"own": "extensions"} |
| 172 |
own_error = GraphQLError( |
| 173 |
"OwnError", original_error=original_error, extensions=own_extensions |
| 174 |
) |
| 175 |
assert own_error.message == "OwnError" |
| 176 |
assert own_error.original_error is original_error |
| 177 |
assert own_error.extensions is own_extensions |
| 178 |
|
| 179 |
own_empty_error = GraphQLError( |
| 180 |
"OwnEmptyError", original_error=original_error, extensions={} |
| 181 |
) |
| 182 |
assert own_empty_error.message == "OwnEmptyError" |
| 183 |
assert own_empty_error.original_error is original_error |
| 184 |
assert own_empty_error.extensions == {} |
| 185 |
|
| 186 |
def serializes_to_include_message(): |
| 187 |
e = GraphQLError("msg") |
| 188 |
assert str(e) == "msg" |
| 189 |
assert repr(e) == "GraphQLError('msg')" |
| 190 |
|
| 191 |
def serializes_to_include_message_and_locations(): |
| 192 |
e = GraphQLError("msg", field_node) |
| 193 |
assert "msg" in str(e) |
| 194 |
assert ":2:3" in str(e) |
| 195 |
assert repr(e) == ( |
| 196 |
"GraphQLError('msg', locations=[SourceLocation(line=2, column=3)])" |
| 197 |
) |
| 198 |
assert e.formatted == { |
| 199 |
"locations": [{"column": 3, "line": 2}], |
| 200 |
"message": "msg", |
| 201 |
} |
| 202 |
|
| 203 |
def serializes_to_include_path(): |
| 204 |
path: List[Union[int, str]] = ["path", 3, "to", "field"] |
| 205 |
e = GraphQLError("msg", path=path) |
| 206 |
assert e.path is path |
| 207 |
assert repr(e) == "GraphQLError('msg', path=['path', 3, 'to', 'field'])" |
| 208 |
assert e.formatted == { |
| 209 |
"message": "msg", |
| 210 |
"path": ["path", 3, "to", "field"], |
| 211 |
} |
| 212 |
|
| 213 |
def serializes_to_include_all_standard_fields(): |
| 214 |
e_short = GraphQLError("msg") |
| 215 |
assert str(e_short) == "msg" |
| 216 |
assert repr(e_short) == "GraphQLError('msg')" |
| 217 |
|
| 218 |
path: List[Union[str, int]] = ["path", 2, "field"] |
| 219 |
extensions = {"foo": "bar "} |
| 220 |
e_full = GraphQLError("msg", field_node, None, None, path, None, extensions) |
| 221 |
assert str(e_full) == ( |
| 222 |
"msg\n\nGraphQL request:2:3\n" "1 | {\n2 | field\n | ^\n3 | }" |
| 223 |
) |
| 224 |
assert repr(e_full) == ( |
| 225 |
"GraphQLError('msg', locations=[SourceLocation(line=2, column=3)]," |
| 226 |
" path=['path', 2, 'field'], extensions={'foo': 'bar '})" |
| 227 |
) |
| 228 |
assert e_full.formatted == { |
| 229 |
"message": "msg", |
| 230 |
"locations": [{"line": 2, "column": 3}], |
| 231 |
"path": ["path", 2, "field"], |
| 232 |
"extensions": {"foo": "bar "}, |
| 233 |
} |
| 234 |
|
| 235 |
def repr_includes_extensions(): |
| 236 |
e = GraphQLError("msg", extensions={"foo": "bar"}) |
| 237 |
assert repr(e) == "GraphQLError('msg', extensions={'foo': 'bar'})" |
| 238 |
|
| 239 |
def always_stores_path_as_list(): |
| 240 |
path: List[Union[int, str]] = ["path", 3, "to", "field"] |
| 241 |
e = GraphQLError("msg,", path=tuple(path)) |
| 242 |
assert isinstance(e.path, list) |
| 243 |
assert e.path == path |
| 244 |
|
| 245 |
def is_comparable(): |
| 246 |
e1 = GraphQLError("msg,", path=["field", 1]) |
| 247 |
assert e1 == e1 |
| 248 |
assert e1 == e1.formatted |
| 249 |
assert not e1 != e1 |
| 250 |
assert not e1 != e1.formatted |
| 251 |
e2 = GraphQLError("msg,", path=["field", 1]) |
| 252 |
assert e1 == e2 |
| 253 |
assert not e1 != e2 |
| 254 |
assert e2.path and e2.path[1] == 1 |
| 255 |
e2.path[1] = 2 |
| 256 |
assert not e1 == e2 |
| 257 |
assert e1 != e2 |
| 258 |
assert not e1 == e2.formatted |
| 259 |
assert e1 != e2.formatted |
| 260 |
|
| 261 |
def is_hashable(): |
| 262 |
hash(GraphQLError("msg")) |
| 263 |
|
| 264 |
def hashes_are_unique_per_instance(): |
| 265 |
e1 = GraphQLError("msg") |
| 266 |
e2 = GraphQLError("msg") |
| 267 |
assert hash(e1) != hash(e2) |
| 268 |
|
| 269 |
|
| 270 |
def describe_to_string(): |
| 271 |
def deprecated_prints_an_error_using_print_error(): |
| 272 |
# noinspection PyProtectedMember |
| 273 |
from graphql.error.graphql_error import print_error |
| 274 |
|
| 275 |
error = GraphQLError("Error") |
| 276 |
assert print_error(error) == "Error" |
| 277 |
with raises(TypeError) as exc_info: |
| 278 |
# noinspection PyTypeChecker |
| 279 |
print_error(Exception) # type: ignore |
| 280 |
assert str(exc_info.value) == "Expected a GraphQLError." |
| 281 |
|
| 282 |
def prints_an_error_without_location(): |
| 283 |
error = GraphQLError("Error without location") |
| 284 |
assert str(error) == "Error without location" |
| 285 |
|
| 286 |
def prints_an_error_using_node_without_location(): |
| 287 |
error = GraphQLError( |
| 288 |
"Error attached to node without location", |
| 289 |
parse("{ foo }", no_location=True), |
| 290 |
) |
| 291 |
assert str(error) == "Error attached to node without location" |
| 292 |
|
| 293 |
def prints_an_error_with_nodes_from_different_sources(): |
| 294 |
doc_a = parse( |
| 295 |
Source( |
| 296 |
dedent(""" |
| 297 |
type Foo { |
| 298 |
field: String |
| 299 |
} |
| 300 |
"""), |
| 301 |
"SourceA", |
| 302 |
) |
| 303 |
) |
| 304 |
op_a = doc_a.definitions[0] |
| 305 |
op_a = cast(ObjectTypeDefinitionNode, op_a) |
| 306 |
assert op_a and op_a.kind == "object_type_definition" and op_a.fields |
| 307 |
field_a = op_a.fields[0] |
| 308 |
doc_b = parse( |
| 309 |
Source( |
| 310 |
dedent(""" |
| 311 |
type Foo { |
| 312 |
field: Int |
| 313 |
} |
| 314 |
"""), |
| 315 |
"SourceB", |
| 316 |
) |
| 317 |
) |
| 318 |
op_b = doc_b.definitions[0] |
| 319 |
op_b = cast(ObjectTypeDefinitionNode, op_b) |
| 320 |
assert op_b and op_b.kind == "object_type_definition" and op_b.fields |
| 321 |
field_b = op_b.fields[0] |
| 322 |
|
| 323 |
error = GraphQLError( |
| 324 |
"Example error with two nodes", [field_a.type, field_b.type] |
| 325 |
) |
| 326 |
|
| 327 |
assert str(error) == dedent(""" |
| 328 |
Example error with two nodes |
| 329 |
|
| 330 |
SourceA:2:10 |
| 331 |
1 | type Foo { |
| 332 |
2 | field: String |
| 333 |
| ^ |
| 334 |
3 | } |
| 335 |
|
| 336 |
SourceB:2:10 |
| 337 |
1 | type Foo { |
| 338 |
2 | field: Int |
| 339 |
| ^ |
| 340 |
3 | } |
| 341 |
""") |
| 342 |
|
| 343 |
|
| 344 |
def describe_formatted(): |
| 345 |
def deprecated_formats_an_error_using_format_error(): |
| 346 |
# noinspection PyProtectedMember |
| 347 |
from graphql.error.graphql_error import format_error |
| 348 |
|
| 349 |
error = GraphQLError("Example Error") |
| 350 |
assert format_error(error) == { |
| 351 |
"message": "Example Error", |
| 352 |
} |
| 353 |
with raises(TypeError) as exc_info: |
| 354 |
# noinspection PyTypeChecker |
| 355 |
format_error(Exception) # type: ignore |
| 356 |
assert str(exc_info.value) == "Expected a GraphQLError." |
| 357 |
|
| 358 |
def formats_graphql_error(): |
| 359 |
path: List[Union[int, str]] = ["one", 2] |
| 360 |
extensions = {"ext": None} |
| 361 |
error = GraphQLError( |
| 362 |
"test message", |
| 363 |
Node(), |
| 364 |
Source(""" |
| 365 |
query { |
| 366 |
something |
| 367 |
} |
| 368 |
"""), |
| 369 |
[16, 41], |
| 370 |
["one", 2], |
| 371 |
ValueError("original"), |
| 372 |
extensions=extensions, |
| 373 |
) |
| 374 |
assert error.formatted == { |
| 375 |
"message": "test message", |
| 376 |
"locations": [{"line": 2, "column": 16}, {"line": 3, "column": 17}], |
| 377 |
"path": path, |
| 378 |
"extensions": extensions, |
| 379 |
} |
| 380 |
|
| 381 |
def uses_default_message(): |
| 382 |
# noinspection PyTypeChecker |
| 383 |
formatted = GraphQLError(None).formatted # type: ignore |
| 384 |
|
| 385 |
assert formatted == { |
| 386 |
"message": "An unknown error occurred.", |
| 387 |
} |
| 388 |
|
| 389 |
def includes_path(): |
| 390 |
path: List[Union[int, str]] = ["path", 3, "to", "field"] |
| 391 |
error = GraphQLError("msg", path=path) |
| 392 |
assert error.formatted == {"message": "msg", "path": path} |
| 393 |
|
| 394 |
def includes_extension_fields(): |
| 395 |
error = GraphQLError("msg", extensions={"foo": "bar"}) |
| 396 |
assert error.formatted == { |
| 397 |
"message": "msg", |
| 398 |
"extensions": {"foo": "bar"}, |
| 399 |
} |
| 400 |
|
| 401 |
def can_be_created_from_dict(): |
| 402 |
args = dict( |
| 403 |
nodes=[operation_node], |
| 404 |
source=source, |
| 405 |
positions=[6], |
| 406 |
path=["path", 2, "a"], |
| 407 |
original_error=Exception("I like turtles"), |
| 408 |
extensions=dict(hee="I like turtles"), |
| 409 |
) |
| 410 |
error = GraphQLError("msg", **args) # type: ignore |
| 411 |
assert error.formatted == { |
| 412 |
"message": "msg", |
| 413 |
"locations": [{"column": 5, "line": 2}], |
| 414 |
"path": ["path", 2, "a"], |
| 415 |
"extensions": {"hee": "I like turtles"}, |
| 416 |
} |