-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
29 lines (23 loc) · 868 Bytes
/
Copy pathexceptions.py
File metadata and controls
29 lines (23 loc) · 868 Bytes
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
class CustomError(Exception):
def __init__(self, message, code=None):
super().__init__(message)
self.code = code
class NotFoundError(CustomError):
def __init__(self, resource):
super().__init__(f'{resource} not found', code=404)
class InvalidInputError(CustomError):
def __init__(self, details):
super().__init__(f'Invalid input: {details}', code=400)
class UnauthorizedAccessError(CustomError):
def __init__(self):
super().__init__('Unauthorized access', code=403)
# Example function demonstrating error handling
def fetch_resource(resource_id):
resources = {1: 'Item 1', 2: 'Item 2'}
if resource_id not in resources:
raise NotFoundError(resource_id)
return resources[resource_id]
try:
print(fetch_resource(3))
except CustomError as e:
print(f'Error: {e}, Code: {e.code}')