Improve error handling on all routes

develop
Ensar Sarajčić 2018-11-03 18:11:52 +01:00
parent b34a8f21ce
commit 162aa71a15
3 changed files with 14 additions and 7 deletions

View File

@ -1,9 +1,11 @@
from flask_restful import Api
from marshmallow import ValidationError
from flask import Blueprint
from flask import Blueprint, jsonify
api_bp = Blueprint('api', __name__)
api = Api(api_bp)
@ -79,12 +81,18 @@ add_resources()
@api_bp.errorhandler(ValidationError)
@api_bp.errorhandler(422)
def handle_validation_error(e):
return {'status': 'error', 'message': str(e)}, 422
return jsonify({'status': 'error', 'message': str(e)}), 422
@api_bp.errorhandler(ValueError)
def handle_value_error(e):
return jsonify({'status': 'error', 'message': str(e)}), 422
@api_bp.errorhandler(Exception)
@api_bp.errorhandler(500)
def handle_unknown_errors(e):
return ({
return jsonify({
'status': 'failed',
'message': 'Unknown error has occurred! ({0})'.format(str(e))
}, 500)
}), 500

View File

@ -1,5 +1,5 @@
# App initialization
from flask_api import FlaskAPI
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_mail import Mail
@ -7,7 +7,7 @@ from flasgger import Swagger
from flask_cors import CORS
from .tasks import celery_configurator
app = FlaskAPI(__name__, instance_relative_config=True)
app = Flask(__name__, instance_relative_config=True)
app.config.from_object('config')
app.config.from_pyfile('config.py', silent=True)
db = SQLAlchemy(app)

View File

@ -10,7 +10,6 @@ cffi==1.11.5
click==6.7
flasgger==0.8.3
Flask==1.0.2
Flask-API==1.0
Flask-Bcrypt==0.7.1
Flask-Cors==3.0.4
Flask-Mail==0.9.1