authentik.lib.sync.outgoing.exceptions

 1from json import JSONDecodeError
 2
 3from authentik.lib.sentry import SentryIgnoredException
 4
 5
 6class BaseSyncException(SentryIgnoredException):
 7    """Base class for all sync exceptions"""
 8
 9    error_prefix = "Sync error"
10    error_default = "Error communicating with remote system"
11
12    def __init__(self, response=None):
13        super().__init__()
14        self.response = response
15
16    def __str__(self):
17        if self.response is not None:
18            if hasattr(self.response, "json"):
19                try:
20                    return f"{self.error_prefix}: {self.response.json()}"
21                except JSONDecodeError:
22                    pass
23            if hasattr(self.response, "text"):
24                return f"{self.error_prefix}: {self.response.text}"
25            return f"{self.error_prefix}: {self.response}"
26        return self.error_default
27
28    def __repr__(self):
29        return self.__str__()
30
31
32class TransientSyncException(BaseSyncException):
33    """Transient sync exception which may be caused by network blips, etc"""
34
35    error_prefix = "Network error"
36    error_default = "Network error communicating with remote system"
37
38
39class NotFoundSyncException(BaseSyncException):
40    """Exception when an object was not found in the remote system"""
41
42    error_prefix = "Object not found"
43    error_default = "Object not found in remote system"
44
45
46class ObjectExistsSyncException(BaseSyncException):
47    """Exception when an object already exists in the remote system"""
48
49    error_prefix = "Object exists"
50    error_default = "Object exists in remote system"
51
52
53class BadRequestSyncException(BaseSyncException):
54    """Exception when invalid data was sent to the remote system"""
55
56    error_prefix = "Bad request"
57    error_default = "Bad request to remote system"
58
59
60class DryRunRejected(BaseSyncException):
61    """When dry_run is enabled and a provider dropped a mutating request"""
62
63    def __init__(self, url: str, method: str, body: dict):
64        super().__init__()
65        self.url = url
66        self.method = method
67        self.body = body
68
69    def __repr__(self):
70        return self.__str__()
71
72    def __str__(self):
73        return f"Dry-run rejected request: {self.method} {self.url}"
74
75
76class StopSync(BaseSyncException):
77    """Exception raised when a configuration error should stop the sync process"""
78
79    def __init__(
80        self, exc: Exception, obj: object | None = None, mapping: object | None = None
81    ) -> None:
82        self.exc = exc
83        self.obj = obj
84        self.mapping = mapping
85
86    def detail(self) -> str:
87        """Get human readable details of this error"""
88        msg = f"Error {str(self.exc)}"
89        if self.obj:
90            msg += f", caused by {self.obj}"
91        if self.mapping:
92            msg += f" (mapping {self.mapping})"
93        return msg
class BaseSyncException(authentik.lib.sentry.SentryIgnoredException):
 7class BaseSyncException(SentryIgnoredException):
 8    """Base class for all sync exceptions"""
 9
10    error_prefix = "Sync error"
11    error_default = "Error communicating with remote system"
12
13    def __init__(self, response=None):
14        super().__init__()
15        self.response = response
16
17    def __str__(self):
18        if self.response is not None:
19            if hasattr(self.response, "json"):
20                try:
21                    return f"{self.error_prefix}: {self.response.json()}"
22                except JSONDecodeError:
23                    pass
24            if hasattr(self.response, "text"):
25                return f"{self.error_prefix}: {self.response.text}"
26            return f"{self.error_prefix}: {self.response}"
27        return self.error_default
28
29    def __repr__(self):
30        return self.__str__()

Base class for all sync exceptions

BaseSyncException(response=None)
13    def __init__(self, response=None):
14        super().__init__()
15        self.response = response
error_prefix = 'Sync error'
error_default = 'Error communicating with remote system'
response
class TransientSyncException(BaseSyncException):
33class TransientSyncException(BaseSyncException):
34    """Transient sync exception which may be caused by network blips, etc"""
35
36    error_prefix = "Network error"
37    error_default = "Network error communicating with remote system"

Transient sync exception which may be caused by network blips, etc

error_prefix = 'Network error'
error_default = 'Network error communicating with remote system'
class NotFoundSyncException(BaseSyncException):
40class NotFoundSyncException(BaseSyncException):
41    """Exception when an object was not found in the remote system"""
42
43    error_prefix = "Object not found"
44    error_default = "Object not found in remote system"

Exception when an object was not found in the remote system

error_prefix = 'Object not found'
error_default = 'Object not found in remote system'
class ObjectExistsSyncException(BaseSyncException):
47class ObjectExistsSyncException(BaseSyncException):
48    """Exception when an object already exists in the remote system"""
49
50    error_prefix = "Object exists"
51    error_default = "Object exists in remote system"

Exception when an object already exists in the remote system

error_prefix = 'Object exists'
error_default = 'Object exists in remote system'
class BadRequestSyncException(BaseSyncException):
54class BadRequestSyncException(BaseSyncException):
55    """Exception when invalid data was sent to the remote system"""
56
57    error_prefix = "Bad request"
58    error_default = "Bad request to remote system"

Exception when invalid data was sent to the remote system

error_prefix = 'Bad request'
error_default = 'Bad request to remote system'
class DryRunRejected(BaseSyncException):
61class DryRunRejected(BaseSyncException):
62    """When dry_run is enabled and a provider dropped a mutating request"""
63
64    def __init__(self, url: str, method: str, body: dict):
65        super().__init__()
66        self.url = url
67        self.method = method
68        self.body = body
69
70    def __repr__(self):
71        return self.__str__()
72
73    def __str__(self):
74        return f"Dry-run rejected request: {self.method} {self.url}"

When dry_run is enabled and a provider dropped a mutating request

DryRunRejected(url: str, method: str, body: dict)
64    def __init__(self, url: str, method: str, body: dict):
65        super().__init__()
66        self.url = url
67        self.method = method
68        self.body = body
url
method
body
class StopSync(BaseSyncException):
77class StopSync(BaseSyncException):
78    """Exception raised when a configuration error should stop the sync process"""
79
80    def __init__(
81        self, exc: Exception, obj: object | None = None, mapping: object | None = None
82    ) -> None:
83        self.exc = exc
84        self.obj = obj
85        self.mapping = mapping
86
87    def detail(self) -> str:
88        """Get human readable details of this error"""
89        msg = f"Error {str(self.exc)}"
90        if self.obj:
91            msg += f", caused by {self.obj}"
92        if self.mapping:
93            msg += f" (mapping {self.mapping})"
94        return msg

Exception raised when a configuration error should stop the sync process

StopSync( exc: Exception, obj: object | None = None, mapping: object | None = None)
80    def __init__(
81        self, exc: Exception, obj: object | None = None, mapping: object | None = None
82    ) -> None:
83        self.exc = exc
84        self.obj = obj
85        self.mapping = mapping
exc
obj
mapping
def detail(self) -> str:
87    def detail(self) -> str:
88        """Get human readable details of this error"""
89        msg = f"Error {str(self.exc)}"
90        if self.obj:
91            msg += f", caused by {self.obj}"
92        if self.mapping:
93            msg += f" (mapping {self.mapping})"
94        return msg

Get human readable details of this error