This repository was archived by the owner on Jan 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Handle jmp-client exceptions and introduce a exception handling module #293
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/jumpstarter-cli-common/jumpstarter_cli_common/exceptions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import asyncclick as click | ||
|
|
||
| from jumpstarter.common.exceptions import JumpstarterException | ||
|
|
||
|
|
||
| class ClickExceptionRed(click.ClickException): | ||
| def format_message(self) -> str: | ||
| return click.style(self.message, fg='red') | ||
|
|
||
| def async_handle_exceptions(func): | ||
| """Decorator to handle exceptions in async functions.""" | ||
| async def wrapped(*args, **kwargs): | ||
| try: | ||
| return await func(*args, **kwargs) | ||
| except JumpstarterException as e: | ||
| raise ClickExceptionRed(str(e)) from None | ||
| except click.ClickException: | ||
| raise # if it was already a click exception from the cli commands, just re-raise it | ||
| except Exception: | ||
| raise | ||
|
|
||
| return wrapped | ||
|
|
||
| def handle_exceptions(func): | ||
| """Decorator to handle exceptions in blocking functions.""" | ||
| def wrapped(*args, **kwargs): | ||
| try: | ||
| return func(*args, **kwargs) | ||
| except JumpstarterException as e: | ||
| raise ClickExceptionRed(str(e)) from None | ||
| except click.ClickException: | ||
| raise # if it was already a click exception from the cli commands, just re-raise it | ||
| except Exception: | ||
| raise | ||
|
|
||
| return wrapped | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from jumpstarter.common import exceptions | ||
|
|
||
|
|
||
| class LeaseError(exceptions.JumpstarterException): | ||
| """Raised when a lease operation fails.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import sys | ||
|
|
||
|
|
||
| class JumpstarterException(Exception): | ||
| """Base class for jumpstarter-specific errors. | ||
|
|
||
| This class should not be raised directly, but should be used as a base | ||
| class for all jumpstarter-specific errors. | ||
| It handles the __cause__ attribute so the jumpstarter errors could be raised as | ||
|
|
||
| raise SomeError("message") from original_exception | ||
| """ | ||
| def __init__(self, message:str): | ||
| super().__init__(message) | ||
| self.message = message | ||
|
|
||
| def __str__(self): | ||
| if self.__cause__: | ||
| return f"{self.message} (Caused by: {self.__cause__})" | ||
| return f"{self.message}" | ||
|
|
||
| def print(self, message:str|None = None): | ||
| ANSI_RED = "\033[91m" | ||
| ANSI_CLEAR = "\033[0m" | ||
| print(f"{ANSI_RED}{self}{ANSI_CLEAR}", file=sys.stderr) | ||
|
|
||
| class ConnectionError(JumpstarterException): | ||
| """Raised when a connection to a jumpstarter server fails.""" | ||
| pass | ||
|
|
||
| class ConfigurationError(JumpstarterException): | ||
| """Raised when a configuration error exists.""" | ||
| pass | ||
|
|
||
| class ArgumentError(JumpstarterException): | ||
| """Raised when a cli argument is not valid.""" | ||
| pass | ||
|
|
||
| class FileAccessError(JumpstarterException): | ||
| """Raised when a file access error occurs.""" | ||
| pass | ||
|
|
||
| class FileNotFoundError(JumpstarterException): | ||
| """Raised when a file is not found.""" | ||
| pass |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am just being explicit here (as I could be leaving this to the "except Exception", but I want to make it clear that if we do the Exception later in time, we need to keep raising the ClickExceptions cleanly.