folk_api/response.rs
1use bytes::Bytes;
2
3/// A chunk in a streaming response from a PHP worker.
4///
5/// The worker emits a sequence ending with [`End`]. Consumers read until `End`
6/// (or the channel closes) to obtain the complete response.
7///
8/// A worker finalises a request in one of three shapes:
9///
10/// * **streaming** — one [`Headers`] chunk followed by zero or more [`Body`]
11/// chunks (the PHP `folk_write_head` / `folk_write` API);
12/// * **return** — a single [`Return`] chunk carrying the handler's return value
13/// verbatim (non-streaming handlers: gRPC, jobs, and HTTP that `return`s an
14/// array);
15/// * **error** — a single [`Error`] chunk carrying a fatal [`WorkerError`]
16/// (uncaught exception, dispatch failure, malformed response).
17///
18/// Each shape is terminated by [`End`]. Consumers interpret `Return` themselves:
19/// the HTTP plugin decodes the `{status, headers, body}` convention, while
20/// gRPC/jobs use the value as-is.
21///
22/// [`Headers`]: ResponseChunk::Headers
23/// [`Body`]: ResponseChunk::Body
24/// [`Return`]: ResponseChunk::Return
25/// [`Error`]: ResponseChunk::Error
26/// [`End`]: ResponseChunk::End
27#[derive(Debug, Clone)]
28pub enum ResponseChunk {
29 /// Response status and headers. Must be the first chunk (after optional EarlyHints).
30 ///
31 /// `headers` is an ordered list of `(name, value)` pairs — NOT a map — so a
32 /// header that legitimately repeats (notably `Set-Cookie`, whose value can
33 /// itself contain a comma in an `Expires` date) survives as distinct entries
34 /// instead of being folded into one corrupted value (folk-releases #86).
35 Headers {
36 status: u16,
37 headers: Vec<(String, String)>,
38 },
39 /// A piece of the response body.
40 Body(Bytes),
41 /// The worker's return value, passed through verbatim.
42 ///
43 /// Used by non-streaming handlers. The consumer decides how to interpret it
44 /// (the HTTP plugin decodes `{status, headers, body}`; gRPC/jobs use it
45 /// directly). Followed by [`End`](ResponseChunk::End).
46 Return(serde_json::Value),
47 /// A fatal worker error (uncaught exception / dispatch failure).
48 ///
49 /// Distinct from a business outcome, which travels as a normal
50 /// [`Return`](ResponseChunk::Return). Consumers translate this into a
51 /// transport-level failure (HTTP 500, gRPC INTERNAL). Followed by
52 /// [`End`](ResponseChunk::End).
53 Error(WorkerError),
54 /// Signals that the response is complete.
55 End,
56}
57
58/// A fatal error raised while a PHP worker handled a request.
59///
60/// Carries the human-readable message and, **in dev mode only**, the exception
61/// class and stack trace. In production `exception_class` / `stacktrace` are
62/// `None` to avoid leaking internals to clients; the full detail is still
63/// logged server-side.
64#[derive(Debug, Clone)]
65pub struct WorkerError {
66 /// Human-readable error message (the exception message, or a dispatch
67 /// failure reason).
68 pub message: String,
69 /// Fully-qualified PHP exception class (dev mode only).
70 pub exception_class: Option<String>,
71 /// Stack trace as a string (dev mode only — info leak in production).
72 pub stacktrace: Option<String>,
73}
74
75impl WorkerError {
76 /// Construct an error with just a message (no class/trace).
77 pub fn new(message: impl Into<String>) -> Self {
78 Self {
79 message: message.into(),
80 exception_class: None,
81 stacktrace: None,
82 }
83 }
84}
85
86impl std::fmt::Display for WorkerError {
87 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88 match &self.exception_class {
89 Some(class) => write!(f, "{class}: {}", self.message),
90 None => f.write_str(&self.message),
91 }
92 }
93}
94
95impl std::error::Error for WorkerError {}