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