-
Notifications
You must be signed in to change notification settings - Fork 665
Add specific exceptions #1025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add specific exceptions #1025
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
01b9188
Refactoring: Move exceptions to separate file.
marcel-kanter 8697f14
Added tests for ixxat interface.
marcel-kanter b9bea05
Introduced CanBackEndError for exceptions related to the backend.
marcel-kanter 938e440
Raise an CanOperationError when an exception occurs in the send metho…
marcel-kanter 07326ef
Change intentions to 4 spaces to match remote.
marcel-kanter d5be879
Skip the test if there is an ImportError
marcel-kanter e9c7096
Merged in suggested changes from pull request.
marcel-kanter 5d40159
Introduced CanTimeoutError.
marcel-kanter e29c162
Change version to 4.0.0-dev
hardbyte 543163d
Fix imports
marcel-kanter 3cf73c8
Update __init__.py
hardbyte a929ffe
Merge branch 'exception-handling' of https://github.com/marcel-kanter…
marcel-kanter 739e405
Merge pull request #562 from marcel-kanter/exception-handling
felixdivo 5f91225
Merge branch 'develop' into unified-exceptions
felixdivo 21f3ae7
Format code with black
felixdivo cd06b77
clean up
felixdivo 89d09ff
cleanup old unwanted changes
felixdivo bfbb0f1
improve docs
felixdivo daf6617
finalize ixxat interface
felixdivo a92928c
Format code with black
felixdivo 8d0d20a
finalize adding specific exceptions to the generic part of the library
felixdivo dcdb3c1
Merge branch 'unified-exceptions' of github.com:hardbyte/python-can i…
felixdivo 65d5bd2
clarify CanOperationError
felixdivo bcf75cb
add error code to CanError
felixdivo 4e0ae77
improve docs
felixdivo 48b2acf
simplify IXXAT interface test
felixdivo ba1b9e9
rename to CanInterfaceNotImplementedError
felixdivo 6f65d77
Merge branch 'develop' into unified-exceptions
mergify[bot] d193bf2
cleanup rename to CanInterfaceNotImplementedError
felixdivo 50e1507
fix IXXAT test
felixdivo cbe3fb2
defer adjusting test_vector_error_pickle
felixdivo 47057a1
Merge branch 'develop' into unified-exceptions
felixdivo f685678
update IXXAT to new exceptions
felixdivo 7c2228d
fix linter issue
felixdivo 91824da
Merge branch 'develop' into unified-exceptions
mergify[bot] 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| """ | ||
| There are several specific :class:`Exception` classes to allow user | ||
| code to react to specific scenarios related to CAN busses:: | ||
|
|
||
| Exception (Python standard library) | ||
| +-- ... | ||
| +-- CanError (python-can) | ||
| +-- CanInterfaceNotImplementedError | ||
| +-- CanInterfaceNotImplementedError | ||
| +-- CanOperationError | ||
| +-- CanTimeoutError | ||
|
|
||
| Keep in mind that some functions and methods may raise different exceptions. | ||
| For example, validating typical arguments and parameters might result in a | ||
| :class:`ValueError`. This should always be documented for the function at hand. | ||
| """ | ||
|
|
||
| from typing import Optional | ||
|
|
||
|
|
||
| class CanError(Exception): | ||
| """Base class for all CAN related exceptions. | ||
|
|
||
| If specified, the error code is automatically prepended to the message: | ||
felixdivo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| >>> # With an error code (it also works with a specific error): | ||
| >>> error = CanOperationError(message="Failed to do the thing", error_code=42) | ||
| >>> str(error) | ||
| 'Failed to do the thing [Error Code 42]' | ||
| >>> | ||
| >>> # Missing the error code: | ||
| >>> plain_error = CanError(message="Something went wrong ...") | ||
| >>> str(plain_error) | ||
| 'Something went wrong ...' | ||
|
|
||
| :param error_code: | ||
| An optional error code to narrow down the cause of the fault | ||
|
|
||
| :arg error_code: | ||
| An optional error code to narrow down the cause of the fault | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| message: str = "", | ||
| error_code: Optional[int] = None, | ||
| ) -> None: | ||
| self.error_code = error_code | ||
| super().__init__( | ||
| message if error_code is None else f"{message} [Error Code {error_code}]" | ||
| ) | ||
|
|
||
|
|
||
| class CanInterfaceNotImplementedError(CanError, NotImplementedError): | ||
| """Indicates that the interface is not supported on the current platform. | ||
|
|
||
| Example scenarios: | ||
| - No interface with that name exists | ||
| - The interface is unsupported on the current operating system or interpreter | ||
| - The driver could not be found or has the wrong version | ||
| """ | ||
|
|
||
|
|
||
| class CanInitializationError(CanError): | ||
| """Indicates an error the occurred while initializing a :class:`can.BusABC`. | ||
|
|
||
| If initialization fails due to a driver or platform missing/being unsupported, | ||
| a :class:`can.CanInterfaceNotImplementedError` is raised instead. | ||
| If initialization fails due to a value being out of range, a :class:`ValueError` | ||
| is raised. | ||
|
|
||
| Example scenarios: | ||
| - Try to open a non-existent device and/or channel | ||
| - Try to use an invalid setting, which is ok by value, but not ok for the interface | ||
| - The device or other resources are already used | ||
| """ | ||
|
|
||
|
|
||
| class CanOperationError(CanError): | ||
| """Indicates an error while in operation. | ||
|
|
||
| Example scenarios: | ||
| - A call to a library function results in an unexpected return value | ||
| - An invalid message was received | ||
| - The driver rejected a message that was meant to be sent | ||
| - Cyclic redundancy check (CRC) failed | ||
| - A message remained unacknowledged | ||
| """ | ||
|
|
||
|
|
||
| class CanTimeoutError(CanError, TimeoutError): | ||
| """Indicates the timeout of an operation. | ||
|
|
||
| Example scenarios: | ||
| - Some message could not be sent after the timeout elapsed | ||
| - No message was read within the given time | ||
| """ | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.