# Path: apis/exception.py
# class Unauthorized(MetisException):
#     pass
# 
# class NotFound(MetisException):
#     pass
# 
# class BadRequest(MetisException):
#     pass
# 
# class Forbidden(MetisException):
#     pass
# 
# class RequestTimeout(MetisException):
#     pass
# 
# class ServerError(MetisException):
#     pass
# 
# Path: apis/custom_errors.py
# class UnprocessableEntity(JSONException):
#     pass
# 
# class ServerError(JSONException):
#     pass
# 
# class Forbidden(JSONException):
#     pass
# 
# Path: apis/settings.py
# class Config(LConfig):
#     pass


from sanic import Sanic
from sanic.response import json
from sanic.exceptions import NotFound as _NotFound, InvalidUsage
from apis.exception import (Unauthorized, NotFound, BadRequest,
                            Forbidden, RequestTimeout, ServerError)
from apis.custom_errors import (UnprocessableEntity,
                                ServerError as _ServerError,
                                Forbidden as _Forbidden)
from apis.settings import Config
    from apis.auth import bp as auth_bp
    from apis.v1 import bp as v1_bp
all_json_errors = [BadRequest, Unauthorized, Forbidden,
                   NotFound, RequestTimeout, ServerError]


@app.exception(*all_json_errors)
def json_error(request, exception):
    return json(
        {
            'error_code': exception.error_code,
            'message': exception.message,
            'text': exception.text
        },
        status=exception.status_code)


# swagger validators 需要用到
@app.exception(_NotFound)
def not_found(request, exception):
    return json({'error_code': 'not_found',
                 'message': exception.args[0]},
                status=exception.status_code,
                )


@app.exception(UnprocessableEntity, _Forbidden, _ServerError)
def custom_json_errors(request, exception):
    return json(
        {
            'error_code': exception.error_code,
            'message': exception.message,

# Path: apis/exception.py
# class Unauthorized(MetisException):
#     pass
# 
# class NotFound(MetisException):
#     pass
# 
# class BadRequest(MetisException):
#     pass
# 
# class Forbidden(MetisException):
#     pass
# 
# class RequestTimeout(MetisException):
#     pass
# 
# class ServerError(MetisException):
#     pass
# 
# Path: apis/custom_errors.py
# class UnprocessableEntity(JSONException):
#     pass
# 
# class ServerError(JSONException):
#     pass
# 
# class Forbidden(JSONException):
#     pass
# 
# Path: apis/settings.py
# class Config(LConfig):
#     pass


# Path: apis/app.py
from sanic import Sanic
from sanic.response import json
from sanic.exceptions import NotFound as _NotFound, InvalidUsage
from apis.exception import (Unauthorized, NotFound, BadRequest,
                            Forbidden, RequestTimeout, ServerError)
from apis.custom_errors import (UnprocessableEntity,
                                ServerError as _ServerError,
                                Forbidden as _Forbidden)
from apis.settings import Config
    from apis.auth import bp as auth_bp
    from apis.v1 import bp as v1_bp

all_json_errors = [BadRequest, Unauthorized, Forbidden,
                   NotFound, RequestTimeout, ServerError]


@app.exception(*all_json_errors)
def json_error(request, exception):
    return json(
        {
            'error_code': exception.error_code,
            'message': exception.message,
            'text': exception.text
        },
        status=exception.status_code)


# swagger validators 需要用到
@app.exception(_NotFound)
def not_found(request, exception):
    return json({'error_code': 'not_found',
                 'message': exception.args[0]},
                status=exception.status_code,
                )


@app.exception(UnprocessableEntity, _Forbidden, _ServerError)
def custom_json_errors(request, exception):
    return json(
        {
            'error_code': exception.error_code,
            'message': exception.message,

            'errors': exception.errors