import collections
import six


def get_details(exc):
    """Get the details from the exception."""
    detail = None
    fields = collections.OrderedDict()

    if exc.detail:
        detail = exc.detail

    if exc.fields:
        for k, v in six.iteritems(exc.fields):
            try:
                field_detail = v['detail']
            except (TypeError, KeyError):
                field_detail = v

            if isinstance(field_detail, (list, tuple)):
                field_detail = ' '.join(field_detail)

            if k == 'non_field_errors':
                if detail:
                    detail += ' ' + field_detail
                else:
                    detail = field_detail
                continue

            fields[k] = field_detail

    return detail, fields