Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
satella
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
public
satella
Commits
808af992
Commit
808af992
authored
5 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
exceptions fixed
parent
85d13eee
No related branches found
Branches containing commit
Tags
v2.5.9
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
docs/exceptions.rst
+27
-0
27 additions, 0 deletions
docs/exceptions.rst
satella/exceptions.py
+22
-0
22 additions, 0 deletions
satella/exceptions.py
tests/test_exceptions.py
+12
-6
12 additions, 6 deletions
tests/test_exceptions.py
with
61 additions
and
6 deletions
docs/exceptions.rst
+
27
−
0
View file @
808af992
...
...
@@ -36,6 +36,33 @@ Example:
assert isinstance(BaseCodedError('message', 2', Code2Error)
The exceptions have to be in a common hierarchy to check them via
codes.
For example, the following is true:
::
class DifferentHierarchy(CodedCustomException):
pass
assert not isinstance(DifferentHierarchy('message', 2), Code2Error)
In general, please do not construct direct exceptions from CodedCustomException,
please do it via a hierarchy such as:
::
class GenericError(CodedCustomException):
pass
class SpecificError(GenericError):
code = 3
raise SpecificError('message')
Note that this won't allow you to handle exceptions like that
::
...
...
This diff is collapsed.
Click to expand it.
satella/exceptions.py
+
22
−
0
View file @
808af992
...
...
@@ -39,6 +39,13 @@ class CustomException(Exception):
return
a
def
get_base_of_bases
(
classes
):
class_bases
=
()
for
class_
in
classes
:
class_bases
+=
class_
.
__bases__
return
class_bases
class
CodedCustomExceptionMetaclass
(
type
):
code
=
None
# type: tp.Optional[tp.Any]
...
...
@@ -46,6 +53,21 @@ class CodedCustomExceptionMetaclass(type):
if
super
().
__instancecheck__
(
instance
):
return
True
if
cls
is
CodedCustomException
:
return
super
().
__instancecheck__
(
cls
,
instance
)
class_base
=
(
cls
,
)
while
CodedCustomException
not
in
get_base_of_bases
(
class_base
)
and
class_base
:
class_base
=
get_base_of_bases
(
class_base
)
inst_base
=
(
instance
.
__class__
,
)
while
CodedCustomException
not
in
get_base_of_bases
(
inst_base
)
and
inst_base
:
inst_base
=
get_base_of_bases
(
inst_base
)
if
len
(
set
(
class_base
).
intersection
(
set
(
inst_base
)))
==
0
:
# These classes belong in different exception hierarchies
return
False
try
:
return
cls
.
code
==
instance
.
code
except
AttributeError
:
...
...
This diff is collapsed.
Click to expand it.
tests/test_exceptions.py
+
12
−
6
View file @
808af992
...
...
@@ -22,13 +22,19 @@ class TestExceptions(unittest.TestCase):
self
.
fail
()
def
test_coded_exception
(
self
):
class
Base
2
Error
(
CodedCustomException
):
code
=
2
class
BaseError
(
CodedCustomException
):
pass
exc
=
CodedCustomException
(
'
message
'
,
2
)
self
.
assertIsInstance
(
exc
,
Base2Error
)
class
Base2Error
(
BaseError
):
code
=
2
exc
=
Base2Error
(
'
message
'
)
self
.
assertIsInstance
(
BaseError
(
'
message
'
,
2
),
CodedCustomException
)
self
.
assertIsInstance
(
Base2Error
(
'
message
'
),
CodedCustomException
)
self
.
assertNotIsInstance
(
CodedCustomException
(
'
message
'
,
2
),
Base2Error
)
self
.
assertIsInstance
(
BaseError
(
'
message
'
,
2
),
Base2Error
)
self
.
assertIsInstance
(
Base2Error
(
'
message
'
),
Base2Error
)
self
.
assertIsInstance
(
exc
,
Base2Error
)
class
DifferentHierarchy
(
CodedCustomException
):
pass
self
.
assertNotIsInstance
(
DifferentHierarchy
(
'
message
'
,
2
),
Base2Error
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment