# Path: eulfedora/util.py
# class RequestFailed(IOError):
#     '''An exception representing an arbitrary error while trying to access a
#     Fedora object or datastream.
#     '''
#     error_regex = re.compile('<pre>(.*\n.*)\n', re.MULTILINE)
# 
#     def __init__(self, response, content=None):
#         # init params:
#         #  response = HttpResponse with the error information
#         #  content = optional content of the response body, if it needed to be read
#         #            to determine what kind of exception to raise
#         super(RequestFailed, self).__init__('%d %s' % (response.status_code, response.text))
#         self.code = response.status_code
#         self.reason = response.text
#         if response.status_code == requests.codes.server_error:
#             # grab the response content if not passed in
#             if content is None:
#                 content = response.text
#             content = force_text(content)
#             # when Fedora gives a 500 error, it includes a stack-trace - pulling first line as detail
#             # NOTE: this is likely to break if and when Fedora error responses change
#             if 'content-type' in response.headers and response.headers['content-type'] == 'text/plain':
#                 # for plain text, first line of stack-trace is first line of text
#                 self.detail = content.split('\n')[0]
#             else:
#                 # for html, stack trace is wrapped with a <pre> tag; using regex to grab first line
#                 match = self.error_regex.findall(content)
#                 if len(match):
#                     self.detail = match[0]
# 
# class PermissionDenied(RequestFailed):
#     '''An exception representing a permission error while trying to access a
#     Fedora object or datastream.
#     '''


from django import template
from eulfedora.util import RequestFailed, PermissionDenied
"""Template tags utilities for communicating with a Fedora server

This module defines a single template tag, {% fedora_access %} (and matched
{% end_fedora_access %}), which further recognizes internal tags
{% permission_denied %} and {% fedora_failed %}. See Sphinx docs for
sample template code.
"""


register = template.Library()

class CatchFedoraErrorsNode(template.Node):
    """Template render node for catching fedora access errors and providing
    fallback content."""
    def __init__(self, fedora_access, permission_denied=None,
                 fedora_failed=None):
        self.fedora_access = fedora_access
        self.permission_denied = permission_denied
        self.fedora_failed = fedora_failed

    def render(self, context):
        try:
            return self.fedora_access.render(context)
        except PermissionDenied:
            if self.permission_denied is not None:
                return self.permission_denied.render(context)
            elif self.fedora_failed is not None:
                return self.fedora_failed.render(context)
            else:
                return ''

# Path: eulfedora/util.py
# class RequestFailed(IOError):
#     '''An exception representing an arbitrary error while trying to access a
#     Fedora object or datastream.
#     '''
#     error_regex = re.compile('<pre>(.*\n.*)\n', re.MULTILINE)
# 
#     def __init__(self, response, content=None):
#         # init params:
#         #  response = HttpResponse with the error information
#         #  content = optional content of the response body, if it needed to be read
#         #            to determine what kind of exception to raise
#         super(RequestFailed, self).__init__('%d %s' % (response.status_code, response.text))
#         self.code = response.status_code
#         self.reason = response.text
#         if response.status_code == requests.codes.server_error:
#             # grab the response content if not passed in
#             if content is None:
#                 content = response.text
#             content = force_text(content)
#             # when Fedora gives a 500 error, it includes a stack-trace - pulling first line as detail
#             # NOTE: this is likely to break if and when Fedora error responses change
#             if 'content-type' in response.headers and response.headers['content-type'] == 'text/plain':
#                 # for plain text, first line of stack-trace is first line of text
#                 self.detail = content.split('\n')[0]
#             else:
#                 # for html, stack trace is wrapped with a <pre> tag; using regex to grab first line
#                 match = self.error_regex.findall(content)
#                 if len(match):
#                     self.detail = match[0]
# 
# class PermissionDenied(RequestFailed):
#     '''An exception representing a permission error while trying to access a
#     Fedora object or datastream.
#     '''


# Path: eulfedora/templatetags/fedora.py
from django import template
from eulfedora.util import RequestFailed, PermissionDenied

"""Template tags utilities for communicating with a Fedora server

This module defines a single template tag, {% fedora_access %} (and matched
{% end_fedora_access %}), which further recognizes internal tags
{% permission_denied %} and {% fedora_failed %}. See Sphinx docs for
sample template code.
"""


register = template.Library()

class CatchFedoraErrorsNode(template.Node):
    """Template render node for catching fedora access errors and providing
    fallback content."""
    def __init__(self, fedora_access, permission_denied=None,
                 fedora_failed=None):
        self.fedora_access = fedora_access
        self.permission_denied = permission_denied
        self.fedora_failed = fedora_failed

    def render(self, context):
        try:
            return self.fedora_access.render(context)
        except PermissionDenied:
            if self.permission_denied is not None:
                return self.permission_denied.render(context)
            elif self.fedora_failed is not None:
                return self.fedora_failed.render(context)
            else:
                return ''

        except RequestFailed: