You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 2.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Lineage writer utils."""
  16. from functools import wraps
  17. from mindinsight.lineagemgr.common.exceptions.error_code import LineageErrors, LineageErrorMsg
  18. from mindinsight.utils.exceptions import MindInsightException
  19. class LineageParamRunContextError(MindInsightException):
  20. """The input parameter run_context error in lineage module."""
  21. def __init__(self, msg):
  22. super(LineageParamRunContextError, self).__init__(
  23. error=LineageErrors.PARAM_RUN_CONTEXT_ERROR,
  24. message=LineageErrorMsg.PARAM_RUN_CONTEXT_ERROR.value.format(msg),
  25. http_code=400
  26. )
  27. class LineageGetModelFileError(MindInsightException):
  28. """The get model file error in lineage module."""
  29. def __init__(self, msg):
  30. super(LineageGetModelFileError, self).__init__(
  31. error=LineageErrors.LINEAGE_GET_MODEL_FILE_ERROR,
  32. message=LineageErrorMsg.LINEAGE_GET_MODEL_FILE_ERROR.value.format(msg),
  33. http_code=400
  34. )
  35. class LineageLogError(MindInsightException):
  36. """The lineage collector error."""
  37. def __init__(self, msg):
  38. super(LineageLogError, self).__init__(
  39. error=LineageErrors.LOG_LINEAGE_INFO_ERROR,
  40. message=LineageErrorMsg.LOG_LINEAGE_INFO_ERROR.value.format(msg),
  41. http_code=400
  42. )
  43. def try_except(logger):
  44. """
  45. Catch or raise exceptions while collecting lineage.
  46. Args:
  47. logger (logger): The logger instance which logs the warning info.
  48. Returns:
  49. function, the decorator which we use to retry the decorated function.
  50. """
  51. def try_except_decorate(func):
  52. @wraps(func)
  53. def wrapper(self, *args, **kwargs):
  54. try:
  55. func(self, *args, **kwargs)
  56. except (AttributeError, MindInsightException,
  57. LineageParamRunContextError, LineageLogError,
  58. LineageGetModelFileError, IOError) as err:
  59. logger.error(err)
  60. try:
  61. raise_except = self.raise_exception
  62. except AttributeError:
  63. raise_except = False
  64. if raise_except is True:
  65. raise
  66. return wrapper
  67. return try_except_decorate