Skip to content

Commit f18171e

Browse files
committed
Use more specific exception classes so that other exceptions won't get swaUse more specific exception classes so that other exceptions won't get swallowedllawed
1 parent 3150f47 commit f18171e

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

python-bitwise-operators/stegano/__main__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
from .bitmap import Bitmap
77
from .cli import CommandLineArguments, parse_args
8-
from .decoder import decode
9-
from .encoder import encode
8+
from .decoder import decode, DecodingError
9+
from .encoder import encode, EncodingError
1010
from .eraser import erase
1111

1212

@@ -24,5 +24,5 @@ def main(args: CommandLineArguments) -> None:
2424
if __name__ == "__main__":
2525
try:
2626
main(parse_args())
27-
except Exception as ex: # pylint: disable=W0703
27+
except (EncodingError, DecodingError) as ex:
2828
print(ex)

python-bitwise-operators/stegano/decoder.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
from .bitmap import Bitmap
1010

1111

12+
class DecodingError(Exception):
13+
pass
14+
15+
1216
def decode(bitmap: Bitmap) -> None:
1317
"""Extract a secret file from the bitmap."""
1418

1519
if bitmap.reserved_field <= 0:
16-
raise ValueError("Secret file not found in the bitmap")
20+
raise DecodingError("Secret file not found in the bitmap")
1721

1822
iterator = secret_bytes(bitmap)
1923

python-bitwise-operators/stegano/encoder.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
from .bitmap import Bitmap
99

1010

11+
class EncodingError(Exception):
12+
pass
13+
14+
1115
class SecretFile:
1216
"""Convenience class for serializing secret data."""
1317

@@ -35,7 +39,7 @@ def encode(bitmap: Bitmap, path: pathlib.Path) -> None:
3539
file = SecretFile(path)
3640

3741
if file.num_secret_bytes > bitmap.max_bytes:
38-
raise ValueError("Not enough pixels to embed a secret file")
42+
raise EncodingError("Not enough pixels to embed a secret file")
3943

4044
bitmap.reserved_field = file.size_bytes
4145
for secret_byte, eight_bytes in zip(file.secret_bytes, bitmap.byte_slices):

0 commit comments

Comments
 (0)