Patchwork D10110: rhg: Align config file parse error formatting with Python

login
register
mail settings
Submitter phabricator
Date March 4, 2021, 11:11 a.m.
Message ID <differential-rev-PHID-DREV-sqca5mc4ph4i6k4qyjsq-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48426/
State Superseded
Headers show

Comments

phabricator - March 4, 2021, 11:11 a.m.
SimonSapin created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D10110

AFFECTED FILES
  rust/hg-core/src/config/layer.rs
  rust/hg-core/src/errors.rs
  rust/rhg/src/error.rs
  rust/rhg/src/main.rs
  tests/test-rhg.t

CHANGE DETAILS




To: SimonSapin, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/tests/test-rhg.t b/tests/test-rhg.t
--- a/tests/test-rhg.t
+++ b/tests/test-rhg.t
@@ -46,7 +46,7 @@ 
 Deleted repository
   $ rm -rf `pwd`
   $ rhg root
-  abort: $ENOENT$: current directory
+  abort: error getting current working directory: $ENOENT$
   [255]
 
 Listing tracked files
@@ -123,7 +123,7 @@ 
   $ rhg cat -r cf8b83 file-2
   2
   $ rhg cat -r c file-2
-  abort: ambiguous revision identifier c
+  abort: ambiguous revision identifier: c
   [255]
   $ rhg cat -r d file-2
   2
diff --git a/rust/rhg/src/main.rs b/rust/rhg/src/main.rs
--- a/rust/rhg/src/main.rs
+++ b/rust/rhg/src/main.rs
@@ -198,8 +198,7 @@ 
             if !message.is_empty() {
                 // Ignore errors when writing to stderr, we’re already exiting
                 // with failure code so there’s not much more we can do.
-                let _ =
-                    ui.write_stderr(&format_bytes!(b"abort: {}\n", message));
+                let _ = ui.write_stderr(&format_bytes!(b"{}\n", message));
             }
         }
         Err(CommandError::UnsupportedFeature { message }) => {
diff --git a/rust/rhg/src/error.rs b/rust/rhg/src/error.rs
--- a/rust/rhg/src/error.rs
+++ b/rust/rhg/src/error.rs
@@ -87,7 +87,7 @@ 
         let NoRepoInCwdError { cwd } = error;
         CommandError::Abort {
             message: format_bytes!(
-                b"no repository found in '{}' (.hg not found)!",
+                b"abort: no repository found in '{}' (.hg not found)!",
                 get_bytes_from_path(cwd)
             ),
         }
@@ -108,19 +108,19 @@ 
         let ConfigParseError {
             origin,
             line,
-            bytes,
+            message,
         } = error;
         let line_message = if let Some(line_number) = line {
-            format_bytes!(b" at line {}", line_number.to_string().into_bytes())
+            format_bytes!(b":{}", line_number.to_string().into_bytes())
         } else {
             Vec::new()
         };
         CommandError::Abort {
             message: format_bytes!(
-                b"config parse error in {}{}: '{}'",
+                b"config error at {}{}: {}",
                 origin,
                 line_message,
-                bytes
+                message
             ),
         }
     }
@@ -130,11 +130,11 @@ 
     fn from((err, rev): (RevlogError, &str)) -> CommandError {
         match err {
             RevlogError::InvalidRevision => CommandError::abort(format!(
-                "invalid revision identifier {}",
+                "abort: invalid revision identifier: {}",
                 rev
             )),
             RevlogError::AmbiguousPrefix => CommandError::abort(format!(
-                "ambiguous revision identifier {}",
+                "abort: ambiguous revision identifier: {}",
                 rev
             )),
             RevlogError::Other(error) => error.into(),
diff --git a/rust/hg-core/src/errors.rs b/rust/hg-core/src/errors.rs
--- a/rust/hg-core/src/errors.rs
+++ b/rust/hg-core/src/errors.rs
@@ -78,10 +78,10 @@ 
         match self {
             HgError::Abort(explanation) => write!(f, "{}", explanation),
             HgError::IoError { error, context } => {
-                write!(f, "{}: {}", error, context)
+                write!(f, "abort: {}: {}", context, error)
             }
             HgError::CorruptedRepository(explanation) => {
-                write!(f, "corrupted repository: {}", explanation)
+                write!(f, "abort: corrupted repository: {}", explanation)
             }
             HgError::UnsupportedFeature(explanation) => {
                 write!(f, "unsupported feature: {}", explanation)
@@ -128,8 +128,12 @@ 
                 from.display(),
                 to.display()
             ),
-            IoErrorContext::CurrentDir => write!(f, "current directory"),
-            IoErrorContext::CurrentExe => write!(f, "current executable"),
+            IoErrorContext::CurrentDir => {
+                write!(f, "error getting current working directory")
+            }
+            IoErrorContext::CurrentExe => {
+                write!(f, "error getting current executable")
+            }
         }
     }
 }
diff --git a/rust/hg-core/src/config/layer.rs b/rust/hg-core/src/config/layer.rs
--- a/rust/hg-core/src/config/layer.rs
+++ b/rust/hg-core/src/config/layer.rs
@@ -9,7 +9,7 @@ 
 
 use crate::errors::{HgError, IoResultExt};
 use crate::utils::files::{get_bytes_from_path, get_path_from_bytes};
-use format_bytes::{write_bytes, DisplayBytes};
+use format_bytes::{format_bytes, write_bytes, DisplayBytes};
 use lazy_static::lazy_static;
 use regex::bytes::Regex;
 use std::collections::HashMap;
@@ -187,10 +187,15 @@ 
                     map.remove(&m[1]);
                 }
             } else {
+                let message = if bytes.starts_with(b" ") {
+                    format_bytes!(b"unexpected leading whitespace: {}", bytes)
+                } else {
+                    bytes.to_owned()
+                };
                 return Err(ConfigParseError {
                     origin: ConfigOrigin::File(src.to_owned()),
                     line: Some(index + 1),
-                    bytes: bytes.to_owned(),
+                    message,
                 }
                 .into());
             }
@@ -278,7 +283,7 @@ 
 pub struct ConfigParseError {
     pub origin: ConfigOrigin,
     pub line: Option<usize>,
-    pub bytes: Vec<u8>,
+    pub message: Vec<u8>,
 }
 
 #[derive(Debug, derive_more::From)]