change_user_run/error.rs
1//! Error handling.
2
3use std::{path::PathBuf, process::ExitStatus, str::Utf8Error};
4
5/// An error that may occur when creating users or running a command as different user.
6#[derive(Debug, thiserror::Error)]
7pub enum Error {
8 /// Unable to attach to stdin of a command.
9 #[error("Unable to attach to stdin of command \"{command}\"")]
10 CommandAttachToStdin {
11 /// The command for which attaching to stdin failed.
12 command: String,
13 },
14
15 /// A command exited unsuccessfully.
16 #[error("The command \"{command}\" could not be started in the background:\n{source}")]
17 CommandBackground {
18 /// The command that could not be started in the background.
19 command: String,
20 /// The source error.
21 source: std::io::Error,
22 },
23
24 /// A command could not be executed.
25 #[error("The command \"{command}\" could not be executed:\n{source}")]
26 CommandExec {
27 /// The command that could not be executed.
28 command: String,
29 /// The source error.
30 source: std::io::Error,
31 },
32
33 /// A command exited unsuccessfully.
34 #[error(
35 "The command \"{command}\" exited with non-zero status code \"{exit_status}\":\nstderr:\n{stderr}"
36 )]
37 CommandNonZero {
38 /// The command that exited with a non-zero exit code.
39 command: String,
40 /// The exit status of `command`.
41 exit_status: ExitStatus,
42 /// The stderr of `command`.
43 stderr: String,
44 },
45
46 /// Unable to write to stdin of a command.
47 #[error("Unable to write to stdin of command \"{command}\"")]
48 CommandWriteToStdin {
49 /// The command for which writing to stdin failed.
50 command: String,
51 /// The source error.
52 source: std::io::Error,
53 },
54
55 /// An executable that is supposed to be called, is not found.
56 #[error("Unable to to find executable \"{command}\"")]
57 ExecutableNotFound {
58 /// The executable that could not be found.
59 command: String,
60 /// The source error.
61 source: which::Error,
62 },
63
64 /// An I/O error.
65 #[error("I/O error while {context}:\n{source}")]
66 Io {
67 /// The short description of the operation.
68 context: &'static str,
69
70 /// The source error.
71 source: std::io::Error,
72 },
73
74 /// An I/O error with a specific path.
75 #[error("I/O error at {path} while {context}:\n{source}")]
76 IoPath {
77 /// The file that was being accessed.
78 path: PathBuf,
79
80 /// The short description of the operation.
81 context: &'static str,
82
83 /// The source error.
84 source: std::io::Error,
85 },
86
87 /// A UTF-8 error occurred when trying to convert a byte vector to a string.
88 #[error("Converting a byte vector to a string failed while {context}:\n{source}")]
89 Utf8String {
90 /// The context in which the error occurred.
91 ///
92 /// Should complete the sentence "Converting a byte vector to a string failed while ".
93 context: String,
94 /// The source error.
95 source: Utf8Error,
96 },
97}