Skip to content
Snippets Groups Projects

Resolve "Jupyter notebook issues"

Closed Ivan Kondov requested to merge 240-jupyter-notebook-issues into master
1 unresolved thread
#NEW CODE
"""handling domain-specific errors"""
import sys
import importlib
@@ -7,6 +9,15 @@ from pint.errors import OffsetUnitCalculusError
from ase.calculators.calculator import CalculatorSetupError
from virtmat.middleware.exceptions import ResourceConfigurationError
from virtmat.language.utilities.textx import get_location_context
from .exceptions import (
ObjectImportError,
ObjectFromFileError,
ObjectSchemaValidationError,
ObjectAlreadyExistsError,
ObjectNotFoundError,
ObjectConversionError,
ObjectUniqueConstraintError
)
FILE_READ_EXCEPTION_IMPORTS = {'ruamel.yaml.parser': 'ParserError',
'ruamel.yaml.scanner': 'ScannerError',
@@ -226,77 +237,59 @@ def format_textxerr_msg(err):
return msg
@textxerror_wrap
def raise_exception(_, exception, msg, where_used=None):
"""utility function to raise an exception at a custom location"""
if where_used is not None:
err = TextXError('', **get_location_context(where_used))
msg += f'\n used here: {format_textxerr_msg(err)}'
raise exception(msg)
try:
from ruamel.yaml.error import MarkedYAMLError
except (ModuleNotFoundError, AttributeError):
class MarkedYAMLError(Exception):
pass
try:
from jsonschema import ValidationError
except (ModuleNotFoundError, AttributeError):
class ValidationError(Exception):
pass
try:
from pymongo.errors import DuplicateKeyError
except (ModuleNotFoundError, AttributeError):
class DuplicateKeyError(Exception):
pass
TEXTX_WRAPPED_EXCEPTIONS = (DimensionalityError, UndefinedUnitError, PintError,
InvalidUnitError, CalculatorSetupError, StructureInputError,
StaticTypeError, RuntimeTypeError, StaticValueError,
RuntimeValueError, PropertyError, SubscriptingError,
EvaluationError, AncestorEvaluationError, ArithmeticError,
FileExistsError, OSError)
try:
from bson.errors import BSONError
except (ModuleNotFoundError, AttributeError):
class BSONError(Exception):
pass
def process_error(err):
"""generic error processor for errors of class TextXError"""
if err.err_type is None:
if isinstance(err, TextXSyntaxError):
err_type = 'Syntax error'
elif isinstance(err, TextXSemanticError):
err_type = 'Semantic error'
elif isinstance(err.__cause__, DimensionalityError):
err_type = 'Dimensionality error'
elif isinstance(err.__cause__, UndefinedUnitError):
err_type = 'Undefined unit'
elif isinstance(err.__cause__, OffsetUnitCalculusError):
err_type = 'Offset unit calculus error'
elif isinstance(err.__cause__, PintError):
err_type = 'Units error'
elif isinstance(err.__cause__, InvalidUnitError):
err_type = 'Invalid units error'
elif isinstance(err.__cause__, CalculatorSetupError):
err_type = 'Calculator setup error'
elif isinstance(err.__cause__, StructureInputError):
err_type = 'Structure input error'
elif isinstance(err.__cause__, (StaticTypeError, RuntimeTypeError)):
err_type = 'Type error'
elif isinstance(err.__cause__, (StaticValueError, RuntimeValueError)):
err_type = 'Value error'
elif isinstance(err.__cause__, PropertyError):
err_type = 'Invalid key'
elif isinstance(err.__cause__, SubscriptingError):
err_type = 'Invalid index'
elif isinstance(err.__cause__, ConvergenceError):
err_type = 'Convergence error'
elif isinstance(err.__cause__, EvaluationError):
err_type = 'Evaluation error'
elif isinstance(err.__cause__, AncestorEvaluationError):
err_type = 'Ancestor evaluation error'
elif isinstance(err.__cause__, UpdateError):
err_type = 'Variable update error'
elif isinstance(err.__cause__, TagError):
err_type = 'Tag error'
elif isinstance(err.__cause__, ResourceConfigurationError):
err_type = 'Resource configuration error'
elif isinstance(err.__cause__, ArithmeticError):
err_type = 'Arithmetic error'
elif isinstance(err.__cause__, NotImplementedError):
err_type = 'Not implemented'
elif isinstance(err.__cause__, FileExistsError):
err_type = 'File exists error'
elif isinstance(err.__cause__, OSError):
err_type = 'Operating system error'
err_type = "Error"
if err.__cause__ is not None:
if isinstance(err.__cause__, MarkedYAMLError):
err_type = 'YAML syntax error'
elif isinstance(err.__cause__, ValidationError):
err_type = 'Schema validation error'
elif isinstance(err.__cause__, DuplicateKeyError):
err_type = 'Duplicate key error'
elif isinstance(err.__cause__, BSONError):
err_type = 'BSON encoding/decoding error'
elif isinstance(err.__cause__, ObjectImportError):
err_type = 'Object import error'
elif isinstance(err.__cause__, ObjectFromFileError):
err_type = 'Object from file error'
elif isinstance(err.__cause__, ObjectSchemaValidationError):
err_type = 'Object schema validation error'
elif isinstance(err.__cause__, ObjectAlreadyExistsError):
err_type = 'Object already exists error'
elif isinstance(err.__cause__, ObjectNotFoundError):
err_type = 'Object not found error'
elif isinstance(err.__cause__, ObjectConversionError):
err_type = 'Object conversion error'
elif isinstance(err.__cause__, ObjectUniqueConstraintError):
err_type = 'Object unique constraint error'
else:
raise err.__cause__
else:
err_type = err.err_type
print_stderr(err_type + ': ' + format_textxerr_msg(err))
return err_type
def error_handler(func):
@@ -304,39 +297,12 @@ def error_handler(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except TextXError as err:
process_error(err)
return None
except ObjectFromFileError as err:
cause_cls = err.__cause__.__class__.__qualname__
cause_mod = err.__cause__.__class__.__module__
print_stderr(f'{cause_mod}.{cause_cls}: {err.path}: {err.__cause__}')
return None
except CompatibilityError as err:
print_stderr(f'Compatibility error: {err}')
return None
except VaryError as err:
print_stderr(f'Vary error: {err}')
return None
except QueryError as err:
print_stderr(f'Query error: {err}')
return None
except ReuseError as err:
print_stderr(f'Reuse error: {err}')
return None
except ModelNotFoundError as err:
print_stderr(f'Model not found: {err}')
return None
except ConfigurationError as err:
print_stderr(f'Configuration error: {err}')
return None
except UpdateError as err:
print_stderr(f'Variable update error: {err}')
return None
except tuple([*MONGODB_EXCEPTIONS, *FILE_READ_EXCEPTIONS]) as err:
err_cls = err.__class__
print_stderr(f'{err_cls.__module__}.{err_cls.__qualname__}: {err}')
return None
except Exception as err:
raise RuntimeError('non-handled exception') from err
return wrapper
err_type = process_error(err)
print_stderr(format_textxerr_msg(f'{err_type}: {err}'))
if isinstance(err, ObjectFromFileError):
cause_cls = err.__cause__.__class__.__qualname__
cause_mod = err.__cause__.__class__.__module__
print_stderr(f'{cause_mod}.{cause_cls}: {err.path}: {err.__cause__}')
raise RuntimeError('Unhandled exception occurred') from err
return wrapper
\ No newline at end of file
Loading