forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB08aa13eAhmet Kaan Gümüş2 months ago378 commits

error.rs61 lines, 2.7 KiBHistoryRawDownload
1use crate::ai::protocol::FrameError;
2
3/// Why an AI dispatch failed.
4///
5/// The split that matters is *whose* fault it is, because that decides whether
6/// a retry can help: [`AiError::NoWorker`] and [`AiError::Timeout`] are ours to
7/// wait out, [`AiError::Remote`] is the service's considered answer and will
8/// repeat, and [`AiError::Transport`] / [`AiError::Protocol`] mean the
9/// connection or the peer's framing is broken.
10#[derive(Debug, thiserror::Error)]
11pub enum AiError {
12 /// No connected service declared this capability. Either none has dialled
13 /// in yet, or every one of them dropped.
14 #[error("no AI service registered for capability `{0}`")]
15 NoWorker(String),
16 /// Every registered worker for the capability is already at its declared
17 /// `max_concurrent`. Distinct from [`AiError::NoWorker`]: the capacity is
18 /// there, it is just busy right now, so a retry is worth making.
19 #[error("all AI services for capability `{0}` are at capacity")]
20 Busy(String),
21 /// The service did not answer within the deadline. The request may still
22 /// be executing on the far side — treat as "unknown", not "did not run".
23 #[error("AI service did not answer within {0}ms")]
24 Timeout(u64),
25 /// The service answered with a handled failure of its own.
26 #[error("AI service refused the request: {code}: {message}")]
27 Remote { code: String, message: String },
28 /// The QUIC connection or stream failed.
29 #[error("AI transport failed: {0}")]
30 Transport(String),
31 /// The peer's bytes did not form a frame we could read.
32 #[error("AI protocol violation: {0}")]
33 Protocol(#[source] FrameError),
34 /// The answer on this stream carried a different trace id than the request
35 /// written to it. Correlation does not depend on the id — the stream does
36 /// that — but a mismatch means the service has mixed up whose work this
37 /// is, so the payload cannot be trusted as this request's result.
38 #[error("AI service answered request {expected} with {got}")]
39 IdMismatch { expected: String, got: String },
40 /// The bridge could not be started at all (bind, certificate, config).
41 #[error("AI bridge setup failed: {0}")]
42 Setup(String),
43}
44
45impl From<FrameError> for AiError {
46 fn from(e: FrameError) -> Self {
47 AiError::Protocol(e)
48 }
49}
50
51impl AiError {
52 /// Is waiting and trying again plausibly useful? Used by callers that sit
53 /// on an HTTP path to decide between a 503 (come back) and a 502 (this
54 /// request is not going to work).
55 pub fn is_retryable(&self) -> bool {
56 matches!(
57 self,
58 AiError::NoWorker(_) | AiError::Busy(_) | AiError::Transport(_)
59 )
60 }
61}