-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-custom-exceptions.py
More file actions
1024 lines (794 loc) · 37.3 KB
/
03-custom-exceptions.py
File metadata and controls
1024 lines (794 loc) · 37.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Question: Create custom exception classes for better error handling and debugging.
Create a comprehensive custom exception hierarchy for a banking application
that handles various types of errors with specific information.
Requirements:
1. Create a base custom exception class
2. Create specific exception classes for different error types
3. Add custom attributes and methods to exceptions
4. Implement exception chaining and context
5. Demonstrate proper exception handling patterns
Example usage:
try:
account.withdraw(1000)
except InsufficientFundsError as e:
print(f"Error: {e}")
print(f"Available balance: {e.available_balance}")
"""
# LEARNING CHALLENGE
#
# Before looking at any solution below, please try to solve this yourself first!
#
# Tips for success:
# - Read the question carefully
# - Think about what exception classes you need
# - Start with a simple base exception
# - Test your code step by step
# - Don't worry if it's not perfect - learning is a process!
#
# Remember: The best way to learn programming is by doing, not by reading solutions!
#
# Take your time, experiment, and enjoy the learning process!
# Try to implement your solution here:
# (Write your code below this line)
# HINT SECTION (Only look if you're really stuck!)
#
# Think about:
# - What is the base exception class you need?
# - What specific banking errors can occur?
# - What additional information should each exception carry?
# - How can you chain exceptions for better debugging?
#
# Remember: Start simple and build up complexity gradually!
# ===============================================================================
# STEP-BY-STEP SOLUTION
# ===============================================================================
#
# CLASSROOM-STYLE WALKTHROUGH
#
# Let's solve this problem step by step, just like in a programming class!
# Each step builds upon the previous one, so you can follow along and understand
# the complete thought process.
#
# ===============================================================================
# Step 1: Create the base custom exception class
# ===============================================================================
# Explanation:
# Custom exceptions should inherit from built-in exception classes.
# A base exception class provides common functionality for all custom exceptions.
class BankingError(Exception):
"""Base exception class for all banking-related errors."""
def __init__(self, message: str, error_code: str = None):
super().__init__(message)
self.message = message
self.error_code = error_code
self.timestamp = self._get_timestamp()
def _get_timestamp(self):
"""Get current timestamp for error logging."""
from datetime import datetime
return datetime.now().isoformat()
def __str__(self):
"""String representation of the error."""
if self.error_code:
return f"[{self.error_code}] {self.message} (at {self.timestamp})"
return f"{self.message} (at {self.timestamp})"
# What we accomplished in this step:
# - Created base BankingError class with common attributes
# - Added timestamp for error tracking
# - Implemented custom string representation
# Step 2: Create specific exception classes with custom attributes
# ===============================================================================
# Explanation:
# Specific exceptions inherit from our base class and add domain-specific
# attributes and behavior for different types of banking errors.
class BankingError(Exception):
"""Base exception class for all banking-related errors."""
def __init__(self, message: str, error_code: str = None):
super().__init__(message)
self.message = message
self.error_code = error_code
self.timestamp = self._get_timestamp()
def _get_timestamp(self):
"""Get current timestamp for error logging."""
from datetime import datetime
return datetime.now().isoformat()
def __str__(self):
"""String representation of the error."""
if self.error_code:
return f"[{self.error_code}] {self.message} (at {self.timestamp})"
return f"{self.message} (at {self.timestamp})"
class InsufficientFundsError(BankingError):
"""Raised when account has insufficient funds for a transaction."""
def __init__(self, message: str, requested_amount: float, available_balance: float, account_id: str = None):
super().__init__(message, "INSUFFICIENT_FUNDS")
self.requested_amount = requested_amount
self.available_balance = available_balance
self.account_id = account_id
self.shortfall = requested_amount - available_balance
def __str__(self):
base_msg = super().__str__()
return f"{base_msg} - Requested: ${self.requested_amount:.2f}, Available: ${self.available_balance:.2f}"
class AccountNotFoundError(BankingError):
"""Raised when an account cannot be found."""
def __init__(self, account_id: str, search_criteria: str = None):
message = f"Account not found: {account_id}"
super().__init__(message, "ACCOUNT_NOT_FOUND")
self.account_id = account_id
self.search_criteria = search_criteria
class InvalidTransactionError(BankingError):
"""Raised when a transaction is invalid."""
def __init__(self, message: str, transaction_type: str, amount: float = None, reason: str = None):
super().__init__(message, "INVALID_TRANSACTION")
self.transaction_type = transaction_type
self.amount = amount
self.reason = reason
class AccountFrozenError(BankingError):
"""Raised when trying to perform operations on a frozen account."""
def __init__(self, account_id: str, freeze_reason: str = None):
message = f"Account {account_id} is frozen"
super().__init__(message, "ACCOUNT_FROZEN")
self.account_id = account_id
self.freeze_reason = freeze_reason
# What we accomplished in this step:
# - Created specific exception classes for different banking scenarios
# - Added domain-specific attributes (amounts, account IDs, etc.)
# - Implemented custom string representations with relevant details
# Step 3: Add exception chaining and context handling
# ===============================================================================
# Explanation:
# Exception chaining helps preserve the original error context while raising
# more specific exceptions. This is crucial for debugging complex systems.
class BankingError(Exception):
"""Base exception class for all banking-related errors."""
def __init__(self, message: str, error_code: str = None):
super().__init__(message)
self.message = message
self.error_code = error_code
self.timestamp = self._get_timestamp()
def _get_timestamp(self):
"""Get current timestamp for error logging."""
from datetime import datetime
return datetime.now().isoformat()
def __str__(self):
"""String representation of the error."""
if self.error_code:
return f"[{self.error_code}] {self.message} (at {self.timestamp})"
return f"{self.message} (at {self.timestamp})"
class InsufficientFundsError(BankingError):
"""Raised when account has insufficient funds for a transaction."""
def __init__(self, message: str, requested_amount: float, available_balance: float, account_id: str = None):
super().__init__(message, "INSUFFICIENT_FUNDS")
self.requested_amount = requested_amount
self.available_balance = available_balance
self.account_id = account_id
self.shortfall = requested_amount - available_balance
def __str__(self):
base_msg = super().__str__()
return f"{base_msg} - Requested: ${self.requested_amount:.2f}, Available: ${self.available_balance:.2f}"
class AccountNotFoundError(BankingError):
"""Raised when an account cannot be found."""
def __init__(self, account_id: str, search_criteria: str = None):
message = f"Account not found: {account_id}"
super().__init__(message, "ACCOUNT_NOT_FOUND")
self.account_id = account_id
self.search_criteria = search_criteria
class InvalidTransactionError(BankingError):
"""Raised when a transaction is invalid."""
def __init__(self, message: str, transaction_type: str, amount: float = None, reason: str = None):
super().__init__(message, "INVALID_TRANSACTION")
self.transaction_type = transaction_type
self.amount = amount
self.reason = reason
class AccountFrozenError(BankingError):
"""Raised when trying to perform operations on a frozen account."""
def __init__(self, account_id: str, freeze_reason: str = None):
message = f"Account {account_id} is frozen"
super().__init__(message, "ACCOUNT_FROZEN")
self.account_id = account_id
self.freeze_reason = freeze_reason
class DatabaseConnectionError(BankingError):
"""Raised when database operations fail."""
def __init__(self, operation: str, original_error: Exception = None):
message = f"Database operation failed: {operation}"
super().__init__(message, "DATABASE_ERROR")
self.operation = operation
self.original_error = original_error
class ValidationError(BankingError):
"""Raised when input validation fails."""
def __init__(self, field: str, value: any, validation_rule: str):
message = f"Validation failed for {field}: {validation_rule}"
super().__init__(message, "VALIDATION_ERROR")
self.field = field
self.value = value
self.validation_rule = validation_rule
# Exception chaining utility functions
def chain_exception(new_exception: BankingError, original_exception: Exception) -> BankingError:
"""Chain a new exception with the original exception for better debugging."""
new_exception.__cause__ = original_exception
return new_exception
def wrap_database_error(operation: str, original_error: Exception) -> DatabaseConnectionError:
"""Wrap database errors with banking-specific context."""
db_error = DatabaseConnectionError(operation, original_error)
db_error.__cause__ = original_error
return db_error
# What we accomplished in this step:
# - Added more specific exception types
# - Implemented exception chaining with __cause__
# - Created utility functions for exception wrapping
# - Added context preservation for debugging
# Step 4: Create a banking system to demonstrate exception usage
# ===============================================================================
# Explanation:
# Now we'll create a simple banking system that uses our custom exceptions
# to show how they work in practice with proper error handling patterns.
class BankingError(Exception):
"""Base exception class for all banking-related errors."""
def __init__(self, message: str, error_code: str = None):
super().__init__(message)
self.message = message
self.error_code = error_code
self.timestamp = self._get_timestamp()
def _get_timestamp(self):
"""Get current timestamp for error logging."""
from datetime import datetime
return datetime.now().isoformat()
def __str__(self):
"""String representation of the error."""
if self.error_code:
return f"[{self.error_code}] {self.message} (at {self.timestamp})"
return f"{self.message} (at {self.timestamp})"
class InsufficientFundsError(BankingError):
"""Raised when account has insufficient funds for a transaction."""
def __init__(self, message: str, requested_amount: float, available_balance: float, account_id: str = None):
super().__init__(message, "INSUFFICIENT_FUNDS")
self.requested_amount = requested_amount
self.available_balance = available_balance
self.account_id = account_id
self.shortfall = requested_amount - available_balance
def __str__(self):
base_msg = super().__str__()
return f"{base_msg} - Requested: ${self.requested_amount:.2f}, Available: ${self.available_balance:.2f}"
class AccountNotFoundError(BankingError):
"""Raised when an account cannot be found."""
def __init__(self, account_id: str, search_criteria: str = None):
message = f"Account not found: {account_id}"
super().__init__(message, "ACCOUNT_NOT_FOUND")
self.account_id = account_id
self.search_criteria = search_criteria
class InvalidTransactionError(BankingError):
"""Raised when a transaction is invalid."""
def __init__(self, message: str, transaction_type: str, amount: float = None, reason: str = None):
super().__init__(message, "INVALID_TRANSACTION")
self.transaction_type = transaction_type
self.amount = amount
self.reason = reason
class AccountFrozenError(BankingError):
"""Raised when trying to perform operations on a frozen account."""
def __init__(self, account_id: str, freeze_reason: str = None):
message = f"Account {account_id} is frozen"
super().__init__(message, "ACCOUNT_FROZEN")
self.account_id = account_id
self.freeze_reason = freeze_reason
class DatabaseConnectionError(BankingError):
"""Raised when database operations fail."""
def __init__(self, operation: str, original_error: Exception = None):
message = f"Database operation failed: {operation}"
super().__init__(message, "DATABASE_ERROR")
self.operation = operation
self.original_error = original_error
class ValidationError(BankingError):
"""Raised when input validation fails."""
def __init__(self, field: str, value: any, validation_rule: str):
message = f"Validation failed for {field}: {validation_rule}"
super().__init__(message, "VALIDATION_ERROR")
self.field = field
self.value = value
self.validation_rule = validation_rule
# Exception chaining utility functions
def chain_exception(new_exception: BankingError, original_exception: Exception) -> BankingError:
"""Chain a new exception with the original exception for better debugging."""
new_exception.__cause__ = original_exception
return new_exception
def wrap_database_error(operation: str, original_error: Exception) -> DatabaseConnectionError:
"""Wrap database errors with banking-specific context."""
db_error = DatabaseConnectionError(operation, original_error)
db_error.__cause__ = original_error
return db_error
# Banking system implementation
class BankAccount:
"""Simple bank account class to demonstrate exception usage."""
def __init__(self, account_id: str, initial_balance: float = 0.0):
self.account_id = self._validate_account_id(account_id)
self.balance = self._validate_amount(initial_balance, "initial_balance")
self.is_frozen = False
self.freeze_reason = None
def _validate_account_id(self, account_id: str) -> str:
"""Validate account ID format."""
if not account_id or not isinstance(account_id, str):
raise ValidationError("account_id", account_id, "must be a non-empty string")
if len(account_id) < 5:
raise ValidationError("account_id", account_id, "must be at least 5 characters long")
return account_id
def _validate_amount(self, amount: float, field_name: str) -> float:
"""Validate monetary amounts."""
if not isinstance(amount, (int, float)):
raise ValidationError(field_name, amount, "must be a number")
if amount < 0:
raise ValidationError(field_name, amount, "must be non-negative")
return float(amount)
def _check_account_status(self):
"""Check if account is available for transactions."""
if self.is_frozen:
raise AccountFrozenError(self.account_id, self.freeze_reason)
def deposit(self, amount: float) -> float:
"""Deposit money into the account."""
try:
self._check_account_status()
amount = self._validate_amount(amount, "deposit_amount")
if amount == 0:
raise InvalidTransactionError(
"Cannot deposit zero amount",
"deposit",
amount,
"Amount must be greater than zero"
)
self.balance += amount
return self.balance
except Exception as e:
if isinstance(e, BankingError):
raise
# Wrap unexpected errors
raise chain_exception(
InvalidTransactionError(f"Deposit failed: {str(e)}", "deposit", amount),
e
)
def withdraw(self, amount: float) -> float:
"""Withdraw money from the account."""
try:
self._check_account_status()
amount = self._validate_amount(amount, "withdrawal_amount")
if amount == 0:
raise InvalidTransactionError(
"Cannot withdraw zero amount",
"withdrawal",
amount,
"Amount must be greater than zero"
)
if amount > self.balance:
raise InsufficientFundsError(
f"Insufficient funds for withdrawal of ${amount:.2f}",
amount,
self.balance,
self.account_id
)
self.balance -= amount
return self.balance
except Exception as e:
if isinstance(e, BankingError):
raise
# Wrap unexpected errors
raise chain_exception(
InvalidTransactionError(f"Withdrawal failed: {str(e)}", "withdrawal", amount),
e
)
def freeze_account(self, reason: str = "Administrative hold"):
"""Freeze the account."""
self.is_frozen = True
self.freeze_reason = reason
def unfreeze_account(self):
"""Unfreeze the account."""
self.is_frozen = False
self.freeze_reason = None
class Bank:
"""Simple bank class to manage multiple accounts."""
def __init__(self):
self.accounts = {}
def create_account(self, account_id: str, initial_balance: float = 0.0) -> BankAccount:
"""Create a new bank account."""
if account_id in self.accounts:
raise InvalidTransactionError(
f"Account {account_id} already exists",
"account_creation",
reason="Duplicate account ID"
)
account = BankAccount(account_id, initial_balance)
self.accounts[account_id] = account
return account
def get_account(self, account_id: str) -> BankAccount:
"""Get an existing account."""
if account_id not in self.accounts:
raise AccountNotFoundError(account_id)
return self.accounts[account_id]
def transfer(self, from_account_id: str, to_account_id: str, amount: float):
"""Transfer money between accounts."""
try:
from_account = self.get_account(from_account_id)
to_account = self.get_account(to_account_id)
# Withdraw from source account
from_account.withdraw(amount)
try:
# Deposit to destination account
to_account.deposit(amount)
except Exception as deposit_error:
# Rollback the withdrawal if deposit fails
from_account.deposit(amount)
raise chain_exception(
InvalidTransactionError(
f"Transfer failed during deposit phase",
"transfer",
amount,
"Deposit to destination account failed"
),
deposit_error
)
except Exception as e:
if isinstance(e, BankingError):
raise
# Wrap unexpected errors
raise chain_exception(
InvalidTransactionError(f"Transfer failed: {str(e)}", "transfer", amount),
e
)
# What we accomplished in this step:
# - Created BankAccount class with proper validation
# - Implemented Bank class for account management
# - Added comprehensive error handling with custom exceptions
# - Demonstrated exception chaining and rollback scenarios
# Step 5: Comprehensive demonstration and testing examples
# ===============================================================================
# Explanation:
# This final step shows how to use all our custom exceptions in practice
# with proper error handling patterns and comprehensive testing scenarios.
from datetime import datetime
class BankingError(Exception):
"""Base exception class for all banking-related errors."""
def __init__(self, message: str, error_code: str = None):
super().__init__(message)
self.message = message
self.error_code = error_code
self.timestamp = self._get_timestamp()
def _get_timestamp(self):
"""Get current timestamp for error logging."""
from datetime import datetime
return datetime.now().isoformat()
def __str__(self):
"""String representation of the error."""
if self.error_code:
return f"[{self.error_code}] {self.message} (at {self.timestamp})"
return f"{self.message} (at {self.timestamp})"
class InsufficientFundsError(BankingError):
"""Raised when account has insufficient funds for a transaction."""
def __init__(self, message: str, requested_amount: float, available_balance: float, account_id: str = None):
super().__init__(message, "INSUFFICIENT_FUNDS")
self.requested_amount = requested_amount
self.available_balance = available_balance
self.account_id = account_id
self.shortfall = requested_amount - available_balance
def __str__(self):
base_msg = super().__str__()
return f"{base_msg} - Requested: ${self.requested_amount:.2f}, Available: ${self.available_balance:.2f}"
class AccountNotFoundError(BankingError):
"""Raised when an account cannot be found."""
def __init__(self, account_id: str, search_criteria: str = None):
message = f"Account not found: {account_id}"
super().__init__(message, "ACCOUNT_NOT_FOUND")
self.account_id = account_id
self.search_criteria = search_criteria
class InvalidTransactionError(BankingError):
"""Raised when a transaction is invalid."""
def __init__(self, message: str, transaction_type: str, amount: float = None, reason: str = None):
super().__init__(message, "INVALID_TRANSACTION")
self.transaction_type = transaction_type
self.amount = amount
self.reason = reason
class AccountFrozenError(BankingError):
"""Raised when trying to perform operations on a frozen account."""
def __init__(self, account_id: str, freeze_reason: str = None):
message = f"Account {account_id} is frozen"
super().__init__(message, "ACCOUNT_FROZEN")
self.account_id = account_id
self.freeze_reason = freeze_reason
class DatabaseConnectionError(BankingError):
"""Raised when database operations fail."""
def __init__(self, operation: str, original_error: Exception = None):
message = f"Database operation failed: {operation}"
super().__init__(message, "DATABASE_ERROR")
self.operation = operation
self.original_error = original_error
class ValidationError(BankingError):
"""Raised when input validation fails."""
def __init__(self, field: str, value: any, validation_rule: str):
message = f"Validation failed for {field}: {validation_rule}"
super().__init__(message, "VALIDATION_ERROR")
self.field = field
self.value = value
self.validation_rule = validation_rule
# Exception chaining utility functions
def chain_exception(new_exception: BankingError, original_exception: Exception) -> BankingError:
"""Chain a new exception with the original exception for better debugging."""
new_exception.__cause__ = original_exception
return new_exception
def wrap_database_error(operation: str, original_error: Exception) -> DatabaseConnectionError:
"""Wrap database errors with banking-specific context."""
db_error = DatabaseConnectionError(operation, original_error)
db_error.__cause__ = original_error
return db_error
# Banking system implementation
class BankAccount:
"""Simple bank account class to demonstrate exception usage."""
def __init__(self, account_id: str, initial_balance: float = 0.0):
self.account_id = self._validate_account_id(account_id)
self.balance = self._validate_amount(initial_balance, "initial_balance")
self.is_frozen = False
self.freeze_reason = None
def _validate_account_id(self, account_id: str) -> str:
"""Validate account ID format."""
if not account_id or not isinstance(account_id, str):
raise ValidationError("account_id", account_id, "must be a non-empty string")
if len(account_id) < 5:
raise ValidationError("account_id", account_id, "must be at least 5 characters long")
return account_id
def _validate_amount(self, amount: float, field_name: str) -> float:
"""Validate monetary amounts."""
if not isinstance(amount, (int, float)):
raise ValidationError(field_name, amount, "must be a number")
if amount < 0:
raise ValidationError(field_name, amount, "must be non-negative")
return float(amount)
def _check_account_status(self):
"""Check if account is available for transactions."""
if self.is_frozen:
raise AccountFrozenError(self.account_id, self.freeze_reason)
def deposit(self, amount: float) -> float:
"""Deposit money into the account."""
try:
self._check_account_status()
amount = self._validate_amount(amount, "deposit_amount")
if amount == 0:
raise InvalidTransactionError(
"Cannot deposit zero amount",
"deposit",
amount,
"Amount must be greater than zero"
)
self.balance += amount
return self.balance
except Exception as e:
if isinstance(e, BankingError):
raise
# Wrap unexpected errors
raise chain_exception(
InvalidTransactionError(f"Deposit failed: {str(e)}", "deposit", amount),
e
)
def withdraw(self, amount: float) -> float:
"""Withdraw money from the account."""
try:
self._check_account_status()
amount = self._validate_amount(amount, "withdrawal_amount")
if amount == 0:
raise InvalidTransactionError(
"Cannot withdraw zero amount",
"withdrawal",
amount,
"Amount must be greater than zero"
)
if amount > self.balance:
raise InsufficientFundsError(
f"Insufficient funds for withdrawal of ${amount:.2f}",
amount,
self.balance,
self.account_id
)
self.balance -= amount
return self.balance
except Exception as e:
if isinstance(e, BankingError):
raise
# Wrap unexpected errors
raise chain_exception(
InvalidTransactionError(f"Withdrawal failed: {str(e)}", "withdrawal", amount),
e
)
def freeze_account(self, reason: str = "Administrative hold"):
"""Freeze the account."""
self.is_frozen = True
self.freeze_reason = reason
def unfreeze_account(self):
"""Unfreeze the account."""
self.is_frozen = False
self.freeze_reason = None
class Bank:
"""Simple bank class to manage multiple accounts."""
def __init__(self):
self.accounts = {}
def create_account(self, account_id: str, initial_balance: float = 0.0) -> BankAccount:
"""Create a new bank account."""
if account_id in self.accounts:
raise InvalidTransactionError(
f"Account {account_id} already exists",
"account_creation",
reason="Duplicate account ID"
)
account = BankAccount(account_id, initial_balance)
self.accounts[account_id] = account
return account
def get_account(self, account_id: str) -> BankAccount:
"""Get an existing account."""
if account_id not in self.accounts:
raise AccountNotFoundError(account_id)
return self.accounts[account_id]
def transfer(self, from_account_id: str, to_account_id: str, amount: float):
"""Transfer money between accounts."""
try:
from_account = self.get_account(from_account_id)
to_account = self.get_account(to_account_id)
# Withdraw from source account
from_account.withdraw(amount)
try:
# Deposit to destination account
to_account.deposit(amount)
except Exception as deposit_error:
# Rollback the withdrawal if deposit fails
from_account.deposit(amount)
raise chain_exception(
InvalidTransactionError(
f"Transfer failed during deposit phase",
"transfer",
amount,
"Deposit to destination account failed"
),
deposit_error
)
except Exception as e:
if isinstance(e, BankingError):
raise
# Wrap unexpected errors
raise chain_exception(
InvalidTransactionError(f"Transfer failed: {str(e)}", "transfer", amount),
e
)
# Error handling utilities
class ErrorLogger:
"""Utility class for logging banking errors."""
@staticmethod
def log_error(error: BankingError, context: str = ""):
"""Log banking errors with full context."""
print(f"ERROR LOG - {context}")
print(f"Error Type: {type(error).__name__}")
print(f"Error Code: {error.error_code}")
print(f"Message: {error.message}")
print(f"Timestamp: {error.timestamp}")
# Log specific attributes based on error type
if isinstance(error, InsufficientFundsError):
print(f"Account ID: {error.account_id}")
print(f"Requested Amount: ${error.requested_amount:.2f}")
print(f"Available Balance: ${error.available_balance:.2f}")
print(f"Shortfall: ${error.shortfall:.2f}")
elif isinstance(error, AccountNotFoundError):
print(f"Account ID: {error.account_id}")
if error.search_criteria:
print(f"Search Criteria: {error.search_criteria}")
elif isinstance(error, ValidationError):
print(f"Field: {error.field}")
print(f"Value: {error.value}")
print(f"Validation Rule: {error.validation_rule}")
# Log chained exceptions
if error.__cause__:
print(f"Caused by: {type(error.__cause__).__name__}: {error.__cause__}")
print("-" * 50)
def safe_banking_operation(operation_func, *args, **kwargs):
"""Safely execute banking operations with comprehensive error handling."""
try:
return operation_func(*args, **kwargs)
except InsufficientFundsError as e:
ErrorLogger.log_error(e, "Insufficient Funds")
print(f"Transaction declined: {e}")
return None
except AccountNotFoundError as e:
ErrorLogger.log_error(e, "Account Lookup")
print(f"Account error: {e}")
return None
except AccountFrozenError as e:
ErrorLogger.log_error(e, "Account Status")
print(f"Account frozen: {e}")
print(f"Reason: {e.freeze_reason}")
return None
except ValidationError as e:
ErrorLogger.log_error(e, "Input Validation")
print(f"Validation error: {e}")
return None
except InvalidTransactionError as e:
ErrorLogger.log_error(e, "Transaction Processing")
print(f"Transaction error: {e}")
return None
except BankingError as e:
ErrorLogger.log_error(e, "General Banking")
print(f"Banking system error: {e}")
return None
except Exception as e:
# Handle unexpected errors
wrapped_error = chain_exception(
BankingError(f"Unexpected error: {str(e)}", "SYSTEM_ERROR"),
e
)
ErrorLogger.log_error(wrapped_error, "System Error")
print(f"System error: {wrapped_error}")
return None
# Demonstration and testing
def demonstrate_custom_exceptions():
"""Comprehensive demonstration of custom exception usage."""
print("=== Custom Exception Demonstration ===\n")
# Create a bank and accounts
bank = Bank()
print("1. Testing successful operations:")
try:
account1 = bank.create_account("ACC12345", 1000.0)
account2 = bank.create_account("ACC67890", 500.0)
print(f"Created account {account1.account_id} with balance ${account1.balance:.2f}")
print(f"Created account {account2.account_id} with balance ${account2.balance:.2f}")
except BankingError as e:
ErrorLogger.log_error(e, "Account Creation")
print("\n2. Testing validation errors:")
# Test invalid account ID
safe_banking_operation(bank.create_account, "123", 100.0) # Too short
# Test negative balance
safe_banking_operation(BankAccount, "ACC99999", -100.0)
print("\n3. Testing insufficient funds:")
account = bank.get_account("ACC12345")
safe_banking_operation(account.withdraw, 2000.0) # More than balance
print("\n4. Testing account not found:")
safe_banking_operation(bank.get_account, "NONEXISTENT")
print("\n5. Testing frozen account:")
account.freeze_account("Suspicious activity detected")
safe_banking_operation(account.withdraw, 100.0)
print("\n6. Testing transfer with rollback:")
account.unfreeze_account()
account2 = bank.get_account("ACC67890")
account2.freeze_account("Account under review")
safe_banking_operation(bank.transfer, "ACC12345", "ACC67890", 200.0)
print("\n7. Testing exception chaining:")
try:
# Simulate a database error during withdrawal
original_error = ConnectionError("Database connection lost")
raise wrap_database_error("account_withdrawal", original_error)
except DatabaseConnectionError as e:
ErrorLogger.log_error(e, "Database Operation")
print("\n8. Testing successful operations after fixes:")
account2.unfreeze_account()
result = safe_banking_operation(bank.transfer, "ACC12345", "ACC67890", 200.0)
if result is not None:
print(f"Transfer successful!")
print(f"Account 1 balance: ${bank.get_account('ACC12345').balance:.2f}")
print(f"Account 2 balance: ${bank.get_account('ACC67890').balance:.2f}")
# Run the demonstration
if __name__ == "__main__":
demonstrate_custom_exceptions()
# What we accomplished in this step:
# - Created comprehensive error logging system
# - Implemented safe operation wrapper with specific error handling
# - Added complete demonstration with all exception types
# - Showed proper exception chaining and context preservation
# - Demonstrated rollback scenarios and error recovery patterns