utils.py 3.2 KB

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