Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions hed/errors/error_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,9 @@ def format_error_from_context(error_type, error_context, *args, actual_error=Non
if not error_func:
error_object = ErrorHandler.val_error_unknown(*args, **kwargs)
error_object['code'] = error_type
ErrorHandler._add_context_to_errors(error_object, error_context)
return [error_object]
else:
error_object = error_func(*args, **kwargs)

error_object = error_func(*args, **kwargs)
if actual_error:
error_object['code'] = actual_error

Expand All @@ -321,8 +320,6 @@ def _add_context_to_errors(error_object, error_context_to_add):
list: A list of dict with needed context strings added at the beginning of the list.

"""
if error_object is None:
error_object = {}
for (context_type, context) in error_context_to_add:
error_object[context_type] = context

Expand All @@ -345,11 +342,7 @@ def _get_tag_span_to_error_object(error_object):
if ErrorContext.HED_STRING not in error_object:
return None, None

if 'char_index' in error_object:
char_index = error_object['char_index']
char_index_end = error_object.get('char_index_end', char_index + 1)
return char_index, char_index_end
elif 'source_tag' in error_object:
if 'source_tag' in error_object:
source_tag = error_object['source_tag']
if isinstance(source_tag, int):
return None, None
Expand All @@ -364,7 +357,9 @@ def _get_tag_span_to_error_object(error_object):
def _update_error_with_char_pos(error_object):
# This part is optional as you can always generate these as needed.
start, end = ErrorHandler._get_tag_span_to_error_object(error_object)
if start is not None and end is not None:
if start is not None:
# silence warning in pycharm
start = int(start)
source_tag = error_object.get('source_tag', None)
# Todo: Move this functionality somewhere more centralized.
# If the tag has been modified from the original, don't try to use sub indexing.
Expand Down
38 changes: 38 additions & 0 deletions tests/errors/test_error_reporter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from hed.errors import ErrorHandler, ErrorContext, ErrorSeverity, ValidationErrors, SchemaWarnings, \
get_printable_issue_string, sort_issues, replace_tag_references
from hed.errors.error_reporter import hed_tag_error, get_printable_issue_string_html
from hed import HedString
from hed import load_schema_version

Expand Down Expand Up @@ -33,6 +34,9 @@ def test_push_error_context(self):
self.assertTrue(column_name == error_list[0][ErrorContext.COLUMN])
self.assertTrue(len(error_list) == 1)
self.error_handler.reset_error_context()
self.error_handler.push_error_context(ErrorContext.ROW, None)
self.assertTrue(self.error_handler.error_context[0][1] == 0)
self.error_handler.reset_error_context()

def test_pop_error_context(self):
error_list = self.error_handler.format_error_with_context(ValidationErrors.TAG_NOT_UNIQUE, "")
Expand Down Expand Up @@ -115,6 +119,18 @@ def test_printable_issue_string_with_filenames(self):
self.assertTrue(len(printable_issues3) > len(printable_issues2))
self.assertEqual(printable_issues3.count(myfile), 1)

printable_issues = get_printable_issue_string_html(error_list, skip_filename=False)
self.assertTrue(len(printable_issues) > 10)
self.assertEqual(printable_issues.count(myfile), 1)

printable_issues2 = get_printable_issue_string_html(error_list, severity=ErrorSeverity.ERROR, skip_filename=False)
self.assertTrue(len(printable_issues) > len(printable_issues2))
self.assertEqual(printable_issues2.count(myfile), 1)
printable_issues3 = get_printable_issue_string_html(error_list, severity=ErrorSeverity.ERROR, skip_filename=False,
title="Later added custom title that is longer")
self.assertTrue(len(printable_issues3) > len(printable_issues2))
self.assertEqual(printable_issues3.count(myfile), 1)

self.error_handler.reset_error_context()

def test_sort_issues(self):
Expand Down Expand Up @@ -160,3 +176,25 @@ def test_replace_tag_references(self):
mixed = {'a': HedString('Hed1', self._schema), 'b': [2, 3, {'c': HedString('Hed2', self._schema)}, 4]}
replace_tag_references(mixed)
self.assertEqual(mixed, {'a': 'Hed1', 'b': [2, 3, {'c': 'Hed2'}, 4]})


def test_register_error_twice(self):
test_code = "test_error_code"
@hed_tag_error(test_code)
def test_error_code(tag):
pass

with self.assertRaises(KeyError):
@hed_tag_error(test_code)
def test_error_code(tag):
pass

def test_format_unknown_error(self):
error_code = "Unknown error type"
error_list = self.error_handler.format_error(error_code, "param1", param2=0)
self.assertEqual(error_list[0]['code'], error_code)

actual_code = "Actual unknown error type"
error_list = self.error_handler.format_error_from_context(error_code, self.error_handler.error_context, "param1", param2=0,
actual_error=actual_code)
self.assertEqual(error_list[0]['code'], actual_code)
Empty file.