Skip to content

Commit

Permalink
chore: replace PipenvCmdError base class to OSError
Browse files Browse the repository at this point in the history
We never really made any usage of click's `ClickException.show()`.
Hence, it is ok to just get read of the ClickException base class.
  • Loading branch information
oz123 committed Dec 12, 2024
1 parent 1ba2737 commit 8d36feb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
27 changes: 12 additions & 15 deletions pipenv/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,19 @@ def show(self, file=None):
console.print(f"{self.message}")


class PipenvCmdError(PipenvException):
def __init__(self, cmd, out="", err="", exit_code=1):
class PipenvCmdError(OSError):
def __init__(self, cmd, stdout, stderr, return_code):
self.cmd = cmd
self.out = out
self.err = err
self.exit_code = exit_code
message = f"Error running command: {cmd}"
PipenvException.__init__(self, message)

def show(self, file=None):
console = Console(stderr=True, file=file, highlight=False)
console.print(f"[red]Error running command:[/red] [bold]$ {self.cmd}[/bold]")
if self.out:
console.print(f"OUTPUT: {self.out}")
if self.err:
console.print(f"STDERR: {self.err}")
self.stdout = stdout
self.stderr = stderr
self.return_code = return_code

def __str__(self):
return (
f"Command {self.cmd!r} failed with return code {self.return_code}.\n"
f"Output: {self.stdout}\n"
f"Error: {self.stderr}"
)


class JSONParseError(PipenvException):
Expand Down
3 changes: 3 additions & 0 deletions pipenv/routines/uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ def _uninstall_from_environment(project: Project, package, system=False):
package,
"-y",
]

c = run_command(cmd, is_verbose=project.s.is_verbose())
console.print("[cyan]c.stdout[/cyan]")

if c.returncode != 0:
console.print(f"Error occurred while uninstalling package {package}.")
console.print(c.stderr)
return False
return True

Expand Down
2 changes: 1 addition & 1 deletion pipenv/routines/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def get_reverse_dependencies(project) -> Dict[str, Set[Tuple[str, str]]]:

c = run_command(cmd_args, is_verbose=project.s.is_verbose())
if c.returncode != 0:
raise PipenvCmdError(c.err, c.out, c.returncode)
raise PipenvCmdError(c.cmdify(), c.err, c.out, c.returncode)
try:
dep_tree = json.loads(c.stdout.strip())
except json.JSONDecodeError:
Expand Down
3 changes: 2 additions & 1 deletion pipenv/utils/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def run_command(cmd, *args, is_verbose=False, **kwargs):
if is_verbose:
err.print(f"[cyan]Command output: {c.stdout}[/cyan]")
if c.returncode and catch_exceptions:
raise PipenvCmdError(cmd.cmdify(), c.stdout, c.stderr, c.returncode)
raise PipenvCmdError(c.returncode, f"Command failed: {cmd.cmdify()}")

return c


Expand Down

0 comments on commit 8d36feb

Please sign in to comment.