|
1 | 1 | from typing import Any, Literal, NotRequired, TypeAlias, TypedDict |
2 | 2 |
|
3 | | -from replit_river.rpc import ExpectedSessionState |
| 3 | +from grpc.aio import BaseError |
| 4 | + |
| 5 | +from replit_river.rpc import ACK_BIT, ACK_BIT_TYPE, ExpectedSessionState |
| 6 | +from replit_river.v2.client_session import STREAM_CANCEL_BIT, STREAM_CANCEL_BIT_TYPE |
4 | 7 |
|
5 | 8 |
|
6 | 9 | class ControlClose(TypedDict): |
@@ -33,7 +36,39 @@ class ControlHandshakeResponse(TypedDict): |
33 | 36 | type: Literal["HANDSHAKE_RESP"] |
34 | 37 | status: HandshakeOK | HandshakeError |
35 | 38 |
|
| 39 | +# This is sent when the server encounters an internal error |
| 40 | +# i.e. an invariant has been violated |
| 41 | +class BaseErrorStructure(TypedDict): |
| 42 | + # This should be a defined literal to make sure errors are easily differentiated |
| 43 | + # code: str # Supplied by implementations |
| 44 | + # This can be any string |
| 45 | + message: str |
| 46 | + # Any extra metadata |
| 47 | + extra: NotRequired[Any] |
| 48 | + |
| 49 | +# When a client sends a malformed request. This can be |
| 50 | +# for a variety of reasons which would be included |
| 51 | +# in the message. |
| 52 | +class InvalidRequestError(BaseErrorStructure): |
| 53 | + code: Literal['INVALID_REQUEST'] |
| 54 | + |
| 55 | +# This is sent when an exception happens in the handler of a stream. |
| 56 | +class UncaughtError(BaseErrorStructure): |
| 57 | + code: Literal['UNCAUGHT_ERROR'] |
| 58 | + |
| 59 | +# This is sent when one side wishes to cancel the stream |
| 60 | +# abruptly from user-space. Handling this is up to the procedure |
| 61 | +# implementation or the caller. |
| 62 | +class CancelError(BaseErrorStructure): |
| 63 | + code: Literal['CANCEL'] |
| 64 | + |
| 65 | +ProtocolError: TypeAlias = UncaughtError | InvalidRequestError | CancelError; |
36 | 66 |
|
37 | 67 | Control: TypeAlias = ( |
38 | 68 | ControlClose | ControlAck | ControlHandshakeRequest | ControlHandshakeResponse |
39 | 69 | ) |
| 70 | + |
| 71 | +ValidPairings = ( |
| 72 | + tuple[ACK_BIT_TYPE, ControlAck] | |
| 73 | + tuple[STREAM_CANCEL_BIT_TYPE, ProtocolError] |
| 74 | +) |
0 commit comments