Skip to content

Commit c823c37

Browse files
committed
custom errors instead of default errors
1 parent c7dca86 commit c823c37

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

api/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Copyright (c) 2019 - present AppSeed.us
44
"""
55

6+
import json
7+
68
from flask import Flask
79

810
from .routes import rest_api, jwt
@@ -15,3 +17,24 @@
1517
db.init_app(app)
1618
rest_api.init_app(app)
1719
jwt.init_app(app)
20+
21+
22+
"""
23+
Custom responses
24+
"""
25+
26+
@app.after_request
27+
def after_request(response):
28+
"""
29+
Sends back a custom error with {"success", "msg"} format
30+
"""
31+
32+
if int(response.status_code) >= 400:
33+
response_data = json.loads(response.get_data())
34+
if "errors" in response_data:
35+
_err_keys = response_data.keys()
36+
response_data = {"success": False,
37+
"msg": list(response_data["errors"].items())[0][1]}
38+
response.set_data(json.dumps(response_data))
39+
response.headers.add('Content-Type', 'application/json')
40+
return response

0 commit comments

Comments
 (0)