Show More
@@ -1,151 +1,160 b'' | |||
|
1 | 1 | use crate::exitcode; |
|
2 | 2 | use crate::ui::utf8_to_local; |
|
3 | 3 | use crate::ui::UiError; |
|
4 | 4 | use format_bytes::format_bytes; |
|
5 | 5 | use hg::errors::HgError; |
|
6 | 6 | use hg::operations::FindRootError; |
|
7 | 7 | use hg::requirements::RequirementsError; |
|
8 | 8 | use hg::revlog::revlog::RevlogError; |
|
9 | 9 | use hg::utils::files::get_bytes_from_path; |
|
10 | 10 | use std::convert::From; |
|
11 | 11 | use std::path::PathBuf; |
|
12 | 12 | |
|
13 | 13 | /// The kind of command error |
|
14 | 14 | #[derive(Debug, derive_more::From)] |
|
15 | 15 | pub enum CommandError { |
|
16 | 16 | /// The root of the repository cannot be found |
|
17 | 17 | RootNotFound(PathBuf), |
|
18 | 18 | /// The current directory cannot be found |
|
19 | 19 | CurrentDirNotFound(std::io::Error), |
|
20 | 20 | /// `.hg/requires` |
|
21 | 21 | #[from] |
|
22 | 22 | RequirementsError(RequirementsError), |
|
23 | 23 | /// The standard output stream cannot be written to |
|
24 | 24 | StdoutError, |
|
25 | 25 | /// The standard error stream cannot be written to |
|
26 | 26 | StderrError, |
|
27 | 27 | /// The command aborted |
|
28 | 28 | Abort(Option<Vec<u8>>), |
|
29 | 29 | /// A mercurial capability as not been implemented. |
|
30 | 30 | Unimplemented, |
|
31 | 31 | /// Common cases |
|
32 | 32 | #[from] |
|
33 | 33 | Other(HgError), |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | impl CommandError { |
|
37 | 37 | pub fn get_exit_code(&self) -> exitcode::ExitCode { |
|
38 | 38 | match self { |
|
39 | 39 | CommandError::RootNotFound(_) => exitcode::ABORT, |
|
40 | 40 | CommandError::CurrentDirNotFound(_) => exitcode::ABORT, |
|
41 | 41 | CommandError::RequirementsError( |
|
42 | 42 | RequirementsError::Unsupported { .. }, |
|
43 | 43 | ) => exitcode::UNIMPLEMENTED_COMMAND, |
|
44 | 44 | CommandError::RequirementsError(_) => exitcode::ABORT, |
|
45 | 45 | CommandError::StdoutError => exitcode::ABORT, |
|
46 | 46 | CommandError::StderrError => exitcode::ABORT, |
|
47 | 47 | CommandError::Abort(_) => exitcode::ABORT, |
|
48 | 48 | CommandError::Unimplemented => exitcode::UNIMPLEMENTED_COMMAND, |
|
49 | 49 | CommandError::Other(HgError::UnsupportedFeature(_)) => { |
|
50 | 50 | exitcode::UNIMPLEMENTED_COMMAND |
|
51 | 51 | } |
|
52 | 52 | CommandError::Other(_) => exitcode::ABORT, |
|
53 | 53 | } |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | /// Return the message corresponding to the error if any |
|
57 | 57 | pub fn get_error_message_bytes(&self) -> Option<Vec<u8>> { |
|
58 | 58 | match self { |
|
59 | 59 | CommandError::RootNotFound(path) => { |
|
60 | 60 | let bytes = get_bytes_from_path(path); |
|
61 | 61 | Some(format_bytes!( |
|
62 | 62 | b"abort: no repository found in '{}' (.hg not found)!\n", |
|
63 | 63 | bytes.as_slice() |
|
64 | 64 | )) |
|
65 | 65 | } |
|
66 | 66 | CommandError::CurrentDirNotFound(e) => Some(format_bytes!( |
|
67 | 67 | b"abort: error getting current working directory: {}\n", |
|
68 | 68 | e.to_string().as_bytes(), |
|
69 | 69 | )), |
|
70 | 70 | CommandError::RequirementsError(RequirementsError::Corrupted) => { |
|
71 | 71 | Some( |
|
72 | 72 | "abort: .hg/requires is corrupted\n".as_bytes().to_owned(), |
|
73 | 73 | ) |
|
74 | 74 | } |
|
75 | 75 | CommandError::Abort(message) => message.to_owned(), |
|
76 | _ => None, | |
|
76 | ||
|
77 | CommandError::RequirementsError(_) | |
|
78 | | CommandError::StdoutError | |
|
79 | | CommandError::StderrError | |
|
80 | | CommandError::Unimplemented | |
|
81 | | CommandError::Other(HgError::UnsupportedFeature(_)) => None, | |
|
82 | ||
|
83 | CommandError::Other(e) => { | |
|
84 | Some(format_bytes!(b"{}\n", e.to_string().as_bytes())) | |
|
85 | } | |
|
77 | 86 | } |
|
78 | 87 | } |
|
79 | 88 | |
|
80 | 89 | /// Exist the process with the corresponding exit code. |
|
81 | 90 | pub fn exit(&self) { |
|
82 | 91 | std::process::exit(self.get_exit_code()) |
|
83 | 92 | } |
|
84 | 93 | } |
|
85 | 94 | |
|
86 | 95 | impl From<UiError> for CommandError { |
|
87 | 96 | fn from(error: UiError) -> Self { |
|
88 | 97 | match error { |
|
89 | 98 | UiError::StdoutError(_) => CommandError::StdoutError, |
|
90 | 99 | UiError::StderrError(_) => CommandError::StderrError, |
|
91 | 100 | } |
|
92 | 101 | } |
|
93 | 102 | } |
|
94 | 103 | |
|
95 | 104 | impl From<FindRootError> for CommandError { |
|
96 | 105 | fn from(err: FindRootError) -> Self { |
|
97 | 106 | match err { |
|
98 | 107 | FindRootError::RootNotFound(path) => { |
|
99 | 108 | CommandError::RootNotFound(path) |
|
100 | 109 | } |
|
101 | 110 | FindRootError::GetCurrentDirError(e) => { |
|
102 | 111 | CommandError::CurrentDirNotFound(e) |
|
103 | 112 | } |
|
104 | 113 | } |
|
105 | 114 | } |
|
106 | 115 | } |
|
107 | 116 | |
|
108 | 117 | impl From<(RevlogError, &str)> for CommandError { |
|
109 | 118 | fn from((err, rev): (RevlogError, &str)) -> CommandError { |
|
110 | 119 | match err { |
|
111 | 120 | RevlogError::IoError(err) => CommandError::Abort(Some( |
|
112 | 121 | utf8_to_local(&format!("abort: {}\n", err)).into(), |
|
113 | 122 | )), |
|
114 | 123 | RevlogError::InvalidRevision => CommandError::Abort(Some( |
|
115 | 124 | utf8_to_local(&format!( |
|
116 | 125 | "abort: invalid revision identifier {}\n", |
|
117 | 126 | rev |
|
118 | 127 | )) |
|
119 | 128 | .into(), |
|
120 | 129 | )), |
|
121 | 130 | RevlogError::AmbiguousPrefix => CommandError::Abort(Some( |
|
122 | 131 | utf8_to_local(&format!( |
|
123 | 132 | "abort: ambiguous revision identifier {}\n", |
|
124 | 133 | rev |
|
125 | 134 | )) |
|
126 | 135 | .into(), |
|
127 | 136 | )), |
|
128 | 137 | RevlogError::UnsuportedVersion(version) => { |
|
129 | 138 | CommandError::Abort(Some( |
|
130 | 139 | utf8_to_local(&format!( |
|
131 | 140 | "abort: unsupported revlog version {}\n", |
|
132 | 141 | version |
|
133 | 142 | )) |
|
134 | 143 | .into(), |
|
135 | 144 | )) |
|
136 | 145 | } |
|
137 | 146 | RevlogError::Corrupted => { |
|
138 | 147 | CommandError::Abort(Some("abort: corrupted revlog\n".into())) |
|
139 | 148 | } |
|
140 | 149 | RevlogError::UnknowDataFormat(format) => { |
|
141 | 150 | CommandError::Abort(Some( |
|
142 | 151 | utf8_to_local(&format!( |
|
143 | 152 | "abort: unknow revlog dataformat {:?}\n", |
|
144 | 153 | format |
|
145 | 154 | )) |
|
146 | 155 | .into(), |
|
147 | 156 | )) |
|
148 | 157 | } |
|
149 | 158 | } |
|
150 | 159 | } |
|
151 | 160 | } |
General Comments 0
You need to be logged in to leave comments.
Login now