Skip to content

Commit f3f9b6c

Browse files
committed
Implement type-safe error codes for FSA validation using an enum. Update documentation to reflect new error code structure and usage examples, enhancing clarity for frontend integration.
1 parent 5444bb9 commit f3f9b6c

4 files changed

Lines changed: 99 additions & 10 deletions

File tree

docs/dev.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,22 @@ Errors include a `highlight` field that tells the frontend exactly which FSA ele
190190
| `accept_state` | `state_id` | Highlight the accept state indicator |
191191
| `alphabet_symbol` | `symbol` | Highlight a symbol in the alphabet |
192192

193-
## Error Codes
193+
## Error Codes (Enum)
194+
195+
Error codes are defined as a type-safe enum (`ErrorCode`) in `evaluation_function/schemas/result.py`.
196+
197+
```python
198+
from evaluation_function.schemas import ErrorCode, ValidationError
199+
200+
# Type-safe error creation
201+
error = ValidationError(
202+
message="State 'q5' does not exist",
203+
code=ErrorCode.INVALID_STATE, # Enum value
204+
severity="error"
205+
)
206+
```
207+
208+
### Available Error Codes
194209

195210
| Code | Highlight Type | Description |
196211
|------|----------------|-------------|
@@ -205,6 +220,14 @@ Errors include a `highlight` field that tells the frontend exactly which FSA ele
205220
| `DUPLICATE_TRANSITION` | transition | Non-deterministic transition |
206221
| `UNREACHABLE_STATE` | state | State not reachable from initial |
207222
| `DEAD_STATE` | state | State cannot reach accept state |
223+
| `WRONG_AUTOMATON_TYPE` | general | Wrong automaton type (NFA when DFA expected) |
224+
| `NOT_DETERMINISTIC` | general | Has non-deterministic transitions |
225+
| `NOT_COMPLETE` | general | DFA missing some transitions |
226+
| `LANGUAGE_MISMATCH` | general | Accepts wrong language |
227+
| `TEST_CASE_FAILED` | general | Failed specific test case |
228+
| `EMPTY_STATES` | general | No states defined |
229+
| `EMPTY_ALPHABET` | general | No alphabet symbols |
230+
| `EVALUATION_ERROR` | general | Internal evaluation error |
208231

209232
## Examples
210233

evaluation_function/schemas/README.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,19 @@ The `highlight` field in `ValidationError` tells the frontend which FSA element
237237
```
238238
**Frontend action:** Highlight alphabet symbol "a" in the alphabet list/panel.
239239

240-
## Error Codes
240+
## Error Codes (`ErrorCode` Enum)
241+
242+
Error codes are defined as a **type-safe enum** in `result.py`:
243+
244+
```python
245+
from evaluation_function.schemas import ErrorCode
246+
247+
# Use enum values for type safety
248+
error_code = ErrorCode.INVALID_STATE # ✅ Type-safe
249+
error_code = "INVALID_STATE" # ❌ No type checking
250+
```
251+
252+
### Available Error Codes
241253

242254
| Code | Element Type | Description |
243255
|------|--------------|-------------|
@@ -252,13 +264,27 @@ The `highlight` field in `ValidationError` tells the frontend which FSA element
252264
| `DUPLICATE_TRANSITION` | transition | Multiple transitions for (state, symbol) |
253265
| `UNREACHABLE_STATE` | state | State not reachable from initial |
254266
| `DEAD_STATE` | state | State cannot reach accept state |
255-
| `WRONG_TYPE` | general | Wrong automaton type (NFA vs DFA) |
256-
| `LANGUAGE_MISMATCH` | general | Accepts wrong language |
267+
| `WRONG_AUTOMATON_TYPE` | general | Wrong automaton type (e.g., NFA when DFA expected) |
268+
| `NOT_DETERMINISTIC` | general | FSA has non-deterministic transitions |
269+
| `NOT_COMPLETE` | general | DFA missing some transitions |
270+
| `LANGUAGE_MISMATCH` | general | FSA accepts wrong language |
271+
| `TEST_CASE_FAILED` | general | Failed specific test case |
272+
| `EMPTY_STATES` | general | No states defined |
273+
| `EMPTY_ALPHABET` | general | No alphabet symbols defined |
274+
| `EVALUATION_ERROR` | general | Internal evaluation error |
257275

258276
## Usage in Evaluation Function
259277

260278
```python
261-
from evaluation_function.schemas import FSA, Answer, Params, Result, ElementHighlight, ValidationError
279+
from evaluation_function.schemas import (
280+
FSA,
281+
Answer,
282+
Params,
283+
Result,
284+
ElementHighlight,
285+
ValidationError,
286+
ErrorCode # Import the enum
287+
)
262288

263289
# Parse student response
264290
fsa = FSA(**response_data)
@@ -267,7 +293,7 @@ fsa = FSA(**response_data)
267293
if invalid_state:
268294
error = ValidationError(
269295
message=f"State '{state}' does not exist",
270-
code="INVALID_STATE",
296+
code=ErrorCode.INVALID_STATE, # Use enum value
271297
severity="error",
272298
highlight=ElementHighlight(
273299
type="state",

evaluation_function/schemas/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from .fsa import FSA, Transition
99
from .answer import Answer, TestCase
1010
from .params import Params
11-
from .result import Result, ValidationError, ElementHighlight, FSAFeedback
11+
from .result import Result, ValidationError, ElementHighlight, FSAFeedback, ErrorCode
1212

1313
__all__ = [
1414
# FSA representation
@@ -23,5 +23,6 @@
2323
"Result",
2424
"ValidationError",
2525
"ElementHighlight",
26+
"ErrorCode",
2627
"FSAFeedback",
2728
]

evaluation_function/schemas/result.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,49 @@
44
Extended result schema with structured feedback for UI highlighting.
55
"""
66

7-
from typing import List, Optional, Literal, Dict, Any
7+
from typing import List, Optional, Literal
8+
from enum import Enum
89
from pydantic import BaseModel, Field
910

1011

12+
class ErrorCode(str, Enum):
13+
"""
14+
Standardized error codes for FSA validation.
15+
16+
These codes allow the frontend to programmatically handle specific error types.
17+
"""
18+
# Structural errors - invalid elements
19+
INVALID_STATE = "INVALID_STATE"
20+
INVALID_INITIAL = "INVALID_INITIAL"
21+
INVALID_ACCEPT = "INVALID_ACCEPT"
22+
INVALID_SYMBOL = "INVALID_SYMBOL"
23+
24+
# Transition errors
25+
INVALID_TRANSITION_SOURCE = "INVALID_TRANSITION_SOURCE"
26+
INVALID_TRANSITION_DEST = "INVALID_TRANSITION_DEST"
27+
INVALID_TRANSITION_SYMBOL = "INVALID_TRANSITION_SYMBOL"
28+
MISSING_TRANSITION = "MISSING_TRANSITION"
29+
DUPLICATE_TRANSITION = "DUPLICATE_TRANSITION"
30+
31+
# Reachability errors
32+
UNREACHABLE_STATE = "UNREACHABLE_STATE"
33+
DEAD_STATE = "DEAD_STATE"
34+
35+
# Type errors
36+
WRONG_AUTOMATON_TYPE = "WRONG_AUTOMATON_TYPE"
37+
NOT_DETERMINISTIC = "NOT_DETERMINISTIC"
38+
NOT_COMPLETE = "NOT_COMPLETE"
39+
40+
# Language errors
41+
LANGUAGE_MISMATCH = "LANGUAGE_MISMATCH"
42+
TEST_CASE_FAILED = "TEST_CASE_FAILED"
43+
44+
# General errors
45+
EMPTY_STATES = "EMPTY_STATES"
46+
EMPTY_ALPHABET = "EMPTY_ALPHABET"
47+
EVALUATION_ERROR = "EVALUATION_ERROR"
48+
49+
1150
class ElementHighlight(BaseModel):
1251
"""
1352
Specifies which FSA element to highlight in the UI.
@@ -73,9 +112,9 @@ class ValidationError(BaseModel):
73112
"""
74113
message: str = Field(..., description="Human-readable error message")
75114

76-
code: str = Field(
115+
code: ErrorCode = Field(
77116
...,
78-
description="Machine-readable error code (e.g., 'MISSING_TRANSITION', 'INVALID_STATE')"
117+
description="Standardized error code from ErrorCode enum"
79118
)
80119

81120
severity: Literal["error", "warning", "info"] = Field(

0 commit comments

Comments
 (0)