diff rust/hg-core/src/errors.rs @ 47413:6e49769b7f97

rhg: add exit code to HgError::Abort() My previous attempts to have rhg end with correct exit code was more of bug hunting. I found cases which were failing and fixed them. But as one might expect, more tests started failing. Let's add exit code `HgError::Abort()` and make it users explicitly tell what exit code they want. Differential Revision: https://phab.mercurial-scm.org/D10838
author Pulkit Goyal <7895pulkit@gmail.com>
date Mon, 07 Jun 2021 17:27:49 +0530
parents bcdcb4423ae3
children cf5f8da2244c
line wrap: on
line diff
--- a/rust/hg-core/src/errors.rs	Mon Jun 07 17:19:46 2021 +0530
+++ b/rust/hg-core/src/errors.rs	Mon Jun 07 17:27:49 2021 +0530
@@ -1,4 +1,5 @@
 use crate::config::ConfigValueParseError;
+use crate::exit_codes;
 use std::fmt;
 
 /// Common error cases that can happen in many different APIs
@@ -27,9 +28,12 @@
 
     /// Operation cannot proceed for some other reason.
     ///
-    /// The given string is a short explanation for users, not intended to be
+    /// The message is a short explanation for users, not intended to be
     /// machine-readable.
-    Abort(String),
+    Abort {
+        message: String,
+        detailed_exit_code: exit_codes::ExitCode,
+    },
 
     /// A configuration value is not in the expected syntax.
     ///
@@ -69,8 +73,15 @@
     pub fn unsupported(explanation: impl Into<String>) -> Self {
         HgError::UnsupportedFeature(explanation.into())
     }
-    pub fn abort(explanation: impl Into<String>) -> Self {
-        HgError::Abort(explanation.into())
+
+    pub fn abort(
+        explanation: impl Into<String>,
+        exit_code: exit_codes::ExitCode,
+    ) -> Self {
+        HgError::Abort {
+            message: explanation.into(),
+            detailed_exit_code: exit_code,
+        }
     }
 }
 
@@ -78,7 +89,7 @@
 impl fmt::Display for HgError {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match self {
-            HgError::Abort(explanation) => write!(f, "{}", explanation),
+            HgError::Abort { message, .. } => write!(f, "{}", message),
             HgError::IoError { error, context } => {
                 write!(f, "abort: {}: {}", context, error)
             }