import logging
import traceback


from flask_restplus import Api
from sqlalchemy.orm.exc import NoResultFound

log = logging.getLogger(__name__)

api_v1 = Api( # pylint: disable=invalid-name
    version='1.0',
    title="Flask-RESTplus Leave Demo",
    description=(
        "It is a [demo example RESTful API server implementation using Flask-RESTplus]"
        "(https://github.com/CockyAmoeba/flask-restplus-leave-demo).\n\n"
        "This demo features:\n"
        "* Self-documented RESTful API server using autogenerated OpenAPI specifications;\n"
        "* Basic Auth support;\n"
        "* Validations;\n"
        "* Custom response codes;\n"
        "## Explore the demo\n\n"
    ),
)


@api_v1.errorhandler
def default_error_handler(e):
    message = 'An unhandled exception occurred.'
    log.exception(message)

    return {'message': message}, 500


@api_v1.errorhandler(NoResultFound)
def database_not_found_error_handler(e):
    log.warning(traceback.format_exc())
    return {'message': 'A database result was required but none was found.'}, 404