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 import os
{%- endif %} {%- endif %}
import os.path import os.path
import subprocess
import sys
from subprocess import CalledProcessError
{{ SECTION }} {{ SECTION }}
# Utility functions for zoxide. # Utility functions for zoxide.
@ -55,15 +59,34 @@ def __zoxide_z(keywords: [str]):
elif len(keywords) == 1 and os.path.isdir(keywords[0]): elif len(keywords) == 1 and os.path.isdir(keywords[0]):
__zoxide_cd(keywords[0]) __zoxide_cd(keywords[0])
else: else:
__zoxide_result = $(zoxide query -- @(keywords))[:-1] try:
if __zoxide_result: __zoxide_cmd = subprocess.run(["zoxide", "query", "--"] + keywords, check=True, stdout=subprocess.PIPE)
__zoxide_cd(__zoxide_result) 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. # Jump to a directory using interactive search.
def __zoxide_zi(keywords: [str]): def __zoxide_zi(keywords: [str]):
__zoxide_result = $(zoxide query -- @(keywords))[:-1] try:
if __zoxide_result: __zoxide_cmd = subprocess.run(["zoxide", "query", "-i", "--"] + keywords, check=True, stdout=subprocess.PIPE)
__zoxide_cd(__zoxide_result) 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. # Add a new entry to the database.
def __zoxide_za(args: [str]): def __zoxide_za(args: [str]):
@ -83,9 +106,18 @@ def __zoxide_zr(args: [str]):
# Remove an entry from the database using interactive selection. # Remove an entry from the database using interactive selection.
def __zoxide_zri(keywords: [str]): def __zoxide_zri(keywords: [str]):
__zoxide_result = $(zoxide query -- @(keywords))[:-1] try:
if __zoxide_result: __zoxide_cmd = subprocess.run(["zoxide", "query", "--"] + keywords, check=True, stdout=subprocess.PIPE)
zoxide remove @(__zoxide_result) 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 }} {{ SECTION }}
# Convenient aliases for zoxide. Disable these using --no-aliases. # 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| { .map(|path| {
let pattern = path let pattern = path
.to_str() .to_str()
.context("invalid utf-8 sequence in _ZO_EXCLUDE_DIRS")?; .context("invalid unicode in _ZO_EXCLUDE_DIRS")?;
glob::Pattern::new(&pattern) glob::Pattern::new(&pattern)
.with_context(|| format!("invalid glob in _ZO_EXCLUDE_DIRS: {}", pattern)) .with_context(|| format!("invalid glob in _ZO_EXCLUDE_DIRS: {}", pattern))
}) })
@ -45,7 +45,7 @@ pub fn zo_maxage() -> Result<Rank> {
Some(maxage_osstr) => { Some(maxage_osstr) => {
let maxage_str = maxage_osstr let maxage_str = maxage_osstr
.to_str() .to_str()
.context("invalid utf-8 sequence in _ZO_MAXAGE")?; .context("invalid unicode in _ZO_MAXAGE")?;
let maxage = maxage_str.parse::<u64>().with_context(|| { let maxage = maxage_str.parse::<u64>().with_context(|| {
format!("unable to parse _ZO_MAXAGE as integer: {}", maxage_str) 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> { pub fn path_to_str<P: AsRef<Path>>(path: &P) -> Result<&str> {
let path = path.as_ref(); let path = path.as_ref();
path.to_str() 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. /// Resolves the absolute version of a path.