Skip to content

Commit

Permalink
resolve conda#7242 configuration load error message
Browse files Browse the repository at this point in the history
Signed-off-by: Kale Franz <kfranz@continuum.io>
  • Loading branch information
kalefranz committed May 2, 2018
1 parent c467517 commit 78eace1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
6 changes: 3 additions & 3 deletions conda/base/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from .._vendor.auxlib.ish import dals
from .._vendor.boltons.setutils import IndexedSet
from ..common.compat import NoneType, iteritems, itervalues, odict, on_win, string_types
from ..common.configuration import (Configuration, LoadError, MapParameter, PrimitiveParameter,
from ..common.configuration import (Configuration, ConfigurationLoadError, MapParameter, PrimitiveParameter,
SequenceParameter, ValidationError)
from ..common.disk import conda_bld_ensure_dir
from ..common.path import expand
Expand Down Expand Up @@ -1192,7 +1192,7 @@ def get_prefix(ctx, args, search=True): # pragma: no cover

try:
context = Context((), None)
except LoadError as e: # pragma: no cover
print(e, file=sys.stderr)
except ConfigurationLoadError as e: # pragma: no cover
print(repr(e), file=sys.stderr)
# Exception handler isn't loaded so use sys.exit
sys.exit(1)
25 changes: 13 additions & 12 deletions conda/common/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ def __call__(self, *args, **kwargs):
return self.handler(e)
try: # pragma: no cover
from ruamel_yaml.comments import CommentedSeq, CommentedMap
from ruamel_yaml.scanner import ScannerError
from ruamel_yaml.scanner import ReaderError, ScannerError
except ImportError: # pragma: no cover
from ruamel.yaml.comments import CommentedSeq, CommentedMap # pragma: no cover
from ruamel.yaml.scanner import ScannerError
from ruamel.yaml.scanner import ReaderError, ScannerError

log = getLogger(__name__)

Expand All @@ -81,19 +81,16 @@ def pretty_map(dictionary, padding=' '):
return '\n'.join("%s%s: %s" % (padding, key, value) for key, value in iteritems(dictionary))


class LoadError(CondaError):
def __init__(self, message, filepath, line, column):
self.line = line
self.filepath = filepath
self.column = column
msg = "Load Error: in %s on line %s, column %s. %s" % (filepath, line, column, message)
super(LoadError, self).__init__(msg)


class ConfigurationError(CondaError):
pass


class ConfigurationLoadError(ConfigurationError):
def __init__(self, path, message_addition=''):
message = "Unable to load configuration file.\n path: %(path)s\n"
super(ConfigurationLoadError, self).__init__(message + message_addition, path=path)


class ValidationError(ConfigurationError):

def __init__(self, parameter_name, parameter_value, source, msg=None, **kwargs):
Expand Down Expand Up @@ -345,7 +342,11 @@ def make_raw_parameters_from_file(cls, filepath):
ruamel_yaml = yaml_load(fh)
except ScannerError as err:
mark = err.problem_mark
raise LoadError("Invalid YAML", filepath, mark.line, mark.column)
raise ConfigurationLoadError(filepath, "reason: invalid yaml at line %s, column %s"
"" % (mark.line, mark.column))
except ReaderError as err:
raise ConfigurationLoadError(filepath, "reason: invalid yaml at position %s"
"" % err.position)
return cls.make_raw_parameters(filepath, ruamel_yaml) or EMPTY_MAP


Expand Down
4 changes: 2 additions & 2 deletions tests/cli/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from conda.base.context import context, reset_context
from conda.cli.python_api import Commands, run_command
from conda.common.configuration import LoadError
from conda.common.configuration import ConfigurationLoadError
from conda.common.serialize import yaml_load
from conda.gateways.disk.delete import rm_rf

Expand Down Expand Up @@ -54,7 +54,7 @@ def test_invalid_config():
with make_temp_condarc(condarc) as rc:
rc_path = rc
run_command(Commands.CONFIG, '--file', rc, '--add', 'channels', 'test')
except LoadError as err:
except ConfigurationLoadError as err:
error1 = "Load Error: in "
error2 = "on line 1, column 8. Invalid YAML"
assert error1 in err.message
Expand Down

0 comments on commit 78eace1

Please sign in to comment.