Skip to content

Commit

Permalink
fixed unicode decode error throwing helpfull error for user. fixes #7050
Browse files Browse the repository at this point in the history
 fixes #7056
  • Loading branch information
tmbo committed Oct 21, 2020
1 parent 7215f97 commit eb4ab08
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
8 changes: 8 additions & 0 deletions rasa/shared/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ def __str__(self) -> Text:
"YAML syntax of your file."
)
return exception_text


class FileNotFoundException(RasaException, FileNotFoundError):
"""Raised when a file, expected to exist, doesn't exist."""


class FileIOException(RasaException):
"""Raised if there is an error while doing file IO."""
29 changes: 19 additions & 10 deletions rasa/shared/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
ENV_LOG_LEVEL,
NEXT_MAJOR_VERSION_FOR_DEPRECATIONS,
)
from rasa.shared.exceptions import YamlSyntaxException
from rasa.shared.exceptions import (
FileIOException,
FileNotFoundException,
YamlSyntaxException,
)

DEFAULT_ENCODING = "utf-8"
YAML_VERSION = (1, 2)
Expand Down Expand Up @@ -117,7 +121,16 @@ def read_file(filename: Union[Text, Path], encoding: Text = DEFAULT_ENCODING) ->
with open(filename, encoding=encoding) as f:
return f.read()
except FileNotFoundError:
raise ValueError(f"File '{filename}' does not exist.")
raise FileNotFoundException(
f"Failed to read file, " f"'{os.path.abspath(filename)}' does not exist."
)
except UnicodeDecodeError:
raise FileIOException(
f"Failed to read file '{os.path.abspath(filename)}', "
f"could not read the file using {encoding} to decode "
f"it. Please make sure the file is stored with this "
f"encoding."
)


def read_json_file(filename: Union[Text, Path]) -> Any:
Expand All @@ -126,9 +139,8 @@ def read_json_file(filename: Union[Text, Path]) -> Any:
try:
return json.loads(content)
except ValueError as e:
raise ValueError(
"Failed to read json from '{}'. Error: "
"{}".format(os.path.abspath(filename), e)
raise FileIOException(
f"Failed to read json from '{os.path.abspath(filename)}'. Error: {e}"
)


Expand All @@ -140,8 +152,7 @@ def list_directory(path: Text) -> List[Text]:

if not isinstance(path, str):
raise ValueError(
"`resource_name` must be a string type. "
"Got `{}` instead".format(type(path))
f"`resource_name` must be a string type. " f"Got `{type(path)}` instead"
)

if os.path.isfile(path):
Expand All @@ -159,9 +170,7 @@ def list_directory(path: Text) -> List[Text]:
results.extend(os.path.join(base, f) for f in good_directories)
return results
else:
raise ValueError(
"Could not locate the resource '{}'.".format(os.path.abspath(path))
)
raise ValueError(f"Could not locate the resource '{os.path.abspath(path)}'.")


def list_files(path: Text) -> List[Text]:
Expand Down
8 changes: 8 additions & 0 deletions tests/shared/utils/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest

import rasa.shared
from rasa.shared.exceptions import FileIOException
import rasa.shared.utils.io
from rasa.shared.constants import NEXT_MAJOR_VERSION_FOR_DEPRECATIONS
from rasa.shared.utils.io import raise_deprecation_warning
Expand Down Expand Up @@ -378,3 +379,10 @@ def test_raise_deprecation_warning_default():
f"This feature is deprecated. "
f"(will be removed in {NEXT_MAJOR_VERSION_FOR_DEPRECATIONS})"
)


def test_read_file_with_wrong_encoding(tmp_path: Path):
file = tmp_path / "myfile.txt"
file.write_text("ä", encoding="latin-1")
with pytest.raises(FileIOException):
rasa.shared.utils.io.read_file(file)

0 comments on commit eb4ab08

Please sign in to comment.