from flask import request, abort as flask_abort, make_response
from werkzeug.exceptions import default_exceptions, HTTPException
import json
import logging

logger = logging.getLogger('mongrest')

class JSONHTTPException(HTTPException):
    """A base class for HTTP exceptions with ``Content-Type:
    application/json``.

    The ``description`` attribute of this class must set to a string (*not* an
    HTML string) which describes the error.

    """

    def get_body(self, environ):
        """Overrides :meth:`werkzeug.exceptions.HTTPException.get_body` to
        return the description of this error in JSON format instead of HTML.

        """
        return json.dumps(dict(description=self.get_description(environ)))

    def get_headers(self, environ):
        """Returns a list of headers including ``Content-Type:
        application/json``.

        """
        return [('Content-Type', 'application/json')]

    def get_description(self, environ=None):
        """Get the description."""
        return u'%s' % self.description


def abort(status_code, body=None, headers={}):
    """
    Content negiate the error response.
    """
    logger.debug('ABORT %s' % (status_code))

    if 'text/html' in request.headers.get("Accept", ""):
        error_cls = HTTPException
    else:
        error_cls = JSONHTTPException

    class_name = error_cls.__name__
    bases = [error_cls]
    attributes = {'code': status_code}

    if status_code in default_exceptions:
        # Mixin the Werkzeug exception
        bases.insert(0, default_exceptions[status_code])

    error_cls = type(class_name, tuple(bases), attributes)
    flask_abort(make_response(error_cls(body), status_code, headers))

from flask import request, abort as flask_abort, make_response
from werkzeug.exceptions import default_exceptions, HTTPException
import json
import logging

logger = logging.getLogger('mongrest')

class JSONHTTPException(HTTPException):
    """A base class for HTTP exceptions with ``Content-Type:
    application/json``.

    The ``description`` attribute of this class must set to a string (*not* an
    HTML string) which describes the error.

    """

    def get_body(self, environ):
        """Overrides :meth:`werkzeug.exceptions.HTTPException.get_body` to
        return the description of this error in JSON format instead of HTML.

        """
        return json.dumps(dict(description=self.get_description(environ)))

    def get_headers(self, environ):
        """Returns a list of headers including ``Content-Type:
        application/json``.

        """
        return [('Content-Type', 'application/json')]

    def get_description(self, environ=None):
        """Get the description."""
        return u'%s' % self.description


def abort(status_code, body=None, headers={}):
    """
    Content negiate the error response.
    """
    logger.debug('ABORT %s' % (status_code))

    if 'text/html' in request.headers.get("Accept", ""):
        error_cls = HTTPException
    else:
        error_cls = JSONHTTPException

    class_name = error_cls.__name__
    bases = [error_cls]
    attributes = {'code': status_code}

    if status_code in default_exceptions:
        # Mixin the Werkzeug exception
        bases.insert(0, default_exceptions[status_code])

    error_cls = type(class_name, tuple(bases), attributes)
    flask_abort(make_response(error_cls(body), status_code, headers))

A base class for HTTP exceptions with ``Content-Type:     application/json``.      The ``description`` attribute of this class must set to a string (*not* an     HTML string) which describes the error. Overrides :meth:`werkzeug.exceptions.HTTPException.get_body` to         return the description of this error in JSON format instead of HTML. Returns a list of headers including ``Content-Type:         application/json``. Get the description. Content negiate the error response. # Mixin the Werkzeug exception