3
def create_app():
    """Application factory, used to create application
    """
    config = appconfig.AppConfig

    # Init connection for elastic search
    connections.create_connection(
        hosts=[config.ELASTIC_SEARCH_URI], timeout=20)

    app = Flask('Company API')
    app.config.from_object(config)
    app.debug = config.APP_DEBUG
    app.db = SQLAlchemy(app)
    app.migrate = Migrate(app, app.db, directory=config.MIGRATION_DIR)  # this
    app.mongo_db = connect(config.MONGO_DBNAME, host=config.MONGO_DATABASE_URI)

    # Swagger Doc Authorization
    authorizations = {
        "bearer token": {
            "type": "apiKey",
            "in": "header",
            "name": "authorization"
        }
    }

    # Add URL Int List Converter
    app.url_map.converters['list'] = ListConverter
    app.url_map.converters['int_list'] = IntListConverter

    app.api = Api(app, version='1', authorizations=authorizations)
    app.wsgi_app = ProxyFix(app.wsgi_app)

    return app


app = create_app()


@app.api.errorhandler(CompnayError)
def autods_error_handler(error):
    return {'errors': error.description}, error.code


@app.api.errorhandler(Exception)
def default_error_handler(error):
    code = error.code if hasattr(error, 'code') else 500
    return {'errors': "{}: {}".format(type(error).__name__, str(error))}, code

When running this code locally without gunicron when an exception happens I get a JSON response with the exception message. When running the same code with gunicorn I'm getting an HTML page instead of the JSON.

Does Gunicorn override the default error handler? Is there a way to override this behavior?

Michael Royf
  • 127
  • 6

0 Answers0