提交 a807c45a 编写于 作者: O ougongchang

add some exceptions, such as TrainJobNotExistError, GraphNotExistError and so on

上级 bf31095f
......@@ -90,3 +90,51 @@ class MaxCountExceededError(MindInsightException):
super(MaxCountExceededError, self).__init__(DataVisualErrors.MAX_COUNT_EXCEEDED_ERROR,
error_msg,
http_code=400)
class TrainJobNotExistError(MindInsightException):
"""Can not find the given train job."""
def __init__(self, error_detail=None):
if error_detail is None:
error_msg = f"Train job is not exist."
else:
error_msg = f"Train job is not exist. Detail: {error_detail}"
super(TrainJobNotExistError, self).__init__(DataVisualErrors.TRAIN_JOB_NOT_EXIST,
error_msg,
http_code=400)
class PluginNotAvailableError(MindInsightException):
"""The given plugin is not available."""
def __init__(self, error_detail):
error_msg = f"Plugin is not available. Detail: {error_detail}"
super(PluginNotAvailableError, self).__init__(DataVisualErrors.PLUGIN_NOT_AVAILABLE,
error_msg,
http_code=400)
class GraphNotExistError(MindInsightException):
"""Can not found the given graph."""
def __init__(self):
error_msg = 'Graph is not exist.'
super(GraphNotExistError, self).__init__(DataVisualErrors.GRAPH_NOT_EXIST,
error_msg,
http_code=400)
class ImageNotExistError(MindInsightException):
"""Unable to get a image based on a given condition."""
def __init__(self, error_detail):
error_msg = f'Image is not exist. Detail: {error_detail}'
super(ImageNotExistError, self).__init__(DataVisualErrors.IMAGE_NOT_EXIST,
error_msg,
http_code=400)
class ScalarNotExistError(MindInsightException):
"""Unable to get scalar values based on a given condition."""
def __init__(self, error_detail):
error_msg = f'Scalar value is not exist. Detail: {error_detail}'
super(ScalarNotExistError, self).__init__(DataVisualErrors.SCALAR_NOT_EXIST,
error_msg,
http_code=400)
......@@ -16,6 +16,7 @@
from numbers import Number
from mindinsight.utils.exceptions import ParamValueError
from mindinsight.utils.exceptions import ParamMissError
from mindinsight.datavisual.common.exceptions import PluginNotAvailableError
from mindinsight.datavisual.common.enums import PluginNameEnum
from mindinsight.datavisual.utils.tools import to_int
......@@ -94,9 +95,8 @@ class Validation:
plugin_name (str): The plugin name.
Raises:
ParamValueError: When plugin name is not valid.
PluginNotAvailableError: When plugin name is not valid.
"""
plugin_name_list = PluginNameEnum.list_members()
if plugin_name not in plugin_name_list:
raise ParamValueError("'plugin_name' only can be one of {}"
"".format(plugin_name_list))
raise PluginNotAvailableError(f"'plugin_name' only can be one of {plugin_name_list}")
......@@ -30,6 +30,7 @@ from mindinsight.datavisual.common import exceptions
from mindinsight.datavisual.common.log import logger
from mindinsight.datavisual.common.enums import DataManagerStatus
from mindinsight.datavisual.common.enums import PluginNameEnum
from mindinsight.datavisual.common.exceptions import TrainJobNotExistError
from mindinsight.datavisual.data_transform.loader_generators.loader_generator import MAX_DATA_LOADER_SIZE
from mindinsight.datavisual.data_transform.loader_generators.data_loader_generator import DataLoaderGenerator
from mindinsight.utils.exceptions import MindInsightException
......@@ -343,7 +344,7 @@ class DataManager:
self._check_status_valid()
loader_pool = self._get_snapshot_loader_pool()
if not self._is_loader_in_loader_pool(train_id, loader_pool):
raise ParamValueError("Can not find any data in loader pool about the train job.")
raise TrainJobNotExistError("Can not find the given train job in cache.")
data_loader = loader_pool[train_id].data_loader
events_data = data_loader.get_events_data()
......@@ -365,7 +366,7 @@ class DataManager:
loader_pool (dict[str, LoaderStruct]): Refer to self._loader_pool.
Raises:
ParamValueError: Can not found train job in data manager.
TrainJobNotExistError: Can not find train job in data manager.
"""
is_exist = False
if train_id in loader_pool:
......@@ -375,7 +376,7 @@ class DataManager:
is_exist = True
break
if not is_exist:
raise ParamValueError("Can not find the train job in data manager.")
raise TrainJobNotExistError("Can not find the train job in data manager.")
def _is_loader_in_loader_pool(self, train_id, loader_pool):
"""
......@@ -406,8 +407,7 @@ class DataManager:
"""Check if the status is valid to load data."""
if self.status == DataManagerStatus.INIT.value:
raise exceptions.SummaryLogIsLoading("Data is being loaded, "
"current status: %s." % self._status)
raise exceptions.SummaryLogIsLoading("Data is being loaded, current status: %s." % self._status)
def get_single_train_job(self, train_id, manual_update=False):
"""
......
......@@ -40,9 +40,9 @@ class GraphProcessor(BaseProcessor):
train_job = self._data_manager.get_train_job_by_plugin(train_id, PluginNameEnum.GRAPH.value)
if train_job is None:
raise exceptions.SummaryLogPathInvalid()
raise exceptions.TrainJobNotExistError()
if not train_job['tags']:
raise ParamValueError("Can not find any graph data in the train job.")
raise exceptions.GraphNotExistError()
if tag is None:
tag = train_job['tags'][0]
......
......@@ -16,6 +16,7 @@
from mindinsight.datavisual.utils.tools import to_int
from mindinsight.utils.exceptions import ParamValueError
from mindinsight.datavisual.common.validation import Validation
from mindinsight.datavisual.common.exceptions import ImageNotExistError
from mindinsight.datavisual.processors.base_processor import BaseProcessor
......@@ -46,7 +47,10 @@ class ImageProcessor(BaseProcessor):
"""
Validation.check_param_empty(train_id=train_id, tag=tag)
result = []
tensors = self._data_manager.list_tensors(train_id, tag)
try:
tensors = self._data_manager.list_tensors(train_id, tag)
except ParamValueError as ex:
raise ImageNotExistError(ex.message)
for tensor in tensors:
# no tensor_proto in TensorEvent
......@@ -75,7 +79,10 @@ class ImageProcessor(BaseProcessor):
Validation.check_param_empty(train_id=train_id, tag=tag, step=step)
step = to_int(step, "step")
tensors = self._data_manager.list_tensors(train_id, tag)
try:
tensors = self._data_manager.list_tensors(train_id, tag)
except ParamValueError as ex:
raise ImageNotExistError(ex.message)
image = None
for tensor in tensors:
......@@ -87,6 +94,6 @@ class ImageProcessor(BaseProcessor):
break
if image is None:
raise ParamValueError("Can not find the step with given train job id and tag.")
raise ImageNotExistError("Can not find the step with given train job id and tag.")
return image
......@@ -13,6 +13,8 @@
# limitations under the License.
# ============================================================================
"""Scalar Processor APIs."""
from mindinsight.utils.exceptions import ParamValueError
from mindinsight.datavisual.common.exceptions import ScalarNotExistError
from mindinsight.datavisual.common.validation import Validation
from mindinsight.datavisual.processors.base_processor import BaseProcessor
......@@ -33,7 +35,10 @@ class ScalarsProcessor(BaseProcessor):
"""
Validation.check_param_empty(train_id=train_id, tag=tag)
job_response = []
tensors = self._data_manager.list_tensors(train_id, tag)
try:
tensors = self._data_manager.list_tensors(train_id, tag)
except ParamValueError as ex:
raise ScalarNotExistError(ex.message)
for tensor in tensors:
job_response.append({
......
......@@ -38,7 +38,7 @@ class TrainTaskManager(BaseProcessor):
Validation.check_plugin_name(plugin_name=plugin_name)
train_job = self._data_manager.get_train_job_by_plugin(train_id=train_id, plugin_name=plugin_name)
if train_job is None:
raise exceptions.SummaryLogPathInvalid()
raise exceptions.TrainJobNotExistError()
return dict(train_jobs=[train_job])
def get_plugins(self, train_id, manual_update=True):
......
......@@ -133,7 +133,7 @@ def get_train_id(request):
try:
train_id = unquote(train_id, errors='strict')
except UnicodeDecodeError:
raise exceptions.ParamValueError('Unquote error with strict mode')
raise exceptions.UrlDecodeError('Unquote train id error with strict mode')
return train_id
......
......@@ -40,6 +40,7 @@ class GeneralErrors(Enum):
PATH_NOT_EXISTS_ERROR = 4
FILE_SYSTEM_PERMISSION_ERROR = 8
PORT_NOT_AVAILABLE_ERROR = 9
URL_DECODE_ERROR = 10
class LineageMgrErrors(Enum):
......@@ -55,6 +56,9 @@ class DataVisualErrors(Enum):
TRAIN_JOB_NOT_EXIST = 5
SUMMARY_LOG_PATH_INVALID = 6
SUMMARY_LOG_IS_LOADING = 7
SUMMARY_LOG_LOAD_FAILED = 8
NODE_NOT_IN_GRAPH_ERROR = 9
PATH_NOT_DIRECTORY_ERROR = 10
PLUGIN_NOT_AVAILABLE = 11
GRAPH_NOT_EXIST = 12
IMAGE_NOT_EXIST = 13
SCALAR_NOT_EXIST = 14
......@@ -176,3 +176,12 @@ class UnknownError(MindInsightException):
GeneralErrors.UNKNOWN_ERROR,
error_msg,
http_code=500)
class UrlDecodeError(MindInsightException):
"""Url decoding failed"""
def __init__(self, error_detail):
error_msg = f"Url decode failed. Detail: {error_detail}"
super(UrlDecodeError, self).__init__(GeneralErrors.URL_DECODE_ERROR,
error_msg,
http_code=400)
......@@ -127,9 +127,8 @@ class TestSingleImage:
assert response.status_code == 400
response = response.get_json()
assert response['error_code'] == '50540002'
assert response['error_msg'] == "Invalid parameter value. Can not find any data " \
"in loader pool about the train job."
assert response['error_code'] == '50545005'
assert response['error_msg'] == "Train job is not exist. Detail: Can not find the given train job in cache."
@pytest.mark.level1
@pytest.mark.env_single
......@@ -149,9 +148,9 @@ class TestSingleImage:
assert response.status_code == 400
response = response.get_json()
assert response['error_code'] == '50540002'
assert response['error_msg'] == "Invalid parameter value. Can not find any data " \
"in this train job by given tag."
assert response['error_code'] == '5054500D'
assert response['error_msg'] == "Image is not exist. Detail: Invalid parameter value. " \
"Can not find any data in this train job by given tag."
@pytest.mark.level1
@pytest.mark.env_single
......@@ -170,9 +169,9 @@ class TestSingleImage:
assert response.status_code == 400
response = response.get_json()
assert response['error_code'] == '50540002'
assert response['error_msg'] == "Invalid parameter value. Can not find the step " \
"with given train job id and tag."
assert response['error_code'] == '5054500D'
assert response['error_msg'] == "Image is not exist. Detail: " \
"Can not find the step with given train job id and tag."
@pytest.mark.level1
@pytest.mark.env_single
......
......@@ -89,9 +89,8 @@ class TestPlugins:
assert response.status_code == 400
response = response.get_json()
assert response['error_code'] == '50540002'
assert response['error_msg'] == "Invalid parameter value. Can not find " \
"the train job in data manager."
assert response['error_code'] == '50545005'
assert response['error_msg'] == "Train job is not exist. Detail: Can not find the train job in data manager."
@pytest.mark.level1
@pytest.mark.env_single
......
......@@ -64,9 +64,9 @@ class TestTrainTask:
assert response.status_code == 400
response = response.get_json()
assert response['error_code'] == '50540002'
assert response['error_msg'] == "Invalid parameter value. 'plugin_name' " \
"only can be one of {}".format(plugin_name_list)
assert response['error_code'] == '5054500B'
assert response['error_msg'] == f"Plugin is not available. " \
f"Detail: 'plugin_name' only can be one of {plugin_name_list}"
@patch.object(TrainTaskManager, 'get_single_train_task')
def test_query_single_train_task_with_plugin_name_exists(self, mock_train_task_manager, client):
......
......@@ -28,6 +28,7 @@ from unittest.mock import Mock, patch
import pytest
from mindinsight.datavisual.common.enums import DataManagerStatus, PluginNameEnum
from mindinsight.datavisual.common.exceptions import TrainJobNotExistError
from mindinsight.datavisual.data_transform import data_manager, ms_data_loader
from mindinsight.datavisual.data_transform.data_loader import DataLoader
from mindinsight.datavisual.data_transform.data_manager import DataManager
......@@ -185,9 +186,10 @@ class TestDataManager:
d_manager._status = DataManagerStatus.LOADING.value
tag = 'image'
train_job_01 = 'train_01'
with pytest.raises(ParamValueError):
with pytest.raises(TrainJobNotExistError) as ex_info:
d_manager.list_tensors(train_job_01, tag)
shutil.rmtree(summary_base_dir)
assert ex_info.value.message == 'Train job is not exist. Detail: Can not find the given train job in cache.'
@patch.object(data_manager.DataLoaderGenerator, "generate_loaders")
def test_caching(self, mock_generate_loaders):
......
......@@ -26,6 +26,7 @@ import pytest
from mindinsight.datavisual.common import exceptions
from mindinsight.datavisual.common.enums import PluginNameEnum
from mindinsight.datavisual.common.exceptions import GraphNotExistError
from mindinsight.datavisual.data_transform import data_manager
from mindinsight.datavisual.data_transform.data_manager import DataManager
from mindinsight.datavisual.data_transform.loader_generators.data_loader_generator import DataLoaderGenerator
......@@ -102,16 +103,16 @@ class TestGraphProcessor:
def test_get_nodes_with_not_exist_train_id(self):
"""Test getting nodes with not exist train id."""
test_train_id = "not_exist_train_id"
with pytest.raises(ParamValueError) as exc_info:
with pytest.raises(exceptions.TrainJobNotExistError) as exc_info:
GraphProcessor(test_train_id, self._mock_data_manager)
assert "Can not find the train job in data manager." in exc_info.value.message
assert "Train job is not exist. Detail: Can not find the train job in data manager." == exc_info.value.message
@pytest.mark.usefixtures('load_graph_record')
@patch.object(DataManager, 'get_train_job_by_plugin')
def test_get_nodes_with_loader_is_none(self, mock_get_train_job_by_plugin):
"""Test get nodes with loader is None."""
mock_get_train_job_by_plugin.return_value = None
with pytest.raises(exceptions.SummaryLogPathInvalid):
with pytest.raises(exceptions.TrainJobNotExistError):
GraphProcessor(self._train_id, self._mock_data_manager)
assert mock_get_train_job_by_plugin.called
......@@ -223,7 +224,8 @@ class TestGraphProcessor:
@pytest.mark.usefixtures('load_no_graph_record')
def test_check_graph_status_no_graph(self):
"""Test checking graph status no graph."""
with pytest.raises(ParamValueError) as exc_info:
with pytest.raises(GraphNotExistError) as exc_info:
GraphProcessor(self._train_id, self._mock_data_manager)
assert exc_info.value.message == "Invalid parameter value. Can not find any graph data " \
"in the train job."
assert exc_info.value.error_code == '5054500C'
assert exc_info.value.message == "Graph is not exist."
......@@ -24,6 +24,8 @@ from unittest.mock import Mock
import pytest
from mindinsight.datavisual.common.enums import PluginNameEnum
from mindinsight.datavisual.common.exceptions import TrainJobNotExistError
from mindinsight.datavisual.common.exceptions import ImageNotExistError
from mindinsight.datavisual.data_transform import data_manager
from mindinsight.datavisual.data_transform.loader_generators.data_loader_generator import DataLoaderGenerator
from mindinsight.datavisual.processors.images_processor import ImageProcessor
......@@ -107,11 +109,11 @@ class TestImagesProcessor:
"""Test getting metadata list with not exist id."""
test_train_id = 'not_exist_id'
image_processor = ImageProcessor(self._mock_data_manager)
with pytest.raises(ParamValueError) as exc_info:
with pytest.raises(TrainJobNotExistError) as exc_info:
image_processor.get_metadata_list(test_train_id, self._tag_name)
assert exc_info.value.error_code == '50540002'
assert "Can not find any data in loader pool about the train job." in exc_info.value.message
assert exc_info.value.error_code == '50545005'
assert "Train job is not exist. Detail: Can not find the given train job in cache." == exc_info.value.message
@pytest.mark.usefixtures('load_image_record')
def test_get_metadata_list_with_not_exist_tag(self):
......@@ -120,10 +122,10 @@ class TestImagesProcessor:
image_processor = ImageProcessor(self._mock_data_manager)
with pytest.raises(ParamValueError) as exc_info:
with pytest.raises(ImageNotExistError) as exc_info:
image_processor.get_metadata_list(self._train_id, test_tag_name)
assert exc_info.value.error_code == '50540002'
assert exc_info.value.error_code == '5054500D'
assert "Can not find any data in this train job by given tag." in exc_info.value.message
@pytest.mark.usefixtures('load_image_record')
......@@ -144,11 +146,11 @@ class TestImagesProcessor:
test_step = self._steps_list[0]
image_processor = ImageProcessor(self._mock_data_manager)
with pytest.raises(ParamValueError) as exc_info:
with pytest.raises(TrainJobNotExistError) as exc_info:
image_processor.get_single_image(test_train_id, test_tag_name, test_step)
assert exc_info.value.error_code == '50540002'
assert "Can not find any data in loader pool about the train job." in exc_info.value.message
assert exc_info.value.error_code == '50545005'
assert "Train job is not exist. Detail: Can not find the given train job in cache." == exc_info.value.message
@pytest.mark.usefixtures('load_image_record')
def test_get_single_image_with_not_exist_tag(self):
......@@ -158,10 +160,10 @@ class TestImagesProcessor:
image_processor = ImageProcessor(self._mock_data_manager)
with pytest.raises(ParamValueError) as exc_info:
with pytest.raises(ImageNotExistError) as exc_info:
image_processor.get_single_image(self._train_id, test_tag_name, test_step)
assert exc_info.value.error_code == '50540002'
assert exc_info.value.error_code == '5054500D'
assert "Can not find any data in this train job by given tag." in exc_info.value.message
@pytest.mark.usefixtures('load_image_record')
......@@ -172,11 +174,12 @@ class TestImagesProcessor:
image_processor = ImageProcessor(self._mock_data_manager)
with pytest.raises(ParamValueError) as exc_info:
with pytest.raises(ImageNotExistError) as exc_info:
image_processor.get_single_image(self._train_id, test_tag_name, test_step)
assert exc_info.value.error_code == '50540002'
assert "Can not find the step with given train job id and tag." in exc_info.value.message
assert exc_info.value.error_code == '5054500D'
assert "Image is not exist. " \
"Detail: Can not find the step with given train job id and tag." == exc_info.value.message
@pytest.mark.usefixtures('load_image_record')
def test_get_single_image_success(self):
......@@ -206,7 +209,7 @@ class TestImagesProcessor:
try:
image_processor.get_single_image(self._train_id, test_tag_name, test_step)
except ParamValueError:
except ImageNotExistError:
cnt += 1
assert len(self._more_steps_list) - cnt == 10
......@@ -233,7 +236,7 @@ class TestImagesProcessor:
try:
image_processor.get_single_image(self._train_id, test_tag_name, test_step)
current_step_list.append(test_step)
except ParamValueError:
except ImageNotExistError:
not_found_step_list.append(test_step)
assert current_step_list == [1, 2, 3, 4, 15]
......
......@@ -24,11 +24,12 @@ from unittest.mock import Mock
import pytest
from mindinsight.datavisual.common.enums import PluginNameEnum
from mindinsight.datavisual.common.exceptions import TrainJobNotExistError
from mindinsight.datavisual.common.exceptions import ScalarNotExistError
from mindinsight.datavisual.data_transform import data_manager
from mindinsight.datavisual.data_transform.loader_generators.data_loader_generator import DataLoaderGenerator
from mindinsight.datavisual.processors.scalars_processor import ScalarsProcessor
from mindinsight.datavisual.utils import crc32
from mindinsight.utils.exceptions import ParamValueError
from ....utils.log_operations import LogOperations
from ....utils.tools import check_loading_done, delete_files_or_dirs
......@@ -84,11 +85,11 @@ class TestScalarsProcessor:
"""Get metadata list with not exist id."""
test_train_id = 'not_exist_id'
scalar_processor = ScalarsProcessor(self._mock_data_manager)
with pytest.raises(ParamValueError) as exc_info:
with pytest.raises(TrainJobNotExistError) as exc_info:
scalar_processor.get_metadata_list(test_train_id, self._tag_name)
assert exc_info.value.error_code == '50540002'
assert "Can not find any data in loader pool about the train job." in exc_info.value.message
assert exc_info.value.error_code == '50545005'
assert "Train job is not exist. Detail: Can not find the given train job in cache." == exc_info.value.message
@pytest.mark.usefixtures('load_scalar_record')
def test_get_metadata_list_with_not_exist_tag(self):
......@@ -97,10 +98,10 @@ class TestScalarsProcessor:
scalar_processor = ScalarsProcessor(self._mock_data_manager)
with pytest.raises(ParamValueError) as exc_info:
with pytest.raises(ScalarNotExistError) as exc_info:
scalar_processor.get_metadata_list(self._train_id, test_tag_name)
assert exc_info.value.error_code == '50540002'
assert exc_info.value.error_code == '5054500E'
assert "Can not find any data in this train job by given tag." in exc_info.value.message
@pytest.mark.usefixtures('load_scalar_record')
......
......@@ -25,11 +25,11 @@ from unittest.mock import Mock
import pytest
from mindinsight.datavisual.common.enums import PluginNameEnum
from mindinsight.datavisual.common.exceptions import TrainJobNotExistError
from mindinsight.datavisual.data_transform import data_manager
from mindinsight.datavisual.data_transform.loader_generators.data_loader_generator import DataLoaderGenerator
from mindinsight.datavisual.processors.train_task_manager import TrainTaskManager
from mindinsight.datavisual.utils import crc32
from mindinsight.utils.exceptions import ParamValueError
from ....utils.log_operations import LogOperations
from ....utils.tools import check_loading_done, delete_files_or_dirs
......@@ -109,12 +109,11 @@ class TestTrainTaskManager:
train_task_manager = TrainTaskManager(self._mock_data_manager)
for plugin_name in PluginNameEnum.list_members():
test_train_id = "not_exist_id"
with pytest.raises(ParamValueError) as exc_info:
with pytest.raises(TrainJobNotExistError) as exc_info:
_ = train_task_manager.get_single_train_task(plugin_name, test_train_id)
assert exc_info.type == ParamValueError
assert exc_info.value.message == "Invalid parameter value. Can not find " \
"the train job in data manager."
assert exc_info.value.error_code == '50540002'
assert exc_info.value.message == "Train job is not exist. " \
"Detail: Can not find the train job in data manager."
assert exc_info.value.error_code == '50545005'
@pytest.mark.usefixtures('load_data')
def test_get_single_train_task_with_params(self):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册