rust/rhg/src/error.rs
changeset 47407 6e49769b7f97
parent 46821 e8ae91b1a63d
child 47408 7954ee2d7cf7
equal deleted inserted replaced
47406:3237ed4dcda4 47407:6e49769b7f97
     1 use crate::exitcode;
       
     2 use crate::ui::utf8_to_local;
     1 use crate::ui::utf8_to_local;
     3 use crate::ui::UiError;
     2 use crate::ui::UiError;
     4 use crate::NoRepoInCwdError;
     3 use crate::NoRepoInCwdError;
     5 use format_bytes::format_bytes;
     4 use format_bytes::format_bytes;
     6 use hg::config::{ConfigError, ConfigParseError, ConfigValueParseError};
     5 use hg::config::{ConfigError, ConfigParseError, ConfigValueParseError};
     7 use hg::errors::HgError;
     6 use hg::errors::HgError;
       
     7 use hg::exit_codes;
     8 use hg::repo::RepoError;
     8 use hg::repo::RepoError;
     9 use hg::revlog::revlog::RevlogError;
     9 use hg::revlog::revlog::RevlogError;
    10 use hg::utils::files::get_bytes_from_path;
    10 use hg::utils::files::get_bytes_from_path;
    11 use hg::{DirstateError, DirstateMapError, StatusError};
    11 use hg::{DirstateError, DirstateMapError, StatusError};
    12 use std::convert::From;
    12 use std::convert::From;
    15 #[derive(Debug)]
    15 #[derive(Debug)]
    16 pub enum CommandError {
    16 pub enum CommandError {
    17     /// Exit with an error message and "standard" failure exit code.
    17     /// Exit with an error message and "standard" failure exit code.
    18     Abort {
    18     Abort {
    19         message: Vec<u8>,
    19         message: Vec<u8>,
    20         detailed_exit_code: exitcode::ExitCode,
    20         detailed_exit_code: exit_codes::ExitCode,
    21     },
    21     },
    22 
    22 
    23     /// Exit with a failure exit code but no message.
    23     /// Exit with a failure exit code but no message.
    24     Unsuccessful,
    24     Unsuccessful,
    25 
    25 
    30     UnsupportedFeature { message: Vec<u8> },
    30     UnsupportedFeature { message: Vec<u8> },
    31 }
    31 }
    32 
    32 
    33 impl CommandError {
    33 impl CommandError {
    34     pub fn abort(message: impl AsRef<str>) -> Self {
    34     pub fn abort(message: impl AsRef<str>) -> Self {
    35         CommandError::abort_with_exit_code(message, exitcode::ABORT)
    35         CommandError::abort_with_exit_code(message, exit_codes::ABORT)
    36     }
    36     }
    37 
    37 
    38     pub fn abort_with_exit_code(
    38     pub fn abort_with_exit_code(
    39         message: impl AsRef<str>,
    39         message: impl AsRef<str>,
    40         detailed_exit_code: exitcode::ExitCode,
    40         detailed_exit_code: exit_codes::ExitCode,
    41     ) -> Self {
    41     ) -> Self {
    42         CommandError::Abort {
    42         CommandError::Abort {
    43             // TODO: bytes-based (instead of Unicode-based) formatting
    43             // TODO: bytes-based (instead of Unicode-based) formatting
    44             // of error messages to handle non-UTF-8 filenames etc:
    44             // of error messages to handle non-UTF-8 filenames etc:
    45             // https://www.mercurial-scm.org/wiki/EncodingStrategy#Mixing_output
    45             // https://www.mercurial-scm.org/wiki/EncodingStrategy#Mixing_output
    76 
    76 
    77 impl From<ConfigValueParseError> for CommandError {
    77 impl From<ConfigValueParseError> for CommandError {
    78     fn from(error: ConfigValueParseError) -> Self {
    78     fn from(error: ConfigValueParseError) -> Self {
    79         CommandError::abort_with_exit_code(
    79         CommandError::abort_with_exit_code(
    80             error.to_string(),
    80             error.to_string(),
    81             exitcode::CONFIG_ERROR_ABORT,
    81             exit_codes::CONFIG_ERROR_ABORT,
    82         )
    82         )
    83     }
    83     }
    84 }
    84 }
    85 
    85 
    86 impl From<UiError> for CommandError {
    86 impl From<UiError> for CommandError {
    98             RepoError::NotFound { at } => CommandError::Abort {
    98             RepoError::NotFound { at } => CommandError::Abort {
    99                 message: format_bytes!(
    99                 message: format_bytes!(
   100                     b"abort: repository {} not found",
   100                     b"abort: repository {} not found",
   101                     get_bytes_from_path(at)
   101                     get_bytes_from_path(at)
   102                 ),
   102                 ),
   103                 detailed_exit_code: exitcode::ABORT,
   103                 detailed_exit_code: exit_codes::ABORT,
   104             },
   104             },
   105             RepoError::ConfigParseError(error) => error.into(),
   105             RepoError::ConfigParseError(error) => error.into(),
   106             RepoError::Other(error) => error.into(),
   106             RepoError::Other(error) => error.into(),
   107         }
   107         }
   108     }
   108     }
   114         CommandError::Abort {
   114         CommandError::Abort {
   115             message: format_bytes!(
   115             message: format_bytes!(
   116                 b"abort: no repository found in '{}' (.hg not found)!",
   116                 b"abort: no repository found in '{}' (.hg not found)!",
   117                 get_bytes_from_path(cwd)
   117                 get_bytes_from_path(cwd)
   118             ),
   118             ),
   119             detailed_exit_code: exitcode::ABORT,
   119             detailed_exit_code: exit_codes::ABORT,
   120         }
   120         }
   121     }
   121     }
   122 }
   122 }
   123 
   123 
   124 impl From<ConfigError> for CommandError {
   124 impl From<ConfigError> for CommandError {
   147                 b"config error at {}{}: {}",
   147                 b"config error at {}{}: {}",
   148                 origin,
   148                 origin,
   149                 line_message,
   149                 line_message,
   150                 message
   150                 message
   151             ),
   151             ),
   152             detailed_exit_code: exitcode::CONFIG_ERROR_ABORT,
   152             detailed_exit_code: exit_codes::CONFIG_ERROR_ABORT,
   153         }
   153         }
   154     }
   154     }
   155 }
   155 }
   156 
   156 
   157 impl From<(RevlogError, &str)> for CommandError {
   157 impl From<(RevlogError, &str)> for CommandError {