def METHOD_NAME(code, error, template):
    """
    Create an error handler that returns a JSON or a template response
    based on the request "Accept" header.
    :param code: status code to handle
    :param error: response error message, if JSON
    :param template: template response
    """
    def _handler_method(e):
        if request.accept_mimetypes.best == "application/json":
            return jsonify(error=error), code
        return render_template(template), code
    return _handler_method