Improve error handling on Xonsh

This commit is contained in:
Ajeet D'Souza 2020-10-20 23:29:28 +05:30
parent 3d432105e0
commit 20d62524bf
3 changed files with 44 additions and 12 deletions

View File

@ -5,6 +5,10 @@
import os
{%- endif %}
import os.path
import subprocess
import sys
from subprocess import CalledProcessError
{{ SECTION }}
# Utility functions for zoxide.
@ -55,15 +59,34 @@ def __zoxide_z(keywords: [str]):
elif len(keywords) == 1 and os.path.isdir(keywords[0]):
__zoxide_cd(keywords[0])
else:
__zoxide_result = $(zoxide query -- @(keywords))[:-1]
if __zoxide_result:
__zoxide_cd(__zoxide_result)
try:
__zoxide_cmd = subprocess.run(["zoxide", "query", "--"] + keywords, check=True, stdout=subprocess.PIPE)
except CalledProcessError as e:
return e.returncode
try:
__zoxide_result = __zoxide_cmd.stdout[:-1].decode("utf-8")
except UnicodeDecodeError:
print(f"zoxide: invalid unicode in result: {__zoxide_result}", file=sys.stderr)
return 1
__zoxide_cd(__zoxide_result)
# Jump to a directory using interactive search.
def __zoxide_zi(keywords: [str]):
__zoxide_result = $(zoxide query -- @(keywords))[:-1]
if __zoxide_result:
__zoxide_cd(__zoxide_result)
try:
__zoxide_cmd = subprocess.run(["zoxide", "query", "-i", "--"] + keywords, check=True, stdout=subprocess.PIPE)
except CalledProcessError as e:
return e.returncode
try:
__zoxide_result = __zoxide_cmd.stdout[:-1].decode("utf-8")
except UnicodeDecodeError:
print(f"zoxide: invalid unicode in result: {__zoxide_result}", file=sys.stderr)
return 1
__zoxide_cd(__zoxide_result)
# Add a new entry to the database.
def __zoxide_za(args: [str]):
@ -83,9 +106,18 @@ def __zoxide_zr(args: [str]):
# Remove an entry from the database using interactive selection.
def __zoxide_zri(keywords: [str]):
__zoxide_result = $(zoxide query -- @(keywords))[:-1]
if __zoxide_result:
zoxide remove @(__zoxide_result)
try:
__zoxide_cmd = subprocess.run(["zoxide", "query", "--"] + keywords, check=True, stdout=subprocess.PIPE)
except CalledProcessError as e:
return e.returncode
try:
__zoxide_result = __zoxide_cmd.stdout[:-1].decode("utf-8")
except UnicodeDecodeError:
print(f"zoxide: invalid unicode in result: {__zoxide_result}", file=sys.stderr)
return 1
zoxide remove @(__zoxide_result)
{{ SECTION }}
# Convenient aliases for zoxide. Disable these using --no-aliases.

View File

@ -27,7 +27,7 @@ pub fn zo_exclude_dirs() -> Result<Vec<glob::Pattern>> {
.map(|path| {
let pattern = path
.to_str()
.context("invalid utf-8 sequence in _ZO_EXCLUDE_DIRS")?;
.context("invalid unicode in _ZO_EXCLUDE_DIRS")?;
glob::Pattern::new(&pattern)
.with_context(|| format!("invalid glob in _ZO_EXCLUDE_DIRS: {}", pattern))
})
@ -45,7 +45,7 @@ pub fn zo_maxage() -> Result<Rank> {
Some(maxage_osstr) => {
let maxage_str = maxage_osstr
.to_str()
.context("invalid utf-8 sequence in _ZO_MAXAGE")?;
.context("invalid unicode in _ZO_MAXAGE")?;
let maxage = maxage_str.parse::<u64>().with_context(|| {
format!("unable to parse _ZO_MAXAGE as integer: {}", maxage_str)
})?;

View File

@ -27,7 +27,7 @@ pub fn current_time() -> Result<Epoch> {
pub fn path_to_str<P: AsRef<Path>>(path: &P) -> Result<&str> {
let path = path.as_ref();
path.to_str()
.with_context(|| format!("invalid UTF-8 in path: {}", path.display()))
.with_context(|| format!("invalid unicode in path: {}", path.display()))
}
/// Resolves the absolute version of a path.