Drop extended ResolverError traceback

This commit is contained in:
Robbe Sneyders
2023-02-21 23:16:08 +01:00
parent 700b071c45
commit 69f117ce52
7 changed files with 23 additions and 34 deletions

View File

@@ -15,14 +15,7 @@ class ConnexionException(Exception):
class ResolverError(LookupError, ConnexionException):
def __init__(self, detail: str = "Unknown reason", *, exc_info: tuple) -> None:
"""
:param detail: Reason why the resolver failed.
:param exc_info: If specified, gives details of the original exception
as returned by sys.exc_info()
"""
self.reason = detail
self.exc_info = exc_info
pass
class InvalidSpecification(ValidationError, ConnexionException):

View File

@@ -26,7 +26,7 @@ class ResolverErrorHandler:
def handle(self, *args, **kwargs):
raise ResolverProblem(
detail=self.exception.reason,
detail=self.exception.args[0],
status=self.status_code,
)
@@ -54,6 +54,6 @@ class ResolverErrorHandler:
async def __call__(self, *args, **kwargs):
raise ResolverProblem(
detail=self.exception.reason,
detail=self.exception.args[0],
status=self.status_code,
)

View File

@@ -1,7 +1,6 @@
import abc
import logging
import pathlib
import sys
import typing as t
from starlette.types import ASGIApp, Receive, Scope, Send
@@ -106,10 +105,10 @@ class AbstractRoutingAPI(AbstractSpecAPI, t.Generic[OP]):
if self.resolver_error_handler is not None:
self._add_resolver_error_handler(method, path, err)
else:
self._handle_add_operation_error(path, method, err.exc_info)
except Exception:
self._handle_add_operation_error(path, method, err)
except Exception as e:
# All other relevant exceptions should be handled as well.
self._handle_add_operation_error(path, method, sys.exc_info())
self._handle_add_operation_error(path, method, e)
def add_operation(self, path: str, method: str) -> None:
"""
@@ -169,15 +168,12 @@ class AbstractRoutingAPI(AbstractSpecAPI, t.Generic[OP]):
self._add_operation_internal(method, path, operation)
def _handle_add_operation_error(
self, path: str, method: str, exc_info: tuple
self, path: str, method: str, exc: Exception
) -> None:
url = f"{self.base_path}{path}"
error_msg = "Failed to add operation for {method} {url}".format(
method=method.upper(), url=url
)
error_msg = f"Failed to add operation for {method.upper()} {url}"
logger.error(error_msg)
_type, value, traceback = exc_info
raise value.with_traceback(traceback)
raise exc from None
def json_loads(self, data):
return self.jsonifier.loads(data)

View File

@@ -41,7 +41,7 @@ class MockResolver(Resolver):
except ResolverError as resolution_error:
logger.debug(
"... {}! Mock function is used for this operation.".format(
resolution_error.reason.capitalize()
resolution_error.args[0].capitalize()
)
)
func = mock_func

View File

@@ -5,7 +5,6 @@ from the operations defined in the OpenAPI spec.
import inspect
import logging
import sys
import typing as t
from inflection import camelize
@@ -70,9 +69,9 @@ class Resolver:
return self.function_resolver(operation_id)
except ImportError as e:
msg = f'Cannot resolve operationId "{operation_id}"! Import error was "{str(e)}"'
raise ResolverError(msg, exc_info=sys.exc_info())
raise ResolverError(msg)
except (AttributeError, ValueError) as e:
raise ResolverError(str(e), exc_info=sys.exc_info())
raise ResolverError(str(e))
class RelativeResolver(Resolver):
@@ -268,9 +267,9 @@ class MethodResolverBase(RestyResolver):
msg = 'Cannot resolve operationId "{}"! Import error was "{}"'.format(
operation_id, str(e)
)
raise ResolverError(msg, exc_info=sys.exc_info())
raise ResolverError(msg)
except (AttributeError, ValueError) as e:
raise ResolverError(str(e), exc_info=sys.exc_info())
raise ResolverError(str(e))
def resolve_method_from_class(self, view_name, meth_name, view_cls):
"""

View File

@@ -5,7 +5,7 @@ from unittest.mock import MagicMock
import pytest
from connexion import FlaskApi
from connexion.exceptions import InvalidSpecification
from connexion.exceptions import InvalidSpecification, ResolverError
from connexion.spec import Specification, canonical_base_path
from yaml import YAMLError
@@ -60,28 +60,28 @@ def test_template():
def test_invalid_operation_does_stop_application_to_setup():
with pytest.raises(ImportError):
with pytest.raises(ResolverError):
FlaskApi(
TEST_FOLDER / "fixtures/op_error_api/swagger.yaml",
base_path="/api/v1.0",
arguments={"title": "OK"},
)
with pytest.raises(ValueError):
with pytest.raises(ResolverError):
FlaskApi(
TEST_FOLDER / "fixtures/missing_op_id/swagger.yaml",
base_path="/api/v1.0",
arguments={"title": "OK"},
)
with pytest.raises(ImportError):
with pytest.raises(ResolverError):
FlaskApi(
TEST_FOLDER / "fixtures/module_not_implemented/swagger.yaml",
base_path="/api/v1.0",
arguments={"title": "OK"},
)
with pytest.raises(ValueError):
with pytest.raises(ResolverError):
FlaskApi(
TEST_FOLDER / "fixtures/user_module_loading_error/swagger.yaml",
base_path="/api/v1.0",

View File

@@ -6,6 +6,7 @@ import importlib_metadata
import pytest
from click.testing import CliRunner
from connexion.cli import main
from connexion.exceptions import ResolverError
from conftest import FIXTURES_FOLDER
@@ -176,14 +177,14 @@ def test_run_unimplemented_operations_and_stub(mock_app_run):
runner = CliRunner()
spec_file = str(FIXTURES_FOLDER / "missing_implementation/swagger.yaml")
with pytest.raises(AttributeError):
with pytest.raises(ResolverError):
runner.invoke(main, ["run", spec_file], catch_exceptions=False)
# yet can be run with --stub option
result = runner.invoke(main, ["run", spec_file, "--stub"], catch_exceptions=False)
assert result.exit_code == 0
spec_file = str(FIXTURES_FOLDER / "module_does_not_exist/swagger.yaml")
with pytest.raises(ImportError):
with pytest.raises(ResolverError):
runner.invoke(main, ["run", spec_file], catch_exceptions=False)
# yet can be run with --stub option
result = runner.invoke(main, ["run", spec_file, "--stub"], catch_exceptions=False)
@@ -194,7 +195,7 @@ def test_run_unimplemented_operations_and_mock(mock_app_run):
runner = CliRunner()
spec_file = str(FIXTURES_FOLDER / "missing_implementation/swagger.yaml")
with pytest.raises(AttributeError):
with pytest.raises(ResolverError):
runner.invoke(main, ["run", spec_file], catch_exceptions=False)
# yet can be run with --mock option
result = runner.invoke(