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};
10 pub const SOCKET_PATH_ENV: &str = "NIRI_SOCKET";10 pub const SOCKET_PATH_ENV: &str = "NIRI_SOCKET";
11 11
12 /// Request from client to niri.12 /// Request from client to niri.
13-#[derive(Debug, Serialize, Deserialize)]13+#[derive(Debug, Serialize, Deserialize, Clone)]
14 pub enum Request {14 pub enum Request {
15 /// Request information about connected outputs.15 /// Request information about connected outputs.
16 Outputs,16 Outputs,
@@ -18,9 +18,21 @@ pub enum Request {
18 Action(Action),18 Action(Action),
19 }19 }
20 20
21-/// Response from niri to client.21+/// Reply from niri to client.
22-#[derive(Debug, Serialize, Deserialize)]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)]
23 pub enum Response {33 pub enum Response {
34+ /// A request that does not need a response was handled successfully.
35+ Handled,
24 /// Information about connected outputs.36 /// Information about connected outputs.
25 ///37 ///
26 /// Map from connector name to output info.38 /// Map from connector name to output info.
@@ -30,7 +42,7 @@ pub enum Response {
30 /// Actions that niri can perform.42 /// Actions that niri can perform.
31 // Variants in this enum should match the spelling of the ones in niri-config. Most, but not all,43 // Variants in this enum should match the spelling of the ones in niri-config. Most, but not all,
32 // variants from niri-config should be present here.44 // variants from niri-config should be present here.
33-#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]45+#[derive(Serialize, Deserialize, Debug, Clone)]
34 #[cfg_attr(feature = "clap", derive(clap::Parser))]46 #[cfg_attr(feature = "clap", derive(clap::Parser))]
35 #[cfg_attr(feature = "clap", command(subcommand_value_name = "ACTION"))]47 #[cfg_attr(feature = "clap", command(subcommand_value_name = "ACTION"))]
36 #[cfg_attr(feature = "clap", command(subcommand_help_heading = "Actions"))]48 #[cfg_attr(feature = "clap", command(subcommand_help_heading = "Actions"))]
@@ -205,7 +217,7 @@ pub enum SizeChange {
205 }217 }
206 218
207 /// Layout to switch to.219 /// Layout to switch to.
208-#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]220+#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
209 pub enum LayoutSwitchTarget {221 pub enum LayoutSwitchTarget {
210 /// The next configured layout.222 /// The next configured layout.
211 Next,223 Next,
src/ipc/client.rsmodified+13 -14
@@ -3,8 +3,8 @@ use std::io::{Read, Write};
3 use std::net::Shutdown;3 use std::net::Shutdown;
4 use std::os::unix::net::UnixStream;4 use std::os::unix::net::UnixStream;
5 5
6-use anyhow::{bail, Context};6+use anyhow::{anyhow, bail, Context};
7-use niri_ipc::{Mode, Output, Request, Response};7+use niri_ipc::{Mode, Output, Reply, Request, Response};
8 8
9 use crate::cli::Msg;9 use crate::cli::Msg;
10 10
@@ -36,20 +36,15 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> {
36 .read_to_end(&mut buf)36 .read_to_end(&mut buf)
37 .context("error reading IPC response")?;37 .context("error reading IPC response")?;
38 38
39- if matches!(msg, Msg::Action { .. }) {39+ let reply: Reply = serde_json::from_slice(&buf).context("error parsing IPC reply")?;
40- if buf.is_empty() {40+
41- return Ok(());41+ let response = reply
42- } else {42+ .map_err(|msg| anyhow!(msg))
43- bail!("unexpected response: expected no response, got {buf:?}");43+ .context("niri could not handle the request")?;
44- }
45- }
46 44
47- let response = serde_json::from_slice(&buf).context("error parsing IPC response")?;
48 match msg {45 match msg {
49 Msg::Outputs => {46 Msg::Outputs => {
50- #[allow(irrefutable_let_patterns)]47+ let Response::Outputs(outputs) = response else {
51- let Response::Outputs(outputs) = response
52- else {
53 bail!("unexpected response: expected Outputs, got {response:?}");48 bail!("unexpected response: expected Outputs, got {response:?}");
54 };49 };
55 50
@@ -109,7 +104,11 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> {
109 println!();104 println!();
110 }105 }
111 }106 }
112- Msg::Action { .. } => unreachable!(),107+ Msg::Action { .. } => {
108+ let Response::Handled = response else {
109+ bail!("unexpected response: expected Handled, got {response:?}");
110+ };
111+ }
113 }112 }
114 113
115 Ok(())114 Ok(())
src/ipc/server.rsmodified+15 -9
@@ -110,7 +110,19 @@ async fn handle_client(ctx: ClientCtx, stream: Async<'_, UnixStream>) -> anyhow:
110 .await110 .await
111 .context("error reading request")?;111 .context("error reading request")?;
112 112
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")?;
114 126
115 let response = match request {127 let response = match request {
116 Request::Outputs => {128 Request::Outputs => {
@@ -122,15 +134,9 @@ async fn handle_client(ctx: ClientCtx, stream: Async<'_, UnixStream>) -> anyhow:
122 ctx.event_loop.insert_idle(move |state| {134 ctx.event_loop.insert_idle(move |state| {
123 state.do_action(action);135 state.do_action(action);
124 });136 });
125- return Ok(());137+ Response::Handled
126 }138 }
127 };139 };
128 140
129- let buf = serde_json::to_vec(&response).context("error formatting response")?;141+ Ok(response)
130- write
131- .write_all(&buf)
132- .await
133- .context("error writing response")?;
134-
135- Ok(())
136 }142 }