File size: 1,497 Bytes
65b5532
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Structured error types for SafeSpace."""

from __future__ import annotations

from typing import Any, Dict, Optional


class SafeSpaceError(Exception):
    """Base error with a stable machine-readable code."""

    def __init__(
        self,
        code: str,
        message: str,
        *,
        status_code: int = 400,
        context: Optional[Dict[str, Any]] = None,
    ) -> None:
        super().__init__(message)
        self.code = code
        self.message = message
        self.status_code = status_code
        self.context = context or {}

    def to_payload(self) -> Dict[str, Any]:
        """Serialize the error for HTTP or test assertions."""
        return {
            "code": self.code,
            "message": self.message,
            "context": self.context,
        }


class ScenarioCorpusError(SafeSpaceError):
    """Raised when the scenario corpus is missing or invalid."""

    def __init__(self, message: str, *, context: Optional[Dict[str, Any]] = None) -> None:
        super().__init__(
            "scenario_corpus_invalid",
            message,
            status_code=500,
            context=context,
        )


class ScenarioLookupError(SafeSpaceError):
    """Raised when a requested task or scenario cannot be resolved."""

    def __init__(
        self,
        code: str,
        message: str,
        *,
        context: Optional[Dict[str, Any]] = None,
    ) -> None:
        super().__init__(code, message, status_code=422, context=context)