|
5 | 5 | In MMIF, views are created by apps in a pipeline that are annotating
|
6 | 6 | data that was previously present in the MMIF file.
|
7 | 7 | """
|
| 8 | +import json |
8 | 9 | from datetime import datetime
|
9 | 10 | from typing import Dict, Union, Optional, Generator, List, cast
|
10 | 11 |
|
@@ -230,6 +231,23 @@ def __getitem__(self, key: str) -> 'Annotation':
|
230 | 231 | def set_error(self, err_message: str, err_trace: str) -> None:
|
231 | 232 | self.metadata.set_error(err_message, err_trace)
|
232 | 233 | self.annotations.empty()
|
| 234 | + |
| 235 | + def get_error(self) -> Optional[str]: |
| 236 | + """ |
| 237 | + Get the "text" representation of the error occurred during |
| 238 | + processing. Text representation is supposed to be human-readable. |
| 239 | + When ths view does not have any error, returns None. |
| 240 | + """ |
| 241 | + if self.has_error(): |
| 242 | + return self.metadata.get_error_as_text() |
| 243 | + else: |
| 244 | + return None |
| 245 | + |
| 246 | + def has_error(self) -> bool: |
| 247 | + return self.metadata.has_error() |
| 248 | + |
| 249 | + def has_warnings(self): |
| 250 | + return self.metadata.has_warnings() |
233 | 251 |
|
234 | 252 |
|
235 | 253 | class ViewMetadata(MmifObject):
|
@@ -267,7 +285,24 @@ def _serialize(self, alt_container: Optional[Dict] = None) -> dict:
|
267 | 285 | if not (self.contains.items() or self.error or self.warnings):
|
268 | 286 | serialized['contains'] = {}
|
269 | 287 | return serialized
|
270 |
| - |
| 288 | + |
| 289 | + def has_error(self) -> bool: |
| 290 | + return len(self.error) > 0 |
| 291 | + |
| 292 | + def has_warnings(self): |
| 293 | + return len(self.warnings) > 0 |
| 294 | + |
| 295 | + def get_error_as_text(self) -> str: |
| 296 | + if self.has_error(): |
| 297 | + if isinstance(self.error, ErrorDict): |
| 298 | + return str(self.error) |
| 299 | + elif isinstance(self.error, dict): |
| 300 | + return f"Error: {json.dumps(self.error, indent=2)}" |
| 301 | + else: |
| 302 | + return f"Error (unknown error format): {self.error}" |
| 303 | + else: |
| 304 | + raise KeyError(f"No error found") |
| 305 | + |
271 | 306 | def new_contain(self, at_type: Union[str, ThingTypesBase], **contains_metadata) -> Optional['Contain']:
|
272 | 307 | """
|
273 | 308 | Adds a new element to the ``contains`` dictionary.
|
@@ -346,6 +381,9 @@ def __init__(self, error_obj: Optional[Union[bytes, str, dict]] = None) -> None:
|
346 | 381 | self.message: str = ''
|
347 | 382 | self.stackTrace: str = ''
|
348 | 383 | super().__init__(error_obj)
|
| 384 | + |
| 385 | + def __str__(self): |
| 386 | + return f"({self.message})\n\n{self.stackTrace}" |
349 | 387 |
|
350 | 388 |
|
351 | 389 | class Contain(DataDict[str, str]):
|
|
0 commit comments