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 True if the status is OK."""
32+ return self .status == DBStatus .OK
33+
34+
35+ @unique
36+ class DBStatus (str , Enum ):
37+ OK = "OK"
38+ WARNING = "WARNING"
39+ ERROR = "ERROR"
2040
2141
2242def sanity_check (
2343 db_root : str | Path ,
2444 * ,
2545 revision : str | None = None ,
2646 include_warning : bool = False ,
27- ) -> DBException | None :
47+ ) -> DBException :
2848 """Perform sanity check and report exception or warning encountered while loading the dataset.
2949
3050 Args:
@@ -44,14 +64,28 @@ def sanity_check(
4464 warnings .filterwarnings ("ignore" )
4565
4666 try :
47- _ = Tier4 (data_root = db_root , revision = revision , verbose = False )
48- exception = None
67+ t4 = Tier4 (data_root = db_root , revision = revision , verbose = False )
68+ exception = DBException (
69+ dataset_id = t4 .dataset_id ,
70+ version = t4 .version ,
71+ status = DBStatus .OK ,
72+ )
73+ except Warning as w :
74+ metadata = load_metadata (db_root )
75+
76+ exception = DBException (
77+ dataset_id = metadata .dataset_id ,
78+ version = metadata .version ,
79+ status = DBStatus .WARNING ,
80+ message = str (w ),
81+ )
4982 except Exception as e :
5083 metadata = load_metadata (db_root )
5184
5285 exception = DBException (
5386 dataset_id = metadata .dataset_id ,
5487 version = metadata .version ,
88+ status = DBStatus .ERROR ,
5589 message = str (e ),
5690 )
5791 return exception
0 commit comments