Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_timer(monkeypatch):
wrapper = UWSGIMetricsCollector('/foo/bar/', 'get')
def operation(req):
raise ProblemException(418, '', '')
op = wrapper(operation)
metrics = MagicMock()
monkeypatch.setattr('flask.request', MagicMock())
monkeypatch.setattr('flask.current_app', MagicMock(response_class=flask.Response))
monkeypatch.setattr('connexion.decorators.metrics.uwsgi_metrics', metrics)
with pytest.raises(ProblemException) as exc:
op(MagicMock())
assert metrics.timer.call_args[0][:2] == ('connexion.response',
'418.GET.foo.bar.{param}')
def common_error_handler(exception):
"""
:type exception: Exception
"""
if isinstance(exception, ProblemException):
response = problem(
status=exception.status, title=exception.title, detail=exception.detail,
type=exception.type, instance=exception.instance, headers=exception.headers,
ext=exception.ext)
else:
if not isinstance(exception, werkzeug.exceptions.HTTPException):
exception = werkzeug.exceptions.InternalServerError()
response = problem(title=exception.name, detail=exception.description,
status=exception.code)
return FlaskApi.get_response(response)
"""
self.reason = reason
self.exc_info = exc_info
def __str__(self): # pragma: no cover
return ''.format(self.reason)
def __repr__(self): # pragma: no cover
return ''.format(self.reason)
class InvalidSpecification(ConnexionException, ValidationError):
pass
class NonConformingResponse(ProblemException):
def __init__(self, reason='Unknown Reason', message=None):
"""
:param reason: Reason why the response did not conform to the specification
:type reason: str
"""
super(NonConformingResponse, self).__init__(status=500, title=reason, detail=message)
self.reason = reason
self.message = message
def __str__(self): # pragma: no cover
return ''.format(self.reason)
def __repr__(self): # pragma: no cover
return ''.format(self.reason)
"""
:param reason: Reason why the response did not conform to the specification
:type reason: str
"""
super(NonConformingResponse, self).__init__(status=500, title=reason, detail=message)
self.reason = reason
self.message = message
def __str__(self): # pragma: no cover
return ''.format(self.reason)
def __repr__(self): # pragma: no cover
return ''.format(self.reason)
class AuthenticationProblem(ProblemException):
def __init__(self, status, title, detail):
super(AuthenticationProblem, self).__init__(status=status, title=title, detail=detail)
class ResolverProblem(ProblemException):
def __init__(self, status, title, detail):
super(ResolverProblem, self).__init__(status=status, title=title, detail=detail)
class BadRequestProblem(ProblemException):
def __init__(self, title='Bad Request', detail=None):
super(BadRequestProblem, self).__init__(status=400, title=title, detail=detail)
class OAuthResponseProblem(OAuthProblem):
def __init__(self, token_response, **kwargs):
self.token_response = token_response
super(OAuthResponseProblem, self).__init__(**kwargs)
class OAuthScopeProblem(Forbidden):
def __init__(self, token_scopes, required_scopes, **kwargs):
self.required_scopes = required_scopes
self.token_scopes = token_scopes
super(OAuthScopeProblem, self).__init__(**kwargs)
class ExtraParameterProblem(ProblemException):
def __init__(self, formdata_parameters, query_parameters, title=None, detail=None, **kwargs):
self.extra_formdata = formdata_parameters
self.extra_query = query_parameters
# This keep backwards compatibility with the old returns
if detail is None:
if self.extra_query:
detail = "Extra {parameter_type} parameter(s) {extra_params} not in spec"\
.format(parameter_type='query', extra_params=', '.join(self.extra_query))
elif self.extra_formdata:
detail = "Extra {parameter_type} parameter(s) {extra_params} not in spec"\
.format(parameter_type='formData', extra_params=', '.join(self.extra_formdata))
super(ExtraParameterProblem, self).__init__(title=title, detail=detail, **kwargs)
def set_errors_handlers(self):
for error_code in werkzeug.exceptions.default_exceptions:
# We don't want to log explicit HTTPExceptions as these are intentional.
self.app.register_error_handler(error_code, self._make_error_response)
# We don't catch ConnexionException specifically, because some other sub-classes handle
# other errors we might want to know about in a crash-report.
self.app.register_error_handler(ProblemException, self._make_error_response)
if not self.debug:
self.app.register_error_handler(Exception, self.log_error)
return ''.format(self.reason)
class AuthenticationProblem(ProblemException):
def __init__(self, status, title, detail):
super(AuthenticationProblem, self).__init__(status=status, title=title, detail=detail)
class ResolverProblem(ProblemException):
def __init__(self, status, title, detail):
super(ResolverProblem, self).__init__(status=status, title=title, detail=detail)
class BadRequestProblem(ProblemException):
def __init__(self, title='Bad Request', detail=None):
super(BadRequestProblem, self).__init__(status=400, title=title, detail=detail)
class UnsupportedMediaTypeProblem(ProblemException):
def __init__(self, title="Unsupported Media Type", detail=None):
super(UnsupportedMediaTypeProblem, self).__init__(status=415, title=title, detail=detail)
class NonConformingResponseBody(NonConformingResponse):
def __init__(self, message, reason="Response body does not conform to specification"):
super(NonConformingResponseBody, self).__init__(reason=reason, message=message)
def common_error_handler(exception):
"""
:type exception: Exception
"""
if isinstance(exception, ProblemException):
response = exception.to_problem()
else:
if not isinstance(exception, werkzeug.exceptions.HTTPException):
exception = werkzeug.exceptions.InternalServerError()
response = problem(title=exception.name, detail=exception.description,
status=exception.code)
return FalconApi.get_response(response)
def set_errors_handlers(self):
for error_code in werkzeug.exceptions.default_exceptions:
self.add_error_handler(error_code, self.common_error_handler)
self.add_error_handler(ProblemException, self.common_error_handler)
super(AuthenticationProblem, self).__init__(status=status, title=title, detail=detail)
class ResolverProblem(ProblemException):
def __init__(self, status, title, detail):
super(ResolverProblem, self).__init__(status=status, title=title, detail=detail)
class BadRequestProblem(ProblemException):
def __init__(self, title='Bad Request', detail=None):
super(BadRequestProblem, self).__init__(status=400, title=title, detail=detail)
class UnsupportedMediaTypeProblem(ProblemException):
def __init__(self, title="Unsupported Media Type", detail=None):
super(UnsupportedMediaTypeProblem, self).__init__(status=415, title=title, detail=detail)
class NonConformingResponseBody(NonConformingResponse):
def __init__(self, message, reason="Response body does not conform to specification"):
super(NonConformingResponseBody, self).__init__(reason=reason, message=message)
class NonConformingResponseHeaders(NonConformingResponse):
def __init__(self, message, reason="Response headers do not conform to specification"):
super(NonConformingResponseHeaders, self).__init__(reason=reason, message=message)
class OAuthProblem(Unauthorized):