| 1 |
# Copyright (C) 2014 Simon Feltman <[email protected]> |
| 2 |
# |
| 3 |
# _error.py: GError Python implementation |
| 4 |
# |
| 5 |
# This library is free software; you can redistribute it and/or |
| 6 |
# modify it under the terms of the GNU Lesser General Public |
| 7 |
# License as published by the Free Software Foundation; either |
| 8 |
# version 2.1 of the License, or (at your option) any later version. |
| 9 |
# |
| 10 |
# This library is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
# Lesser General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU Lesser General Public |
| 16 |
# License along with this library; if not, write to the Free Software |
| 17 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 |
| 18 |
# USA |
| 19 |
|
| 20 |
|
| 21 |
# NOTE: This file should not have any dependencies on introspection libs |
| 22 |
# like gi.repository.GLib because it would cause a circular dependency. |
| 23 |
# Developers wanting to use the GError class in their applications should |
| 24 |
# use gi.repository.GLib.GError |
| 25 |
|
| 26 |
from __future__ import annotations |
| 27 |
|
| 28 |
import typing |
| 29 |
|
| 30 |
if typing.TYPE_CHECKING: |
| 31 |
from typing_extensions import Self |
| 32 |
|
| 33 |
|
| 34 |
class GError(RuntimeError): |
| 35 |
message: str |
| 36 |
domain: str |
| 37 |
code: int |
| 38 |
|
| 39 |
def __init__( |
| 40 |
self, message: str = "unknown error", domain: str = "pygi-error", code: int = 0 |
| 41 |
) -> None: |
| 42 |
super().__init__(message) |
| 43 |
self.message = message |
| 44 |
self.domain = domain |
| 45 |
self.code = code |
| 46 |
|
| 47 |
def __str__(self) -> str: |
| 48 |
return f"{self.domain:s}: {self.message:s} ({self.code:d})" |
| 49 |
|
| 50 |
def __repr__(self) -> str: |
| 51 |
return f"{GError.__module__.rsplit('.', 1)[-1]:s}.{GError.__name__:s}('{self.message:s}', '{self.domain:s}', {self.code:d})" |
| 52 |
|
| 53 |
def copy(self) -> Self: |
| 54 |
return GError(self.message, self.domain, self.code) |
| 55 |
|
| 56 |
def matches(self, domain: str | int, code: int) -> bool: |
| 57 |
"""Placeholder that will be monkey patched in GLib overrides.""" |
| 58 |
raise NotImplementedError |
| 59 |
|
| 60 |
@staticmethod |
| 61 |
def new_literal(domain: int, message: str, code: int) -> Self: |
| 62 |
"""Placeholder that will be monkey patched in GLib overrides.""" |
| 63 |
raise NotImplementedError |