##// END OF EJS Templates
rhg: extract function handle_stdout_error...
Antoine Cezar -
r45925:10c36ead default
parent child Browse files
Show More
@@ -34,38 +34,23 b' impl Ui {'
34 34 pub fn write_stdout(&self, bytes: &[u8]) -> Result<(), UiError> {
35 35 let mut stdout = self.stdout.lock();
36 36
37 self.write_stream(&mut stdout, bytes)
38 .or_else(|e| self.handle_stdout_error(e))?;
39
40 stdout.flush().or_else(|e| self.handle_stdout_error(e))
41 }
37 stdout
38 .write_all(bytes)
39 .or_else(|e| handle_stdout_error(e))?;
42 40
43 /// Sometimes writing to stdout is not possible, try writing to stderr to
44 /// signal that failure, otherwise just bail.
45 fn handle_stdout_error(&self, error: io::Error) -> Result<(), UiError> {
46 self.write_stderr(
47 &[b"abort: ", error.to_string().as_bytes(), b"\n"].concat(),
48 )?;
49 Err(UiError::StdoutError(error))
41 stdout.flush().or_else(|e| handle_stdout_error(e))
50 42 }
51 43
52 44 /// Write bytes to stderr
53 45 pub fn write_stderr(&self, bytes: &[u8]) -> Result<(), UiError> {
54 46 let mut stderr = self.stderr.lock();
55 47
56 self.write_stream(&mut stderr, bytes)
57 .or_else(|e| Err(UiError::StderrError(e)))?;
48 stderr
49 .write_all(bytes)
50 .or_else(|e| handle_stderr_error(e))?;
58 51
59 52 stderr.flush().or_else(|e| Err(UiError::StderrError(e)))
60 53 }
61
62 fn write_stream(
63 &self,
64 stream: &mut impl Write,
65 bytes: &[u8],
66 ) -> Result<(), io::Error> {
67 stream.write_all(bytes)
68 }
69 54 }
70 55
71 56 /// A buffered stdout writer for faster batch printing operations.
@@ -81,29 +66,31 b' impl<W: Write> StdoutBuffer<W> {'
81 66
82 67 /// Write bytes to stdout buffer
83 68 pub fn write_all(&mut self, bytes: &[u8]) -> Result<(), UiError> {
84 self.buf.write_all(bytes).or_else(|e| self.io_err(e))
69 self.buf
70 .write_all(bytes)
71 .or_else(|e| handle_stdout_error(e))
85 72 }
86 73
87 74 /// Flush bytes to stdout
88 75 pub fn flush(&mut self) -> Result<(), UiError> {
89 self.buf.flush().or_else(|e| self.io_err(e))
90 }
91
92 fn io_err(&self, error: io::Error) -> Result<(), UiError> {
93 if let ErrorKind::BrokenPipe = error.kind() {
94 // This makes `| head` work for example
95 return Ok(());
96 }
97 let mut stderr = io::stderr();
98
99 stderr
100 .write_all(
101 &[b"abort: ", error.to_string().as_bytes(), b"\n"].concat(),
102 )
103 .map_err(|e| UiError::StderrError(e))?;
104
105 stderr.flush().map_err(|e| UiError::StderrError(e))?;
106
107 Err(UiError::StdoutError(error))
76 self.buf.flush().or_else(|e| handle_stdout_error(e))
108 77 }
109 78 }
79
80 /// Sometimes writing to stdout is not possible, try writing to stderr to
81 /// signal that failure, otherwise just bail.
82 fn handle_stdout_error(error: io::Error) -> Result<(), UiError> {
83 if let ErrorKind::BrokenPipe = error.kind() {
84 // This makes `| head` work for example
85 return Ok(());
86 }
87 let mut stderr = io::stderr();
88
89 stderr
90 .write_all(&[b"abort: ", error.to_string().as_bytes(), b"\n"].concat())
91 .map_err(|e| UiError::StderrError(e))?;
92
93 stderr.flush().map_err(|e| UiError::StderrError(e))?;
94
95 Err(UiError::StdoutError(error))
96 }
General Comments 0
You need to be logged in to leave comments. Login now