:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
//! Shared gRPC-over-HTTP response helpers used by the proxy, the leader
//! backend, and the stash handler to construct error responses directly,
//! without going through a tonic codec.
use bytes::Bytes;
use http::HeaderValue;
use http_body_util::{BodyExt, Empty};
use personhog_common::grpc::NOT_APPLIED_HEADER;
use tonic::body::BoxBody;
use tonic::Code;
/// Build a gRPC error response carrying `code` and `message` in the
/// `grpc-status`/`grpc-message` headers with an empty body.
pub(crate) fn grpc_error_response(code: Code, message: &str) -> http::Response<BoxBody> {
let body = BoxBody::new(Empty::<Bytes>::new().map_err(|never| match never {}));
let mut response = http::Response::new(body);
response
.headers_mut()
.insert("content-type", HeaderValue::from_static("application/grpc"));
response
.headers_mut()
.insert("grpc-status", HeaderValue::from(code as i32));
if !message.is_empty() {
let encoded = percent_encode_grpc(message);
if let Ok(val) = encoded.parse::<HeaderValue>() {
response.headers_mut().insert("grpc-message", val);
}
}
response
}
/// Whether a response carries a non-OK `grpc-status` in its HTTP headers.
/// gRPC errors are trailers-only responses — a single HEADERS frame — so
/// their status is visible here without polling the body; a successful
/// response carries its status in the trailers and reads as not-an-error.
pub(crate) fn is_grpc_error_response(response: &http::Response<BoxBody>) -> bool {
response
.headers()
.get("grpc-status")
.and_then(|v| v.to_str().ok())
.is_some_and(|s| s != "0")
}
/// The `grpc-status` code carried in a response's HTTP headers, if any.
/// Only trailers-only (error) responses expose their status here; a
/// successful response carries its status in the trailers and yields
/// `None`.
pub(crate) fn grpc_status_code(response: &http::Response<BoxBody>) -> Option<i32> {
response
.headers()
.get("grpc-status")
.and_then(|v| v.to_str().ok())
.and_then(|s| s.parse::<i32>().ok())
}
/// Any other status may have applied the call. The slug is bounded for the label.
pub(crate) fn unapplied_refusal_reason(response: &http::Response<BoxBody>) -> Option<&str> {
if grpc_status_code(response) != Some(Code::Unavailable as i32) {
return None;
}
response
.headers()
.get(NOT_APPLIED_HEADER)
.and_then(|v| v.to_str().ok())
.map(|r| {
if r.len() <= 64
&& r.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_'))
{
r
} else {
"unknown"
}
})
}
/// Percent-encode a gRPC status message so it is safe to carry in the
/// ASCII-only `grpc-message` header.
fn percent_encode_grpc(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for b in s.bytes() {
match b {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
out.push(b as char);
}
_ => {
const HEX: &[u8; 16] = b"0123456789ABCDEF";
out.push('%');
out.push(HEX[(b >> 4) as usize] as char);
out.push(HEX[(b & 0xf) as usize] as char);
}
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
/// An error response is flagged by `is_grpc_error_response` while an
/// OK status is not — this drives the proxy's error counter and the
/// stash drain's outcome label.
#[test]
fn grpc_status_classification() {
assert!(!is_grpc_error_response(&grpc_error_response(Code::Ok, "")));
assert!(is_grpc_error_response(&grpc_error_response(
Code::NotFound,
"not found"
)));
}
#[test]
fn only_a_marked_unavailable_reads_as_unapplied() {
let marked = |code| {
let mut r = grpc_error_response(code, "Server at capacity");
r.headers_mut()
.insert(NOT_APPLIED_HEADER, HeaderValue::from_static("load_shed"));
r
};
assert_eq!(
unapplied_refusal_reason(&marked(Code::Unavailable)),
Some("load_shed")
);
assert_eq!(unapplied_refusal_reason(&marked(Code::Ok)), None);
assert_eq!(
unapplied_refusal_reason(&marked(Code::ResourceExhausted)),
None
);
assert_eq!(unapplied_refusal_reason(&marked(Code::Internal)), None);
assert_eq!(
unapplied_refusal_reason(&grpc_error_response(
Code::Unavailable,
"Database pool exhausted"
)),
None
);
}
#[test]
fn an_unsafe_reason_slug_falls_back() {
let mut resp = grpc_error_response(Code::Unavailable, "shed");
resp.headers_mut()
.insert(NOT_APPLIED_HEADER, HeaderValue::from_static("a b/c"));
assert_eq!(unapplied_refusal_reason(&resp), Some("unknown"));
}
#[test]
fn grpc_error_response_sets_status_and_message() {
let response = grpc_error_response(Code::NotFound, "person not found");
assert_eq!(
response.headers().get("grpc-status").unwrap(),
&format!("{}", Code::NotFound as i32),
);
let msg = response
.headers()
.get("grpc-message")
.unwrap()
.to_str()
.unwrap();
assert!(msg.contains("person"));
}
#[test]
fn percent_encode_grpc_preserves_safe_chars() {
assert_eq!(percent_encode_grpc("hello"), "hello");
assert_eq!(percent_encode_grpc("a-b_c.d~e"), "a-b_c.d~e");
}
#[test]
fn percent_encode_grpc_encodes_special_chars() {
assert_eq!(percent_encode_grpc("hello world"), "hello%20world");
assert_eq!(percent_encode_grpc("a/b"), "a%2Fb");
assert_eq!(percent_encode_grpc("a:b"), "a%3Ab");
}
}