forge
Explore
Log in

Don't you dare go Hollow

Implement error reporting in IPC

Browse files at this commit

0931447ec178ed503ff0a085896ec6908cf1292eIvan MolodetskikhParent: a388c25
niri-ipc/src/lib.rsmodified+17 -5
@@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
1010 pub const SOCKET_PATH_ENV: &str = "NIRI_SOCKET";
1111
1212 /// Request from client to niri.
13-#[derive(Debug, Serialize, Deserialize)]
13+#[derive(Debug, Serialize, Deserialize, Clone)]
1414 pub enum Request {
1515 /// Request information about connected outputs.
1616 Outputs,
@@ -18,9 +18,21 @@ pub enum Request {
1818 Action(Action),
1919 }
2020
21-/// Response from niri to client.
22-#[derive(Debug, Serialize, Deserialize)]
21+/// Reply from niri to client.
22+///
23+/// Every request gets one reply.
24+///
25+/// * If an error had occurred, it will be an `Reply::Err`.
26+/// * If the request does not need any particular response, it will be
27+/// `Reply::Ok(Response::Handled)`. Kind of like an `Ok(())`.
28+/// * Otherwise, it will be `Reply::Ok(response)` with one of the other [`Response`] variants.
29+pub type Reply = Result<Response, String>;
30+
31+/// Successful response from niri to client.
32+#[derive(Debug, Serialize, Deserialize, Clone)]
2333 pub enum Response {
34+ /// A request that does not need a response was handled successfully.
35+ Handled,
2436 /// Information about connected outputs.
2537 ///
2638 /// Map from connector name to output info.
@@ -30,7 +42,7 @@ pub enum Response {
3042 /// Actions that niri can perform.
3143 // Variants in this enum should match the spelling of the ones in niri-config. Most, but not all,
3244 // variants from niri-config should be present here.
33-#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
45+#[derive(Serialize, Deserialize, Debug, Clone)]
3446 #[cfg_attr(feature = "clap", derive(clap::Parser))]
3547 #[cfg_attr(feature = "clap", command(subcommand_value_name = "ACTION"))]
3648 #[cfg_attr(feature = "clap", command(subcommand_help_heading = "Actions"))]
@@ -205,7 +217,7 @@ pub enum SizeChange {
205217 }
206218
207219 /// Layout to switch to.
208-#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
220+#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
209221 pub enum LayoutSwitchTarget {
210222 /// The next configured layout.
211223 Next,
src/ipc/client.rsmodified+13 -14
@@ -3,8 +3,8 @@ use std::io::{Read, Write};
33 use std::net::Shutdown;
44 use std::os::unix::net::UnixStream;
55
6-use anyhow::{bail, Context};
7-use niri_ipc::{Mode, Output, Request, Response};
6+use anyhow::{anyhow, bail, Context};
7+use niri_ipc::{Mode, Output, Reply, Request, Response};
88
99 use crate::cli::Msg;
1010
@@ -36,20 +36,15 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> {
3636 .read_to_end(&mut buf)
3737 .context("error reading IPC response")?;
3838
39- if matches!(msg, Msg::Action { .. }) {
40- if buf.is_empty() {
41- return Ok(());
42- } else {
43- bail!("unexpected response: expected no response, got {buf:?}");
44- }
45- }
39+ let reply: Reply = serde_json::from_slice(&buf).context("error parsing IPC reply")?;
40+
41+ let response = reply
42+ .map_err(|msg| anyhow!(msg))
43+ .context("niri could not handle the request")?;
4644
47- let response = serde_json::from_slice(&buf).context("error parsing IPC response")?;
4845 match msg {
4946 Msg::Outputs => {
50- #[allow(irrefutable_let_patterns)]
51- let Response::Outputs(outputs) = response
52- else {
47+ let Response::Outputs(outputs) = response else {
5348 bail!("unexpected response: expected Outputs, got {response:?}");
5449 };
5550
@@ -109,7 +104,11 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> {
109104 println!();
110105 }
111106 }
112- Msg::Action { .. } => unreachable!(),
107+ Msg::Action { .. } => {
108+ let Response::Handled = response else {
109+ bail!("unexpected response: expected Handled, got {response:?}");
110+ };
111+ }
113112 }
114113
115114 Ok(())
src/ipc/server.rsmodified+15 -9
@@ -110,7 +110,19 @@ async fn handle_client(ctx: ClientCtx, stream: Async<'_, UnixStream>) -> anyhow:
110110 .await
111111 .context("error reading request")?;
112112
113- let request: Request = serde_json::from_str(&buf).context("error parsing request")?;
113+ let reply = process(&ctx, &buf).map_err(|err| {
114+ warn!("error processing IPC request: {err:?}");
115+ err.to_string()
116+ });
117+
118+ let buf = serde_json::to_vec(&reply).context("error formatting reply")?;
119+ write.write_all(&buf).await.context("error writing reply")?;
120+
121+ Ok(())
122+}
123+
124+fn process(ctx: &ClientCtx, buf: &str) -> anyhow::Result<Response> {
125+ let request: Request = serde_json::from_str(buf).context("error parsing request")?;
114126
115127 let response = match request {
116128 Request::Outputs => {
@@ -122,15 +134,9 @@ async fn handle_client(ctx: ClientCtx, stream: Async<'_, UnixStream>) -> anyhow:
122134 ctx.event_loop.insert_idle(move |state| {
123135 state.do_action(action);
124136 });
125- return Ok(());
137+ Response::Handled
126138 }
127139 };
128140
129- let buf = serde_json::to_vec(&response).context("error formatting response")?;
130- write
131- .write_all(&buf)
132- .await
133- .context("error writing response")?;
134-
135- Ok(())
141+ Ok(response)
136142 }