File: error.py

package info (click to toggle)
python-wyoming 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 376 kB
  • sloc: python: 3,025; makefile: 3
file content (34 lines) | stat: -rw-r--r-- 809 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""Error event."""

from dataclasses import dataclass
from typing import Any, Dict, Optional

from .event import Event, Eventable

_ERROR_TYPE = "error"


@dataclass
class Error(Eventable):
    """Error with text and an optional code."""

    text: str
    """Human-readable error message."""

    code: Optional[str] = None
    """Machine-readable error code."""

    @staticmethod
    def is_type(event_type: str) -> bool:
        return event_type == _ERROR_TYPE

    def event(self) -> Event:
        data: Dict[str, Any] = {"text": self.text}
        if self.code is not None:
            data["code"] = self.code

        return Event(type=_ERROR_TYPE, data=data)

    @staticmethod
    def from_event(event: Event) -> "Error":
        return Error(text=event.data["text"], code=event.data.get("code"))