Skip to content

Commit 3e5d677

Browse files
refactor: Refactorized the exceptions
1 parent d84f6fe commit 3e5d677

File tree

2 files changed

+37
-48
lines changed

2 files changed

+37
-48
lines changed

diracx-core/src/diracx/core/exceptions.py

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class DiracError(RuntimeError):
1515

1616
def __init__(self, detail: str = "Unknown"):
1717
self.detail = detail
18+
super().__init__(detail)
1819

1920

2021
class AuthorizationError(DiracError): ...
@@ -53,19 +54,19 @@ class InvalidQueryError(DiracError):
5354

5455

5556
class TokenNotFoundError(DiracError):
56-
def __init__(self, jti: str, detail: str | None = None):
57+
def __init__(self, jti: str, detail: str = ""):
5758
self.jti: str = jti
5859
super().__init__(f"Token {jti} not found" + (f" ({detail})" if detail else ""))
5960

6061

6162
class JobNotFoundError(DiracError):
62-
def __init__(self, job_id: int, detail: str | None = None):
63+
def __init__(self, job_id: int, detail: str = ""):
6364
self.job_id: int = job_id
6465
super().__init__(f"Job {job_id} not found" + (f" ({detail})" if detail else ""))
6566

6667

6768
class SandboxNotFoundError(DiracError):
68-
def __init__(self, pfn: str, se_name: str, detail: str | None = None):
69+
def __init__(self, pfn: str, se_name: str, detail: str = ""):
6970
self.pfn: str = pfn
7071
self.se_name: str = se_name
7172
super().__init__(
@@ -75,7 +76,7 @@ def __init__(self, pfn: str, se_name: str, detail: str | None = None):
7576

7677

7778
class SandboxAlreadyAssignedError(DiracError):
78-
def __init__(self, pfn: str, se_name: str, detail: str | None = None):
79+
def __init__(self, pfn: str, se_name: str, detail: str = ""):
7980
self.pfn: str = pfn
8081
self.se_name: str = se_name
8182
super().__init__(
@@ -85,7 +86,7 @@ def __init__(self, pfn: str, se_name: str, detail: str | None = None):
8586

8687

8788
class SandboxAlreadyInsertedError(DiracError):
88-
def __init__(self, pfn: str, se_name: str, detail: str | None = None):
89+
def __init__(self, pfn: str, se_name: str, detail: str = ""):
8990
self.pfn: str = pfn
9091
self.se_name: str = se_name
9192
super().__init__(
@@ -95,7 +96,7 @@ def __init__(self, pfn: str, se_name: str, detail: str | None = None):
9596

9697

9798
class JobError(DiracError):
98-
def __init__(self, job_id, detail: str | None = None):
99+
def __init__(self, job_id, detail: str = ""):
99100
self.job_id: int = job_id
100101
super().__init__(
101102
f"Error concerning job {job_id}" + (f" ({detail})" if detail else "")
@@ -106,81 +107,69 @@ class NotReadyError(DiracError):
106107
"""Tried to access a value which is asynchronously loaded but not yet available."""
107108

108109

109-
class GenericError(Exception):
110-
head: str = "Error"
111-
tail: str = ""
110+
class DiracFormattedError(DiracError):
111+
# TODO: Refactor?
112+
pattern = "Error %s"
112113

113-
def __init__(self, data: dict[str, str], detail: str | None = None):
114+
def __init__(self, data: dict[str, str], detail: str = ""):
114115
self.data = data
115-
self.detail = detail
116116

117117
parts = [f"({key}: {value})" for key, value in data.items()]
118-
message = f"{self.head} {' '.join(parts)} {self.tail}"
118+
message = type(self).pattern % (" ".join(parts))
119119
if detail:
120120
message += f": {detail}"
121121

122122
super().__init__(message)
123123

124124

125-
class PilotNotFoundError(GenericError):
126-
head = "Pilot"
127-
tail = "not found"
125+
class PilotNotFoundError(DiracFormattedError):
126+
pattern = "Pilot %s not found"
128127

129128
def __init__(
130129
self,
131130
data: dict[str, str],
132-
detail: str | None = None,
131+
detail: str = "",
133132
non_existing_pilots: set = set(),
134133
):
135134
super().__init__(data, detail)
136135
self.non_existing_pilots = non_existing_pilots
137136

138137

139-
class PilotAlreadyExistsError(GenericError):
140-
head = "Pilot"
141-
tail = "already exists"
138+
class PilotAlreadyExistsError(DiracFormattedError):
139+
pattern = "Pilot %s already exists"
142140

143141

144-
class BadPilotCredentialsError(GenericError):
145-
head = "Bad secret/pilot_stamp"
146-
tail = ""
142+
class BadPilotCredentialsError(DiracFormattedError):
143+
pattern = "Bad secret/pilot_stamp %s "
147144

148145

149-
class BadPilotVOError(GenericError):
150-
head = "Bad VO"
151-
tail = ""
146+
class BadPilotVOError(DiracFormattedError):
147+
pattern = "Bad VO %s "
152148

153149

154-
class SecretNotFoundError(GenericError):
155-
head = "Secret"
156-
tail = "not found"
150+
class SecretNotFoundError(DiracFormattedError):
151+
pattern = "Secret %s not found"
157152

158153

159-
class CredentialsNotFoundError(GenericError):
160-
head = "Credentials"
161-
tail = "not found"
154+
class CredentialsNotFoundError(DiracFormattedError):
155+
pattern = "Credentials %s not found"
162156

163157

164-
class CredentialsAlreadyExistError(GenericError):
165-
head = "Credentials"
166-
tail = "already exist"
158+
class CredentialsAlreadyExistError(DiracFormattedError):
159+
pattern = "Credentials %s already exist"
167160

168161

169-
class SecretHasExpiredError(GenericError):
170-
head = "Secret"
171-
tail = "has expired"
162+
class SecretHasExpiredError(DiracFormattedError):
163+
pattern = "Secret %s has expired"
172164

173165

174-
class SecretAlreadyExistsError(GenericError):
175-
head = "Secret"
176-
tail = "already exists"
166+
class SecretAlreadyExistsError(DiracFormattedError):
167+
pattern = "Secret %s already exists"
177168

178169

179-
class PilotJobsNotFoundError(GenericError):
180-
head = "Pilots or Jobs"
181-
tail = "not found"
170+
class PilotJobsNotFoundError(DiracFormattedError):
171+
pattern = "Pilots or Jobs %s not found"
182172

183173

184-
class PilotAlreadyAssociatedWithJobError(GenericError):
185-
head = "Pilot is already associated with a job"
186-
tail = ""
174+
class PilotAlreadyAssociatedWithJobError(DiracFormattedError):
175+
pattern = "Pilot is already associated with a job %s "

diracx-db/src/diracx/db/sql/utils/functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from sqlalchemy.ext.compiler import compiles
1010
from sqlalchemy.sql import ColumnElement, expression
1111

12-
from diracx.core.exceptions import GenericError, InvalidQueryError
12+
from diracx.core.exceptions import DiracFormattedError, InvalidQueryError
1313
from diracx.db.exceptions import DBInBadStateError
1414

1515
if TYPE_CHECKING:
@@ -160,7 +160,7 @@ def hash(code: str):
160160
async def fetch_records_bulk_or_raises(
161161
conn: AsyncConnection,
162162
model: Any, # Here, we currently must use `Any` because `declarative_base()` returns any
163-
missing_elements_error_cls: Type[GenericError],
163+
missing_elements_error_cls: Type[DiracFormattedError],
164164
column_attribute_name: str,
165165
column_name: str,
166166
elements_to_fetch: list,

0 commit comments

Comments
 (0)