feat(host): unify typed failure results

Represent denied, timeout, unavailable, and error host outcomes with one HostFailure result shape, update the browser adapter, and keep Workout host recovery flowing through app code before UI effects.

req: host/002

req: host/005

req: local/003
This commit is contained in:
slhx agent
2026-06-12 07:58:44 +02:00
parent cc97330bd1
commit 21100aafdf
4 changed files with 109 additions and 27 deletions
+7 -7
View File
@@ -5,19 +5,19 @@
return keys.length === 1 ? { kind: keys[0], data: value[keys[0]] || {} } : null;
}
function failure(id, message) {
return { Failed: { id: id || null, message: String(message) } };
function failure(id, capability, kind, message) {
return { Failed: { id: id || null, capability: capability || null, kind, message: String(message) } };
}
async function haptic(data) {
if (!navigator.vibrate) return failure(data.id, "haptics unsupported");
if (!navigator.vibrate) return failure(data.id, "Haptics", "Unavailable", "haptics unsupported");
const pattern = data.pattern === "Warning" || data.pattern === "Error" ? [30, 40, 30] : 20;
navigator.vibrate(pattern);
return { Acknowledged: { id: data.id } };
}
async function share(data) {
if (!navigator.share) return failure(data.id, "share unsupported");
if (!navigator.share) return failure(data.id, "Share", "Unavailable", "share unsupported");
const payload = data.payload || {};
const request = {};
if (payload.title) request.title = payload.title;
@@ -28,7 +28,7 @@
return { ShareCompleted: { id: data.id, completed: true } };
} catch (error) {
if (error && error.name === "AbortError") return { ShareCompleted: { id: data.id, completed: false } };
return failure(data.id, error && error.message ? error.message : error);
return failure(data.id, "Share", "Error", error && error.message ? error.message : error);
}
}
@@ -39,10 +39,10 @@
async function perform(call) {
const request = variant(call);
if (!request) return failure(null, "invalid host call");
if (!request) return failure(null, null, "Error", "invalid host call");
if (request.kind === "Haptic") return haptic(request.data);
if (request.kind === "Share") return share(request.data);
return failure(request.data.id || null, `unsupported host call ${request.kind}`);
return failure(request.data.id || null, null, "Unavailable", `unsupported host call ${request.kind}`);
}
window.hemxBrowserHost = Object.freeze({ name: "browser-pwa", supports, perform });
+71 -7
View File
@@ -381,6 +381,47 @@ impl HostCall {
}
}
/// Boring, typed host failure classes that app code can handle before it emits UI.
/// req: host/002 req: host/005
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum HostFailureKind {
PermissionDenied,
Timeout,
Unavailable,
Error,
}
/// One host failure result shape for denied, timeout, unavailable, and error cases.
/// req: host/002 req: host/005
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct HostFailure {
pub id: Option<HostCallId>,
pub capability: Option<Capability>,
pub kind: HostFailureKind,
pub message: String,
}
impl HostFailure {
pub fn new(kind: HostFailureKind, message: impl Into<String>) -> Self {
Self {
id: None,
capability: None,
kind,
message: message.into(),
}
}
pub fn with_id(mut self, id: HostCallId) -> Self {
self.id = Some(id);
self
}
pub fn with_capability(mut self, capability: Capability) -> Self {
self.capability = Some(capability);
self
}
}
/// Facts and results produced by a host adapter.
///
/// App code decides what a host event means for the product/domain before any
@@ -390,17 +431,11 @@ pub enum HostEvent {
Acknowledged {
id: HostCallId,
},
Failed {
id: Option<HostCallId>,
message: String,
},
Failed(HostFailure),
ShareCompleted {
id: HostCallId,
completed: bool,
},
PermissionDenied {
capability: Capability,
},
StreamChunk {
stream: HostStreamId,
bytes: Vec<u8>,
@@ -442,6 +477,8 @@ mod tests {
assert!(
BROWSER_HOST_JS.contains("return { ShareCompleted: { id: data.id, completed: true } }")
);
assert!(BROWSER_HOST_JS.contains("kind, message"));
assert!(BROWSER_HOST_JS.contains("\"Unavailable\""));
assert!(!BROWSER_HOST_JS.contains("querySelector"));
assert!(!BROWSER_HOST_JS.contains("innerHTML"));
assert!(!BROWSER_HOST_JS.contains("classList"));
@@ -520,6 +557,33 @@ mod tests {
}
}
#[test]
fn host_failures_use_one_typed_result_shape() {
// req: host/002 req: host/005
let denied = HostEvent::Failed(
HostFailure::new(HostFailureKind::PermissionDenied, "share denied")
.with_id(HostCallId::new("share-1"))
.with_capability(Capability::Share),
);
let timeout = HostEvent::Failed(
HostFailure::new(HostFailureKind::Timeout, "share timed out")
.with_id(HostCallId::new("share-1"))
.with_capability(Capability::Share),
);
let unavailable = HostEvent::Failed(
HostFailure::new(HostFailureKind::Unavailable, "share unsupported")
.with_capability(Capability::Share),
);
let error = HostEvent::Failed(HostFailure::new(HostFailureKind::Error, "host crashed"));
for event in [denied, timeout, unavailable, error] {
match event {
HostEvent::Failed(failure) => assert!(!failure.message.is_empty()),
other => panic!("unexpected host event shape: {other:?}"),
}
}
}
#[test]
fn web_pwa_host_result_routes_through_app_code_before_hemx_effect() {
// req: host/001 req: host/002 req: host/005