-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.py
More file actions
55 lines (39 loc) · 1.55 KB
/
Copy patherrors.py
File metadata and controls
55 lines (39 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
## this file is for global error handling
import logging
from flask import jsonify
from werkzeug.exceptions import HTTPException
logger = logging.getLogger(__name__)
class BadRequestError(Exception):
"""error 400 - uncorrect request"""
class NotFoundError(Exception):
"""404 - not found"""
class ConflictError(Exception):
"""409 - Data conflict"""
class DatabaseError(Exception):
"""500 - DataBase error"""
def register_error_handlers(app):
@app.errorhandler(BadRequestError)
def handle_bad_request(e):
logger.warning("400 BadRequest: %s", e)
return jsonify({'error': str(e)}), 400
@app.errorhandler(NotFoundError)
def handle_not_found(e):
logger.warning("404 NotFound: %s", e)
return jsonify({'error': str(e)}), 404
@app.errorhandler(ConflictError)
def handle_conflict(e):
logger.warning("409 Conflict: %s", e)
return jsonify({'error': str(e)}), 409
@app.errorhandler(DatabaseError)
def handle_db_error(e):
logger.error("500 DatabaseError: %s", e, exc_info=True)
return jsonify({'error': 'DB error'}), 500
@app.errorhandler(HTTPException)
def handle_http_exception(e: HTTPException):
logger.warning("%s %s", e.code, e.description)
return jsonify({'error': e.description}), e.code
# every error that we didnt handle - consider as 500 error
@app.errorhandler(Exception)
def handle_unexpected(e):
logger.error("500 Unexpected: %s", e, exc_info=True)
return jsonify({'error': 'Internal server error'}), 500