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