utils.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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. import os
  15. __all__ = [
  16. "build_res_dict",
  17. "CheckFailedError",
  18. "ConvertFailedError",
  19. "SplitFailedError",
  20. "AnalyseFailedError",
  21. "DatasetFileNotFoundError",
  22. ]
  23. def build_res_dict(check_pass, err_type=None, err_msg=None, **kwargs):
  24. """build the dataset checking result to return"""
  25. if check_pass:
  26. if err_type is not None:
  27. raise ValueError(
  28. f"`check_pass` is {check_pass}, but `err_type` is not None."
  29. )
  30. if err_msg is not None:
  31. raise ValueError(
  32. f"`check_pass` is {check_pass}, but `err_msg` is not None."
  33. )
  34. return dict(check_pass=check_pass, **kwargs)
  35. else:
  36. if err_type is None:
  37. raise ValueError(f"`check_pass` is {check_pass}, but `err_type` is None.")
  38. if err_msg is None:
  39. if _is_known_error_type(err_type):
  40. err_msg = ""
  41. else:
  42. raise ValueError(
  43. f"{err_type} is not a known error type, in which case `err_msg` must be specified to a value other \
  44. than None."
  45. )
  46. return dict(check_pass=check_pass, err_type=err_type, err_msg=err_msg, **kwargs)
  47. class FailedError(Exception):
  48. """base error class"""
  49. def __init__(self, err_info=None, solution=None, message=None):
  50. if message is None:
  51. message = self._construct_message(err_info, solution)
  52. super().__init__(message)
  53. def _construct_message(self, err_info, solution):
  54. if err_info is None:
  55. return ""
  56. else:
  57. msg = (
  58. f"{self.mode} failed. We encountered the following error:\n {err_info}"
  59. )
  60. if solution is not None:
  61. msg += f"\nPlease try to resolve the issue as follows:\n {solution}"
  62. return msg
  63. class CheckFailedError(FailedError):
  64. """check dataset error"""
  65. mode = "Check dataset"
  66. class ConvertFailedError(FailedError):
  67. """convert dataset error"""
  68. mode = "Convert dataset"
  69. class SplitFailedError(FailedError):
  70. """split dataset error"""
  71. mode = "Split dataset"
  72. class AnalyseFailedError(FailedError):
  73. """analyse dataset error"""
  74. mode = "Analyse dataset"
  75. class DatasetFileNotFoundError(CheckFailedError):
  76. """dataset file not found error"""
  77. def __init__(self, file_path=None, err_info=None, solution=None, message=None):
  78. if err_info is None:
  79. if file_path is not None:
  80. err_info = f"{file_path} does not exist."
  81. super().__init__(err_info, solution, message)
  82. def _is_known_error_type(err_type):
  83. """is known error type"""
  84. return isinstance(err_type, CheckFailedError)