Implement error reporting in IPC
Browse files at this commit0931447ec178ed503ff0a085896ec6908cf1292eParent: a388c253 changed files +45 -28
- niri-ipc/src/lib.rs +17 -5
- src/ipc/client.rs +13 -14
- src/ipc/server.rs +15 -9
niri-ipc/src/lib.rsmodified+17 -5
| @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize}; | ||
| 10 | 10 | pub const SOCKET_PATH_ENV: &str = "NIRI_SOCKET"; |
| 11 | 11 | |
| 12 | 12 | /// Request from client to niri. |
| 13 | -#[derive(Debug, Serialize, Deserialize)] | |
| 13 | +#[derive(Debug, Serialize, Deserialize, Clone)] | |
| 14 | 14 | pub enum Request { |
| 15 | 15 | /// Request information about connected outputs. |
| 16 | 16 | Outputs, |
| @@ -18,9 +18,21 @@ pub enum Request { | ||
| 18 | 18 | Action(Action), |
| 19 | 19 | } |
| 20 | 20 | |
| 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)] | |
| 23 | 33 | pub enum Response { |
| 34 | + /// A request that does not need a response was handled successfully. | |
| 35 | + Handled, | |
| 24 | 36 | /// Information about connected outputs. |
| 25 | 37 | /// |
| 26 | 38 | /// Map from connector name to output info. |
| @@ -30,7 +42,7 @@ pub enum Response { | ||
| 30 | 42 | /// Actions that niri can perform. |
| 31 | 43 | // Variants in this enum should match the spelling of the ones in niri-config. Most, but not all, |
| 32 | 44 | // variants from niri-config should be present here. |
| 33 | -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] | |
| 45 | +#[derive(Serialize, Deserialize, Debug, Clone)] | |
| 34 | 46 | #[cfg_attr(feature = "clap", derive(clap::Parser))] |
| 35 | 47 | #[cfg_attr(feature = "clap", command(subcommand_value_name = "ACTION"))] |
| 36 | 48 | #[cfg_attr(feature = "clap", command(subcommand_help_heading = "Actions"))] |
| @@ -205,7 +217,7 @@ pub enum SizeChange { | ||
| 205 | 217 | } |
| 206 | 218 | |
| 207 | 219 | /// Layout to switch to. |
| 208 | -#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)] | |
| 220 | +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)] | |
| 209 | 221 | pub enum LayoutSwitchTarget { |
| 210 | 222 | /// The next configured layout. |
| 211 | 223 | Next, |
src/ipc/client.rsmodified+13 -14
| @@ -3,8 +3,8 @@ use std::io::{Read, Write}; | ||
| 3 | 3 | use std::net::Shutdown; |
| 4 | 4 | use std::os::unix::net::UnixStream; |
| 5 | 5 | |
| 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}; | |
| 8 | 8 | |
| 9 | 9 | use crate::cli::Msg; |
| 10 | 10 | |
| @@ -36,20 +36,15 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | ||
| 36 | 36 | .read_to_end(&mut buf) |
| 37 | 37 | .context("error reading IPC response")?; |
| 38 | 38 | |
| 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")?; | |
| 46 | 44 | |
| 47 | - let response = serde_json::from_slice(&buf).context("error parsing IPC response")?; | |
| 48 | 45 | match msg { |
| 49 | 46 | Msg::Outputs => { |
| 50 | - #[allow(irrefutable_let_patterns)] | |
| 51 | - let Response::Outputs(outputs) = response | |
| 52 | - else { | |
| 47 | + let Response::Outputs(outputs) = response else { | |
| 53 | 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 | 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 | 114 | Ok(()) |
src/ipc/server.rsmodified+15 -9
| @@ -110,7 +110,19 @@ async fn handle_client(ctx: ClientCtx, stream: Async<'_, UnixStream>) -> anyhow: | ||
| 110 | 110 | .await |
| 111 | 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 | 127 | let response = match request { |
| 116 | 128 | Request::Outputs => { |
| @@ -122,15 +134,9 @@ async fn handle_client(ctx: ClientCtx, stream: Async<'_, UnixStream>) -> anyhow: | ||
| 122 | 134 | ctx.event_loop.insert_idle(move |state| { |
| 123 | 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")?; | |
| 130 | - write | |
| 131 | - .write_all(&buf) | |
| 132 | - .await | |
| 133 | - .context("error writing response")?; | |
| 134 | - | |
| 135 | - Ok(()) | |
| 141 | + Ok(response) | |
| 136 | 142 | } |