Skip to content

Commit 87b3b14

Browse files
committed
feat: created exception_handler
1 parent 7f0723f commit 87b3b14

File tree

6 files changed

+37
-54
lines changed

6 files changed

+37
-54
lines changed

README.md

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,15 @@ $ pip3 install -r requirements.txt
2929
-----------------------------------
3030

3131
```
32-
$ python3 app.py
32+
$ python3 run.py
3333
```
3434

35-
5- Start redis-server (if is not active)
36-
-----------------------------------
37-
```
38-
$ redis-server --daemonize yes
39-
```
40-
41-
6- Start celery worker (if your application needs)
42-
-----------------------------------
43-
44-
```
45-
$ celery -A tasks app.celery --loglevel=INFO
46-
```
4735
<br />
4836

49-
## Check the default env variables (available on ``` config/variables.py ```)
37+
## Check some default env variables (available on ``` config/variables.py ```)
5038

5139
| Env Variable | Default Value |
5240
| ------------- | ------------- |
53-
| REDIS_URL | 'redis://localhost:6379/0' |
5441
| HOST | '0.0.0.0' |
5542
| PORT | '8080' |
5643
| DEBUG | 'True' |

server/app.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
11
from flask import Flask
2-
from flask_cors import CORS
3-
from celery import Celery
4-
from werkzeug.exceptions import HTTPException
5-
6-
from src.shared.utils.handle_exception import handle_exception
2+
from src.shared.utils.exception_handler import exception_handler
73
from src.modules.projects.project_blueprint import project_bp
84
from src.modules.users.user_blueprint import user_bp
95
from src.modules.tasks.task_blueprint import task_bp
10-
from config import variables
116

12-
app = Flask(__name__)
13-
CORS(app)
147

15-
app.register_blueprint(user_bp)
16-
app.register_blueprint(project_bp)
17-
app.register_blueprint(task_bp)
8+
def create_app() -> Flask:
189

19-
app.config['CELERY_BROKER_URL'] = variables.REDIS_URL
20-
app.config['CELERY_RESULT_BACKEND'] = variables.REDIS_URL
10+
app = Flask(__name__)
11+
db_conn = SQLConnector()
2112

22-
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
23-
celery.conf.update(app.config)
13+
db.init_app(app)
14+
with app.app_context():
15+
db.create_all()
2416

25-
@app.errorhandler(HTTPException)
26-
def get_error(error):
27-
return handle_exception(error)
17+
app.register_blueprint(user_bp)
18+
app.register_blueprint(project_bp)
19+
app.register_blueprint(task_bp)
2820

21+
@app.errorhandler(Exception)
22+
def get_error(error):
23+
return exception_handler(error)
2924

30-
if __name__ == '__main__':
31-
app.run(
32-
host=variables.HOST,
33-
port=variables.PORT,
34-
debug=bool(variables.DEBUG)
35-
)
25+
return app

server/config/jwt_secret.py

Whitespace-only changes.

server/run.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from app import create_app
2+
from config import variables
3+
4+
5+
if __name__ == '__main__':
6+
app = create_app()
7+
app.app_context().push()
8+
app.run(
9+
host=variables.HOST,
10+
port=int(variables.PORT),
11+
debug=bool(variables.DEBUG)
12+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import json
2+
from werkzeug.exceptions import HTTPException
3+
4+
5+
def exception_handler(error: Exception) -> json:
6+
code = 500
7+
if isinstance(error, HTTPException):
8+
code = error.code
9+
return json.dumps({'error': error.__repr__()}), code

server/src/shared/utils/handle_exception.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)