Skip to content

Commit 8f17937

Browse files
committed
added test + handle_exception function
1 parent dca962e commit 8f17937

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

app.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
import flask_cors
77
from werkzeug.exceptions import HTTPException
88

9-
from werkzeug.exceptions import HTTPException
10-
119
from src.opengeodeweb_back.routes import blueprint_routes
10+
from src.opengeodeweb_back.geode_functions import handle_exception
1211

1312

1413
""" Global config """
@@ -36,17 +35,19 @@
3635

3736

3837
@app.errorhandler(HTTPException)
39-
def handle_exception(e):
40-
response = e.get_response()
41-
response.data = flask.json.dumps(
42-
{
43-
"code": e.code,
44-
"name": e.name,
45-
"description": e.description,
46-
}
47-
)
48-
response.content_type = "application/json"
49-
return response
38+
def errorhandler(e):
39+
# print("tutu", e, flush=True)
40+
return handle_exception(e)
41+
42+
43+
@app.route(
44+
"/error",
45+
methods=["POST"],
46+
)
47+
def return_error():
48+
# print("return_error 123", flush=True)
49+
flask.abort(500, f"Test")
50+
# return flask.make_response({}, 500)
5051

5152

5253
# ''' Main '''

src/opengeodeweb_back/geode_functions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,16 @@ def send_file(upload_folder, saved_files, new_file_name):
354354
response.headers["Access-Control-Expose-Headers"] = "new-file-name"
355355

356356
return response
357+
358+
359+
def handle_exception(e):
360+
response = e.get_response()
361+
response.data = flask.json.dumps(
362+
{
363+
"code": e.code,
364+
"name": e.name,
365+
"description": e.description,
366+
}
367+
)
368+
response.content_type = "application/json"
369+
return response

src/opengeodeweb_back/routes/blueprint_routes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def allowed_files():
3131
extensions = geode_functions.list_input_extensions(
3232
flask.request.json["supported_feature"]
3333
)
34-
return {"status": 200, "extensions": extensions}
34+
return flask.make_response({"extensions": extensions}, 200)
3535

3636

3737
with open(

tests/test_functions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import os
22
import uuid
33
from src.opengeodeweb_back import geode_functions, geode_objects
4+
from werkzeug.exceptions import HTTPException
5+
from jsonschema import validate
6+
from jsonschema.exceptions import ValidationError
47

58

69
data_folder = os.path.join(os.path.dirname(__file__), "data")
@@ -329,3 +332,14 @@ def test_extension_from_filename():
329332
extension = geode_functions.extension_from_filename("test.toto")
330333
assert type(extension) is str
331334
assert extension.count(".") == 0
335+
336+
337+
def test_handle_exception(client):
338+
route = "/error"
339+
response = client.post(route)
340+
assert response.status_code == 500
341+
data = response.get_json()
342+
assert type(data) is dict
343+
assert type(data["description"]) is str
344+
assert type(data["name"]) is str
345+
assert type(data["code"]) is int

0 commit comments

Comments
 (0)