11from __future__ import annotations
22
33import warnings
4+ from enum import Enum , unique
45from pathlib import Path
56
67from attrs import define
1213
1314@define
1415class DBException :
15- """A dataclass to store error message of the corresponding dataset."""
16+ """A dataclass to store error message of the corresponding dataset.
17+
18+ Attributes:
19+ dataset_id (str): Dataset ID.
20+ version (str | None): Dataset version.
21+ status (DBStatus): Status of the dataset.
22+ message (str): Error or warning message.
23+ """
1624
1725 dataset_id : str
1826 version : str | None
19- message : str
27+ status : DBStatus
28+ message : str | None = None
29+
30+ def is_ok (self ) -> bool :
31+ return self .status == DBStatus .OK
32+
33+
34+ @unique
35+ class DBStatus (str , Enum ):
36+ OK = "OK"
37+ WARNING = "WARNING"
38+ ERROR = "ERROR"
2039
2140
2241def sanity_check (
2342 db_root : str | Path ,
2443 * ,
2544 revision : str | None = None ,
2645 include_warning : bool = False ,
27- ) -> DBException | None :
46+ ) -> DBException :
2847 """Perform sanity check and report exception or warning encountered while loading the dataset.
2948
3049 Args:
@@ -44,14 +63,28 @@ def sanity_check(
4463 warnings .filterwarnings ("ignore" )
4564
4665 try :
47- _ = Tier4 (data_root = db_root , revision = revision , verbose = False )
48- exception = None
66+ t4 = Tier4 (data_root = db_root , revision = revision , verbose = False )
67+ exception = DBException (
68+ dataset_id = t4 .dataset_id ,
69+ version = t4 .version ,
70+ status = DBStatus .OK ,
71+ )
72+ except Warning as w :
73+ metadata = load_metadata (db_root )
74+
75+ exception = DBException (
76+ dataset_id = metadata .dataset_id ,
77+ version = metadata .version ,
78+ status = DBStatus .WARNING ,
79+ message = str (w ),
80+ )
4981 except Exception as e :
5082 metadata = load_metadata (db_root )
5183
5284 exception = DBException (
5385 dataset_id = metadata .dataset_id ,
5486 version = metadata .version ,
87+ status = DBStatus .ERROR ,
5588 message = str (e ),
5689 )
5790 return exception
0 commit comments