Skip to content

Commit 1150c9b

Browse files
authored
Fix exceptions structure (#21)
* fix exceptions structure.
1 parent 0c24fa6 commit 1150c9b

File tree

4 files changed

+11
-49
lines changed

4 files changed

+11
-49
lines changed

src/flask_first/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@
2626
from .exceptions import FirstRequestPathArgsValidation
2727
from .exceptions import FirstResponseJSONValidation
2828
from .exceptions import FirstValidation
29-
from .exceptions import register_errors
3029
from .schema.tools import convert_schemas
3130
from .schema.tools import resolving_refs
3231

33-
__version__ = '0.12.1'
32+
__version__ = '0.13.0'
3433

3534

3635
class First:
@@ -291,7 +290,6 @@ def add_response_validating(response: Response) -> Response:
291290
def init_app(self, app: Flask) -> None:
292291
self.app = app
293292
self.app.config.setdefault('FIRST_RESPONSE_VALIDATION', False)
294-
register_errors(self.app)
295293
self.app.extensions['first'] = self
296294

297295
if self.swagger_ui_path:

src/flask_first/exceptions.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
"""The module contains the exceptions used in the Flask-First extension."""
2-
from flask import current_app
3-
from flask import Flask
4-
from flask import jsonify
5-
from flask import make_response
6-
from flask import Response
7-
8-
9-
def _bad_request(description: str) -> Response:
10-
return make_response(jsonify(code=400, name='Bad Request', description=description), 400)
112

123

134
class FirstException(Exception):
@@ -40,34 +31,3 @@ class FirstRequestJSONValidation(FirstValidation):
4031

4132
class FirstResponseJSONValidation(FirstValidation):
4233
"""Exception for response validation error."""
43-
44-
45-
def register_errors(app: Flask) -> None:
46-
"""Registering handlers for common errors."""
47-
48-
@app.errorhandler(FirstRequestPathArgsValidation)
49-
def path_args_validation_exception(exc) -> Response:
50-
current_app.logger.debug(exc)
51-
return _bad_request(str(exc))
52-
53-
@app.errorhandler(FirstRequestArgsValidation)
54-
def args_validation_exception(exc) -> Response:
55-
current_app.logger.debug(exc)
56-
return _bad_request(str(exc))
57-
58-
@app.errorhandler(FirstRequestCookieValidation)
59-
def cookie_validation_exception(exc) -> Response:
60-
current_app.logger.debug(exc)
61-
return _bad_request(str(exc))
62-
63-
@app.errorhandler(FirstRequestJSONValidation)
64-
def request_validation_exception(exc) -> Response:
65-
current_app.logger.debug(exc)
66-
return _bad_request(str(exc))
67-
68-
@app.errorhandler(FirstResponseJSONValidation)
69-
def response_validation_exception(exc) -> Response:
70-
current_app.logger.error(exc)
71-
return make_response(
72-
jsonify(code=500, name='Internal Server Error', description=str(exc)), 500
73-
)

tests/test_args.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from pathlib import Path
22

3+
import pytest
34
from flask import Flask
45
from flask import request
56
from flask_first import First
7+
from flask_first.exceptions import FirstRequestArgsValidation
68

79
from .conftest import BASEDIR
810

@@ -27,9 +29,9 @@ def create_app():
2729

2830
app = create_app()
2931

30-
with app.test_client() as test_client:
31-
r = test_client.get(
32+
with pytest.raises(FirstRequestArgsValidation) as e:
33+
app.test_client().get(
3234
'/parameters_endpoint',
3335
query_string={'non_exist_arg': 'NON_EXIST_ARGS'},
3436
)
35-
assert r.status_code == 400
37+
assert str(FirstRequestArgsValidation) in str(e.type)

tests/test_flask_first.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from flask import request
88
from flask import Response
99
from flask_first import First
10+
from flask_first.exceptions import FirstRequestPathArgsValidation
1011

1112
from .conftest import BASEDIR
1213

@@ -85,9 +86,10 @@ def get_item(uuid: str) -> Response:
8586

8687
fx_app.extensions['first'].add_view_func(get_item)
8788

88-
r_get = fx_client.get(f'/items/{path_param}')
89-
assert r_get.status_code == 400
90-
assert "{'uuid': ['Not a valid UUID.']}" in r_get.json['description']
89+
with pytest.raises(FirstRequestPathArgsValidation) as e:
90+
fx_client.get(f'/items/{path_param}')
91+
assert str(FirstRequestPathArgsValidation) in str(e.type)
92+
assert "{'uuid': ['Not a valid UUID.']}" in e.value.args[0]
9193

9294

9395
def test_specification__all_type_url_parameters(fx_app, fx_client):

0 commit comments

Comments
 (0)