Skip to content

Commit 6087d3d

Browse files
committed
fix: exceptions
1 parent b4ac4f0 commit 6087d3d

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

questionpy_common/api/qtype.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ def sidegrade(self, question_state: str) -> str:
7878

7979

8080
class OptionsFormValidationError(QPyBaseError):
81-
def __init__(self, errors: dict[str, str]):
81+
def __init__(self, errors: dict[str, str], reason: str | None = None, temporary: bool = False): # noqa: FBT001, FBT002
8282
"""There was at least one validation error."""
8383
self.errors = errors # input element name -> error description
84-
super().__init__("Form input data could not be validated successfully.")
84+
super().__init__("Form input data could not be validated successfully.", reason=reason, temporary=temporary)
8585

8686

8787
class InvalidAttemptStateError(QPyBaseError):
@@ -105,4 +105,8 @@ class MigrationErrorKind(Enum):
105105
class MigrationError(QPyBaseError):
106106
"""The migration failed."""
107107

108-
kind = MigrationErrorKind.OTHER_ERROR
108+
def __init__(
109+
self, *args: object, kind: MigrationErrorKind, reason: str | None = None, temporary: bool = False
110+
) -> None:
111+
self.kind = kind
112+
super().__init__(*args, reason=reason, temporary=temporary)

questionpy_server/worker/runtime/messages.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,26 +261,28 @@ class ErrorType(StrEnum):
261261
message_id: ClassVar[MessageIds] = MessageIds.ERROR
262262
expected_response_id: MessageIds
263263
type: ErrorType
264+
exception_kwargs: dict[str, Any] = {}
264265
message: str | None
265-
error_data: dict[str, str] | None = None
266266

267267
original_stacktrace: str | None = None
268268
"""The original worker-side stacktrace."""
269269

270270
@classmethod
271271
def from_exception(cls, error: Exception, cause: MessageToWorker) -> "WorkerError":
272272
"""Get a WorkerError message from an exception."""
273-
error_data: dict[str, str] | None = None
273+
kwargs: dict[str, Any] = {}
274274

275275
if isinstance(error, MemoryError):
276276
error_type = WorkerError.ErrorType.MEMORY_EXCEEDED
277277
elif isinstance(error, InvalidQuestionStateError):
278278
error_type = WorkerError.ErrorType.QUESTION_STATE_INVALID
279+
kwargs = {"reason": error.reason, "temporary": error.temporary}
279280
elif isinstance(error, OptionsFormValidationError):
280281
error_type = WorkerError.ErrorType.FORM_OPTIONS_INVALID
281-
error_data = error.errors
282+
kwargs = {"errors": error.errors, "reason": error.reason, "temporary": error.temporary}
282283
elif isinstance(error, MigrationError):
283284
error_type = WorkerError.ErrorType.MIGRATION_ERROR
285+
kwargs = {"kind": error.kind, "reason": error.reason, "temporary": error.temporary}
284286
else:
285287
error_type = WorkerError.ErrorType.UNKNOWN
286288

@@ -294,7 +296,7 @@ def from_exception(cls, error: Exception, cause: MessageToWorker) -> "WorkerErro
294296
message=str(error),
295297
expected_response_id=cause.Response.message_id,
296298
original_stacktrace=original_stacktrace,
297-
error_data=error_data,
299+
exception_kwargs=kwargs,
298300
)
299301

300302
def to_exception(self, worker_name: str) -> Exception:
@@ -305,9 +307,9 @@ def to_exception(self, worker_name: str) -> Exception:
305307
elif self.type == WorkerError.ErrorType.QUESTION_STATE_INVALID:
306308
error = InvalidQuestionStateError(self.message)
307309
elif self.type == WorkerError.ErrorType.FORM_OPTIONS_INVALID:
308-
error = OptionsFormValidationError(self.error_data or {})
310+
error = OptionsFormValidationError(**self.exception_kwargs)
309311
elif self.type == WorkerError.ErrorType.MIGRATION_ERROR:
310-
error = MigrationError(self.message)
312+
error = MigrationError(self.message, **self.exception_kwargs)
311313
else:
312314
error = WorkerUnknownError(self.message, worker_name=worker_name)
313315

0 commit comments

Comments
 (0)