-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathexceptions.py
More file actions
88 lines (64 loc) · 2.33 KB
/
exceptions.py
File metadata and controls
88 lines (64 loc) · 2.33 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
"""
Exceptions module for DataFog SDK.
This module defines custom exceptions and utility functions for error handling in the DataFog SDK.
"""
from typing import Optional
class DataFogException(Exception):
"""
Base exception for DataFog SDK.
Attributes:
message (str): The error message.
status_code (int, optional): The HTTP status code associated with the error.
"""
def __init__(self, message: str, status_code: Optional[int] = None):
"""
Initialize a DataFogException.
Args:
message (str): The error message.
status_code (int, optional): The HTTP status code associated with the error.
"""
self.message = message
self.status_code = status_code
super().__init__(self.message)
class BadRequestError(DataFogException):
"""
Exception raised for 400 Bad Request errors.
Inherits from DataFogException and sets the status code to 400.
"""
def __init__(self, message: str):
"""
Initialize a BadRequestError.
Args:
message (str): The error message.
"""
super().__init__(message, status_code=400)
class UnprocessableEntityError(DataFogException):
"""
Exception raised for 422 Unprocessable Entity errors.
Inherits from DataFogException and sets the status code to 422.
"""
def __init__(self, message: str):
"""
Initialize an UnprocessableEntityError.
Args:
message (str): The error message.
"""
super().__init__(message, status_code=422)
class EngineNotAvailable(DataFogException):
"""Raised when a requested detection engine dependency is unavailable."""
def __init__(self, message: str):
super().__init__(message, status_code=None)
def raise_for_status_code(status_code: int, error_message: str):
"""
Raise the appropriate exception based on the status code.
Args:
status_code (int): The HTTP status code.
error_message (str): The error message to include in the exception.
Raises:
BadRequestError: If the status code is 400.
UnprocessableEntityError: If the status code is 422.
"""
if status_code == 400:
raise BadRequestError(error_message)
elif status_code == 422:
raise UnprocessableEntityError(error_message)