|
1 | | -""" osparc ERROR CODES (OEC) |
| 1 | +"""osparc ERROR CODES (OEC) |
2 | 2 | Unique identifier of an exception instance |
3 | 3 | Intended to report a user about unexpected errors. |
4 | 4 | Unexpected exceptions can be traced by matching the |
|
7 | 7 | SEE test_error_codes for some use cases |
8 | 8 | """ |
9 | 9 |
|
| 10 | +import hashlib |
10 | 11 | import re |
11 | | -from typing import TYPE_CHECKING, Annotated |
| 12 | +import traceback |
| 13 | +from datetime import UTC, datetime |
| 14 | +from typing import Annotated, Final, TypeAlias |
12 | 15 |
|
13 | 16 | from pydantic import StringConstraints, TypeAdapter |
14 | 17 |
|
15 | | -_LABEL = "OEC:{}" |
16 | | -_PATTERN = r"OEC:\d+" |
| 18 | +_LABEL = "OEC:{fingerprint}-{timestamp}" |
17 | 19 |
|
18 | | -if TYPE_CHECKING: |
19 | | - ErrorCodeStr = str |
20 | | -else: |
21 | | - ErrorCodeStr = Annotated[ |
22 | | - str, StringConstraints(strip_whitespace=True, pattern=_PATTERN) |
23 | | - ] |
| 20 | +_LEN = 12 # chars (~48 bits) |
| 21 | +_NAMED_PATTERN = re.compile( |
| 22 | + r"OEC:(?P<fingerprint>[a-fA-F0-9]{12})-(?P<timestamp>\d{13,14})" |
| 23 | + # NOTE: timestamp limits: 13 digits (from 2001), 14 digits (good for ~500+ years) |
| 24 | +) |
| 25 | +_PATTERN = re.compile(r"OEC:[a-fA-F0-9]{12}-\d{13,14}") |
| 26 | + |
| 27 | + |
| 28 | +ErrorCodeStr: TypeAlias = Annotated[ |
| 29 | + str, StringConstraints(strip_whitespace=True, pattern=_NAMED_PATTERN) |
| 30 | +] |
| 31 | + |
| 32 | + |
| 33 | +def _create_fingerprint(exc: BaseException) -> str: |
| 34 | + """ |
| 35 | + Unique error fingerprint of the **traceback** for deduplication purposes |
| 36 | + """ |
| 37 | + tb = traceback.extract_tb(exc.__traceback__) |
| 38 | + frame_sigs = [f"{frame.name}:{frame.lineno}" for frame in tb] |
| 39 | + fingerprint = f"{type(exc).__name__}|" + "|".join(frame_sigs) |
| 40 | + # E.g. ZeroDivisionError|foo:23|main:10 |
| 41 | + return hashlib.sha256(fingerprint.encode()).hexdigest()[:_LEN] |
| 42 | + |
| 43 | + |
| 44 | +_SECS_TO_MILISECS: Final[int] = 1000 # ms |
| 45 | + |
| 46 | + |
| 47 | +def _create_timestamp() -> int: |
| 48 | + """Timestamp as milliseconds since epoch |
| 49 | + NOTE: this reduces the precission to milliseconds but it is good enough for our purpose |
| 50 | + """ |
| 51 | + ts = datetime.now(UTC).timestamp() * _SECS_TO_MILISECS |
| 52 | + return int(ts) |
24 | 53 |
|
25 | 54 |
|
26 | 55 | def create_error_code(exception: BaseException) -> ErrorCodeStr: |
27 | | - return TypeAdapter(ErrorCodeStr).validate_python(_LABEL.format(id(exception))) |
| 56 | + """ |
| 57 | + Generates a unique error code for the given exception. |
| 58 | +
|
| 59 | + The error code follows the format: `OEC:{traceback}-{timestamp}`. |
| 60 | + This code is intended to be shared with the front-end as a `SupportID` |
| 61 | + for debugging and support purposes. |
| 62 | + """ |
| 63 | + return TypeAdapter(ErrorCodeStr).validate_python( |
| 64 | + _LABEL.format( |
| 65 | + fingerprint=_create_fingerprint(exception), |
| 66 | + timestamp=_create_timestamp(), |
| 67 | + ) |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +def parse_error_codes(obj) -> list[ErrorCodeStr]: |
| 72 | + return TypeAdapter(list[ErrorCodeStr]).validate_python(_PATTERN.findall(f"{obj}")) |
28 | 73 |
|
29 | 74 |
|
30 | | -def parse_error_code(obj) -> set[ErrorCodeStr]: |
31 | | - return set(re.findall(_PATTERN, f"{obj}")) |
| 75 | +def parse_error_code_parts(oec: ErrorCodeStr) -> tuple[str, datetime]: |
| 76 | + """Returns traceback-fingerprint and timestamp from `OEC:{traceback}-{timestamp}`""" |
| 77 | + match = _NAMED_PATTERN.match(oec) |
| 78 | + if not match: |
| 79 | + msg = f"Invalid error code format: {oec}" |
| 80 | + raise ValueError(msg) |
| 81 | + fingerprint = match.group("fingerprint") |
| 82 | + timestamp = datetime.fromtimestamp( |
| 83 | + float(match.group("timestamp")) / _SECS_TO_MILISECS, tz=UTC |
| 84 | + ) |
| 85 | + return fingerprint, timestamp |
0 commit comments