openapp_sdk_common/error.rs
1//! Wire-level error envelope shared with the `OpenApp` backend.
2//!
3//! The backend surfaces non-2xx responses as JSON bodies shaped like
4//! [`ApiErrorResponse`] (see `apps/backend/local_server/src/api_error.rs`).
5
6use serde::{Deserialize, Serialize};
7
8/// JSON body returned by the `OpenApp` API for non-2xx responses.
9#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
10pub struct ApiErrorResponse {
11 /// Short machine-friendly error code (e.g. `"unauthorized"`, `"validation_error"`).
12 #[serde(default)]
13 pub code: Option<String>,
14
15 /// Human-readable message suitable for surfacing directly to the user.
16 pub message: String,
17
18 /// Optional structured details payload.
19 #[serde(default)]
20 pub details: Option<serde_json::Value>,
21
22 /// Server-generated correlation id, when present.
23 #[serde(default, rename = "correlationId")]
24 pub correlation_id: Option<String>,
25}
26
27impl ApiErrorResponse {
28 /// Build a synthetic envelope when the backend returned a non-2xx response whose body
29 /// was not JSON (e.g. a bare `502` from a proxy). This keeps downstream error surfaces
30 /// uniform.
31 #[must_use]
32 pub fn synthesize(message: impl Into<String>) -> Self {
33 Self {
34 code: None,
35 message: message.into(),
36 details: None,
37 correlation_id: None,
38 }
39 }
40}