import traceback

from flask import Flask, jsonify

from api.version import version_api
from api.watchdog import watchdog_api
from api.route import route_api
from api.errors import TRFormattedError
from api.utils import jsonify_errors
from api.extensions import http_rpc


app = Flask(__name__)

app.url_map.strict_slashes = False
app.config.from_object('config.Config')

app.register_blueprint(route_api)
app.register_blueprint(version_api)
app.register_blueprint(watchdog_api)


@app.errorhandler(Exception)
def handle_error(exception):
    app.logger.error(traceback.format_exc())
    code = getattr(exception, 'code', 500)
    message = getattr(exception, 'description', 'Something went wrong.')
    reason = '.'.join([
        exception.__class__.__module__,
        exception.__class__.__name__,
    ])

    response = jsonify(code=code, message=message, reason=reason)
    return response, code


@app.errorhandler(TRFormattedError)
def handle_tr_formatted_error(exception):
    app.logger.error(traceback.format_exc())
    return jsonify_errors(exception.json)


if __name__ == '__main__':
    app.run()

import traceback

from flask import Flask, jsonify

from api.version import version_api
from api.watchdog import watchdog_api
from api.route import route_api
from api.errors import TRFormattedError
from api.utils import jsonify_errors
from api.extensions import http_rpc


app = Flask(__name__)

app.url_map.strict_slashes = False
app.config.from_object('config.Config')

app.register_blueprint(route_api)
app.register_blueprint(version_api)
app.register_blueprint(watchdog_api)


@app.errorhandler(Exception)
def handle_error(exception):
    app.logger.error(traceback.format_exc())
    code = getattr(exception, 'code', 500)
    message = getattr(exception, 'description', 'Something went wrong.')
    reason = '.'.join([
        exception.__class__.__module__,
        exception.__class__.__name__,
    ])

    response = jsonify(code=code, message=message, reason=reason)
    return response, code


@app.errorhandler(TRFormattedError)
def handle_tr_formatted_error(exception):
    app.logger.error(traceback.format_exc())
    return jsonify_errors(exception.json)


if __name__ == '__main__':
    app.run()
