Skip to content

Commit

Permalink
Fix #259 - better error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
rhunwicks committed May 7, 2015
1 parent ae6dd48 commit 0b144b6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
20 changes: 19 additions & 1 deletion import_export/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.utils import six
from django.db.models.fields import FieldDoesNotExist
from django.db.models.query import QuerySet
from django.db.transaction import TransactionManagementError
from django.conf import settings

from .results import Error, Result, RowResult
Expand Down Expand Up @@ -45,6 +46,17 @@
except ImportError:
from django.utils.datastructures import SortedDict as OrderedDict

# Set default logging handler to avoid "No handler found" warnings.
import logging
try: # Python 2.7+
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, record):
pass

logging.getLogger(__name__).addHandler(NullHandler())

USE_TRANSACTIONS = getattr(settings, 'IMPORT_EXPORT_USE_TRANSACTIONS', False)


Expand Down Expand Up @@ -327,6 +339,7 @@ def import_data(self, dataset, dry_run=False, raise_errors=False,
try:
self.before_import(dataset, real_dry_run, **kwargs)
except Exception as e:
logging.exception(e)
tb_info = traceback.format_exc(2)
result.base_errors.append(Error(repr(e), tb_info))
if raise_errors:
Expand Down Expand Up @@ -369,8 +382,12 @@ def import_data(self, dataset, dry_run=False, raise_errors=False,
row_result.diff = self.get_diff(original, instance,
real_dry_run)
except Exception as e:
# There is no point logging a transaction error for each row
# when only the original error is likely to be relevant
if not isinstance(e, TransactionManagementError):
logging.exception(e)
tb_info = traceback.format_exc(2)
row_result.errors.append(Error(e, tb_info))
row_result.errors.append(Error(e, tb_info, row))
if raise_errors:
if use_transactions:
savepoint_rollback(sp1)
Expand Down Expand Up @@ -472,6 +489,7 @@ def __new__(cls, name, bases, attrs):
try:
f = model._meta.get_field_by_name(attr)[0]
except FieldDoesNotExist as e:
logging.exception(e)
raise FieldDoesNotExist("%s: %s has no field named '%s'" %
(verbose_path, model.__name__, attr))

Expand Down
3 changes: 2 additions & 1 deletion import_export/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

class Error(object):

def __init__(self, error, traceback=None):
def __init__(self, error, traceback=None, row=None):
self.error = error
self.traceback = traceback
self.row = row


class RowResult(object):
Expand Down
6 changes: 5 additions & 1 deletion import_export/templates/admin/import_export/import.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,16 @@ <h1>{% trans "Import" %}</h1>
<h2>{% trans "Errors" %}</h2>
<ul>
{% for error in result.base_errors %}
<li>{{ error.error }}</li>
<li>
{{ error.error }}
<div class="traceback">{{ error.traceback|linebreaks }}</div>
</li>
{% endfor %}
{% for line, errors in result.row_errors %}
{% for error in errors %}
<li>
{% trans "Line number" %}: {{ line }} - {{ error.error }}
<div>{{ error.row }}</div>
<div class="traceback">{{ error.traceback|linebreaks }}</div>
</li>
{% endfor %}
Expand Down
12 changes: 12 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,15 @@
'NAME': os.path.join(os.path.dirname(__file__), 'database.db'),
}
}

LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'handlers': {
'console': {
'class': 'logging.NullHandler'
}
},
'root': {
'handlers': ['console'],
}}

0 comments on commit 0b144b6

Please sign in to comment.