<commit_before>
from rest_framework import status
from rest_framework.exceptions import APIException, ParseError


def json_api_exception_handler(exc, context):
    """ Custom exception handler that returns errors object as an array """

    # Import inside method to avoid errors when the OSF is loaded without Django
    from rest_framework.views import exception_handler
    response = exception_handler(exc, context)

    # Error objects may have the following members. Title and id removed to avoid clash with "title" and "id" field errors.
    top_level_error_keys = ['links', 'status', 'code', 'detail', 'source', 'meta']
    resource_object_identifiers = ['type', 'id']
    errors = []

    if response:
        message = response.data

        if isinstance(message, dict):
            for error_key, error_description in message.iteritems():
                if error_key in top_level_error_keys:
                    errors.append({error_key: error_description})
                elif error_key in resource_object_identifiers:
                    errors.append({'source': {'pointer': '/data/' + error_key}, 'detail': error_description})
                elif error_key == 'attributes':
                    for property, description in message[error_key].iteritems():
                        errors.append({'source': {'pointer': '/data/attributes/' + property}, 'detail': description})
# .
#                 else:
#                     if isinstance(error_description, basestring):
#                         error_description = [error_description]
#                     errors.extend([{'source': {'pointer': '/data/attributes/' + error_key}, 'detail': reason}
#                                    for reason in error_description])
        else:
            if isinstance(message, basestring):
                message = [message]
            errors.extend([{'detail': error} for error in message])

        response.data = {'errors': errors}

    return response


# Custom Exceptions the Django Rest Framework does not support
class Gone(APIException):
    status_code = status.HTTP_410_GONE
    default_detail = ('The requested resource is no longer available.')


class Conflict(APIException):
    status_code = status.HTTP_409_CONFLICT
    default_detail = ('Resource identifier does not match server endpoint.')<commit_msg>Modify exceptions to nest details properly if error_key is attributes<commit_after>
from rest_framework import status
from rest_framework.exceptions import APIException, ParseError


def json_api_exception_handler(exc, context):
    """ Custom exception handler that returns errors object as an array """

    # Import inside method to avoid errors when the OSF is loaded without Django
    from rest_framework.views import exception_handler
    response = exception_handler(exc, context)

    # Error objects may have the following members. Title and id removed to avoid clash with "title" and "id" field errors.
    top_level_error_keys = ['links', 'status', 'code', 'detail', 'source', 'meta']
    resource_object_identifiers = ['type', 'id']
    errors = []

    if response:
        message = response.data

        if isinstance(message, dict):
            for error_key, error_description in message.iteritems():
                if error_key in top_level_error_keys:
                    errors.append({error_key: error_description})
                elif error_key in resource_object_identifiers:
                     if isinstance(error_description, basestring):
                            error_description = [error_description]
                     errors.extend([{'source': {'pointer': '/data/' + error_key}, 'detail': reason} for reason in error_description])
                elif error_key == 'attributes':
                    for property, description in message[error_key].iteritems():
                        if isinstance(description, basestring):
                            description = [description]
                        errors.extend([{'source': {'pointer': '/data/attributes/' + property}, 'detail': reason} for reason in description])
        else:
            if isinstance(message, basestring):
                message = [message]
            errors.extend([{'detail': error} for error in message])

        response.data = {'errors': errors}

    return response


# Custom Exceptions the Django Rest Framework does not support
class Gone(APIException):
    status_code = status.HTTP_410_GONE
    default_detail = ('The requested resource is no longer available.')


class Conflict(APIException):
    status_code = status.HTTP_409_CONFLICT
    default_detail = ('Resource identifier does not match server endpoint.')
{'fraction_non_alphanumeric': 0.09104509510579184, 'fraction_numerical': 0.0025646505663603335, 'mean_word_length': 2.7231503579952268, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3181074', 'n_tokens_mistral': 1152, 'n_tokens_neox': 1092, 'n_words': 374}