From 162aa71a154b336940c13f0232b60ee69b2dc010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Sat, 3 Nov 2018 18:11:52 +0100 Subject: [PATCH] Improve error handling on all routes --- app/api/blueprint.py | 16 ++++++++++++---- app/core.py | 4 ++-- requirements.txt | 1 - 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/api/blueprint.py b/app/api/blueprint.py index 25a543c..488e8ec 100644 --- a/app/api/blueprint.py +++ b/app/api/blueprint.py @@ -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 diff --git a/app/core.py b/app/core.py index 32002f2..bda72ff 100644 --- a/app/core.py +++ b/app/core.py @@ -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) diff --git a/requirements.txt b/requirements.txt index 0ccdb5a..171b2e1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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