Skip to content

api_exceptions

APIExceptions

Bases: CoreExceptions

API specific exceptions that inherit from CoreExceptions.

All API related custom exceptions should be raised through this class.

Source code in libs\cafex_api\src\cafex_api\api_exceptions.py
  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
class APIExceptions(CoreExceptions):
    """API specific exceptions that inherit from CoreExceptions.

    All API related custom exceptions should be raised through this
    class.
    """

    def __init__(self):
        super().__init__()

    def raise_null_response_object(
        self,
        insert_report: bool = True,
        trim_log: bool = True,
        log_local: bool = True,
        fail_test: bool = True,
    ) -> None:
        """Raise exception when response object is null.

        This method handles cases where an API response object is entirely null,
        different from cases where the response exists but contains null values.

        Args:
            insert_report: Whether to add exception details to the test report
            trim_log: If True, includes only application frames in stack trace
            log_local: Whether to enable local logging of the exception
            fail_test: If True, marks the current test as failed

        Example:
            # In your API test class
            def verify_user_response(self):
                response = self.api_client.get_user_details()
                if not response:
                    self.exceptions.raise_null_response_object(fail_test=True)

        Note:
            Use raise_null_response() for modern implementations.
            This method is maintained for backward compatibility.
        """
        message = "The response object is null"
        self.raise_generic_exception(
            message=message,
            insert_report=insert_report,
            trim_log=trim_log,
            log_local=log_local,
            fail_test=fail_test,
        )

    def raise_null_value(
        self,
        message: str,
        insert_report: bool = True,
        trim_log: bool = True,
        log_local: bool = True,
        fail_test: bool = True,
    ) -> None:
        """Raise exception when a specific value in the API response is null.

        This method handles cases where certain fields or values within an API response
        are null when they are expected to contain data.

        Args:
            message: Custom message describing which value is null
            insert_report: Whether to add exception details to the test report
            trim_log: If True, includes only application frames in stack trace
            log_local: Whether to enable local logging of the exception
            fail_test: If True, marks the current test as failed

        Example:
            # In your API response validation
            def validate_user_data(self, user_response):
                if user_response.get('email') is None:
                    self.exceptions.raise_null_value(
                        message="User email field is null",
                        fail_test=True
                    )
        """
        self.raise_generic_exception(
            message=message,
            insert_report=insert_report,
            trim_log=trim_log,
            log_local=log_local,
            fail_test=fail_test,
        )

    def raise_invalid_value(
        self,
        message: str,
        insert_report: bool = True,
        trim_log: bool = True,
        log_local: bool = True,
        fail_test: bool = True,
    ) -> None:
        """Raise exception when an API response contains invalid data.

        This method is used when a value in the API response is present but does not
        meet the expected format, type, or validation criteria.

        Args:
            message: Description of the invalid value and expected criteria
            insert_report: Whether to add exception details to the test report
            trim_log: If True, includes only application frames in stack trace
            log_local: Whether to enable local logging of the exception
            fail_test: If True, marks the current test as failed

        Example:
            # Validating status in response
            def check_order_status(self, order_response):
                valid_statuses = ['pending', 'completed', 'cancelled']
                if order_response.status not in valid_statuses:
                    self.exceptions.raise_invalid_value(
                        message=f"Invalid order status: {order_response.status}. Expected one of {valid_statuses}",
                        fail_test=False
                    )
        """
        self.raise_generic_exception(
            message=message,
            insert_report=insert_report,
            trim_log=trim_log,
            log_local=log_local,
            fail_test=fail_test,
        )

    def raise_file_not_found(
        self,
        file_path: str,
        insert_report: bool = True,
        trim_log: bool = True,
        log_local: bool = True,
        fail_test: bool = True,
    ) -> None:
        """Raise exception when a required API-related file is not found.

        This method is used when files required for API operations (like request payloads,
        response templates, or configuration files) are missing from the expected location.

        Args:
            file_path: Path of the missing file
            insert_report: Whether to add exception details to the test report
            trim_log: If True, includes only application frames in stack trace
            log_local: Whether to enable local logging of the exception
            fail_test: If True, marks the current test as failed

        Example:
            # Checking for required request payload file
            def load_request_payload(self, test_case):
                payload_path = f"payloads/{test_case}_payload.json"
                if not os.path.exists(payload_path):
                    self.exceptions.raise_file_not_found(
                        file_path=payload_path,
                        fail_test=True
                    )
        """
        message = f"The file {file_path} is not found"
        self.raise_generic_exception(
            message=message,
            insert_report=insert_report,
            trim_log=trim_log,
            log_local=log_local,
            fail_test=fail_test,
        )

    def raise_invalid_status_code(
        self,
        status_code: int,
        expected_code: int,
        insert_report: bool = True,
        trim_log: bool = True,
        log_local: bool = True,
        fail_test: bool = True,
    ) -> None:
        """Raise exception when API response status code doesn't match expected
        value.

        This method handles cases where an API returns a different HTTP status code
        than what was expected for the operation, indicating potential API errors
        or unexpected behavior.

        Args:
            status_code: The actual status code received from the API
            expected_code: The status code that was expected
            insert_report: Whether to add exception details to the test report
            trim_log: If True, includes only application frames in stack trace
            log_local: Whether to enable local logging of the exception
            fail_test: If True, marks the current test as failed

        Example:
            # Validating API response status
            def verify_user_creation(self, response):
                if response.status_code != 201:
                    self.exceptions.raise_invalid_status_code(
                        status_code=response.status_code,
                        expected_code=201,
                        fail_test=True
                    )
        """
        message = f"Invalid status code. Expected: {expected_code}, Got: {status_code}"
        self.raise_generic_exception(
            message=message,
            insert_report=insert_report,
            trim_log=trim_log,
            log_local=log_local,
            fail_test=fail_test,
        )

    def raise_invalid_response_format(
        self,
        expected_format: str,
        insert_report: bool = True,
        trim_log: bool = True,
        log_local: bool = True,
        fail_test: bool = True,
    ) -> None:
        """Raise exception when API response format is invalid.

        This method is used when the structure or format of the API response
        doesn't match the expected schema or data format (e.g., JSON, XML).

        Args:
            expected_format: Description of the expected response format
            insert_report: Whether to add exception details to the test report
            trim_log: If True, includes only application frames in stack trace
            log_local: Whether to enable local logging of the exception
            fail_test: If True, marks the current test as failed

        Example:
            # Validating response format
            def validate_response_format(self, response):
                try:
                    response_data = response.json()
                except ValueError:
                    self.exceptions.raise_invalid_response_format(
                        expected_format="JSON object with user details",
                        fail_test=True
                    )
        """
        message = f"Invalid API response format. Expected: {expected_format}"
        self.raise_generic_exception(
            message=message,
            insert_report=insert_report,
            trim_log=trim_log,
            log_local=log_local,
            fail_test=fail_test,
        )

    def raise_missing_required_field(
        self,
        field_name: str,
        insert_report: bool = True,
        trim_log: bool = True,
        log_local: bool = True,
        fail_test: bool = True,
    ) -> None:
        """Raise exception when a required field is missing from API response.

        This method handles cases where mandatory fields are absent from the API response,
        indicating potential API issues or incomplete data scenarios.

        Args:
            field_name: Name of the missing required field
            insert_report: Whether to add exception details to the test report
            trim_log: If True, includes only application frames in stack trace
            log_local: Whether to enable local logging of the exception
            fail_test: If True, marks the current test as failed

        Example:
            # Checking required fields in user profile response
            def validate_user_profile(self, profile_data):
                if 'user_id' not in profile_data:
                    self.exceptions.raise_missing_required_field(
                        field_name='user_id',
                        fail_test=True
                    )
        """
        message = f"Required field missing in API response: {field_name}"
        self.raise_generic_exception(
            message=message,
            insert_report=insert_report,
            trim_log=trim_log,
            log_local=log_local,
            fail_test=fail_test,
        )

raise_file_not_found(file_path, insert_report=True, trim_log=True, log_local=True, fail_test=True)

Raise exception when a required API-related file is not found.

This method is used when files required for API operations (like request payloads, response templates, or configuration files) are missing from the expected location.

Parameters:

Name Type Description Default
file_path str

Path of the missing file

required
insert_report bool

Whether to add exception details to the test report

True
trim_log bool

If True, includes only application frames in stack trace

True
log_local bool

Whether to enable local logging of the exception

True
fail_test bool

If True, marks the current test as failed

True
Example

Checking for required request payload file

def load_request_payload(self, test_case): payload_path = f"payloads/{test_case}_payload.json" if not os.path.exists(payload_path): self.exceptions.raise_file_not_found( file_path=payload_path, fail_test=True )

Source code in libs\cafex_api\src\cafex_api\api_exceptions.py
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
def raise_file_not_found(
    self,
    file_path: str,
    insert_report: bool = True,
    trim_log: bool = True,
    log_local: bool = True,
    fail_test: bool = True,
) -> None:
    """Raise exception when a required API-related file is not found.

    This method is used when files required for API operations (like request payloads,
    response templates, or configuration files) are missing from the expected location.

    Args:
        file_path: Path of the missing file
        insert_report: Whether to add exception details to the test report
        trim_log: If True, includes only application frames in stack trace
        log_local: Whether to enable local logging of the exception
        fail_test: If True, marks the current test as failed

    Example:
        # Checking for required request payload file
        def load_request_payload(self, test_case):
            payload_path = f"payloads/{test_case}_payload.json"
            if not os.path.exists(payload_path):
                self.exceptions.raise_file_not_found(
                    file_path=payload_path,
                    fail_test=True
                )
    """
    message = f"The file {file_path} is not found"
    self.raise_generic_exception(
        message=message,
        insert_report=insert_report,
        trim_log=trim_log,
        log_local=log_local,
        fail_test=fail_test,
    )

raise_invalid_response_format(expected_format, insert_report=True, trim_log=True, log_local=True, fail_test=True)

Raise exception when API response format is invalid.

This method is used when the structure or format of the API response doesn't match the expected schema or data format (e.g., JSON, XML).

Parameters:

Name Type Description Default
expected_format str

Description of the expected response format

required
insert_report bool

Whether to add exception details to the test report

True
trim_log bool

If True, includes only application frames in stack trace

True
log_local bool

Whether to enable local logging of the exception

True
fail_test bool

If True, marks the current test as failed

True
Example

Validating response format

def validate_response_format(self, response): try: response_data = response.json() except ValueError: self.exceptions.raise_invalid_response_format( expected_format="JSON object with user details", fail_test=True )

Source code in libs\cafex_api\src\cafex_api\api_exceptions.py
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
def raise_invalid_response_format(
    self,
    expected_format: str,
    insert_report: bool = True,
    trim_log: bool = True,
    log_local: bool = True,
    fail_test: bool = True,
) -> None:
    """Raise exception when API response format is invalid.

    This method is used when the structure or format of the API response
    doesn't match the expected schema or data format (e.g., JSON, XML).

    Args:
        expected_format: Description of the expected response format
        insert_report: Whether to add exception details to the test report
        trim_log: If True, includes only application frames in stack trace
        log_local: Whether to enable local logging of the exception
        fail_test: If True, marks the current test as failed

    Example:
        # Validating response format
        def validate_response_format(self, response):
            try:
                response_data = response.json()
            except ValueError:
                self.exceptions.raise_invalid_response_format(
                    expected_format="JSON object with user details",
                    fail_test=True
                )
    """
    message = f"Invalid API response format. Expected: {expected_format}"
    self.raise_generic_exception(
        message=message,
        insert_report=insert_report,
        trim_log=trim_log,
        log_local=log_local,
        fail_test=fail_test,
    )

raise_invalid_status_code(status_code, expected_code, insert_report=True, trim_log=True, log_local=True, fail_test=True)

Raise exception when API response status code doesn't match expected value.

This method handles cases where an API returns a different HTTP status code than what was expected for the operation, indicating potential API errors or unexpected behavior.

Parameters:

Name Type Description Default
status_code int

The actual status code received from the API

required
expected_code int

The status code that was expected

required
insert_report bool

Whether to add exception details to the test report

True
trim_log bool

If True, includes only application frames in stack trace

True
log_local bool

Whether to enable local logging of the exception

True
fail_test bool

If True, marks the current test as failed

True
Example

Validating API response status

def verify_user_creation(self, response): if response.status_code != 201: self.exceptions.raise_invalid_status_code( status_code=response.status_code, expected_code=201, fail_test=True )

Source code in libs\cafex_api\src\cafex_api\api_exceptions.py
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
def raise_invalid_status_code(
    self,
    status_code: int,
    expected_code: int,
    insert_report: bool = True,
    trim_log: bool = True,
    log_local: bool = True,
    fail_test: bool = True,
) -> None:
    """Raise exception when API response status code doesn't match expected
    value.

    This method handles cases where an API returns a different HTTP status code
    than what was expected for the operation, indicating potential API errors
    or unexpected behavior.

    Args:
        status_code: The actual status code received from the API
        expected_code: The status code that was expected
        insert_report: Whether to add exception details to the test report
        trim_log: If True, includes only application frames in stack trace
        log_local: Whether to enable local logging of the exception
        fail_test: If True, marks the current test as failed

    Example:
        # Validating API response status
        def verify_user_creation(self, response):
            if response.status_code != 201:
                self.exceptions.raise_invalid_status_code(
                    status_code=response.status_code,
                    expected_code=201,
                    fail_test=True
                )
    """
    message = f"Invalid status code. Expected: {expected_code}, Got: {status_code}"
    self.raise_generic_exception(
        message=message,
        insert_report=insert_report,
        trim_log=trim_log,
        log_local=log_local,
        fail_test=fail_test,
    )

raise_invalid_value(message, insert_report=True, trim_log=True, log_local=True, fail_test=True)

Raise exception when an API response contains invalid data.

This method is used when a value in the API response is present but does not meet the expected format, type, or validation criteria.

Parameters:

Name Type Description Default
message str

Description of the invalid value and expected criteria

required
insert_report bool

Whether to add exception details to the test report

True
trim_log bool

If True, includes only application frames in stack trace

True
log_local bool

Whether to enable local logging of the exception

True
fail_test bool

If True, marks the current test as failed

True
Example

Validating status in response

def check_order_status(self, order_response): valid_statuses = ['pending', 'completed', 'cancelled'] if order_response.status not in valid_statuses: self.exceptions.raise_invalid_value( message=f"Invalid order status: {order_response.status}. Expected one of {valid_statuses}", fail_test=False )

Source code in libs\cafex_api\src\cafex_api\api_exceptions.py
 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
def raise_invalid_value(
    self,
    message: str,
    insert_report: bool = True,
    trim_log: bool = True,
    log_local: bool = True,
    fail_test: bool = True,
) -> None:
    """Raise exception when an API response contains invalid data.

    This method is used when a value in the API response is present but does not
    meet the expected format, type, or validation criteria.

    Args:
        message: Description of the invalid value and expected criteria
        insert_report: Whether to add exception details to the test report
        trim_log: If True, includes only application frames in stack trace
        log_local: Whether to enable local logging of the exception
        fail_test: If True, marks the current test as failed

    Example:
        # Validating status in response
        def check_order_status(self, order_response):
            valid_statuses = ['pending', 'completed', 'cancelled']
            if order_response.status not in valid_statuses:
                self.exceptions.raise_invalid_value(
                    message=f"Invalid order status: {order_response.status}. Expected one of {valid_statuses}",
                    fail_test=False
                )
    """
    self.raise_generic_exception(
        message=message,
        insert_report=insert_report,
        trim_log=trim_log,
        log_local=log_local,
        fail_test=fail_test,
    )

raise_missing_required_field(field_name, insert_report=True, trim_log=True, log_local=True, fail_test=True)

Raise exception when a required field is missing from API response.

This method handles cases where mandatory fields are absent from the API response, indicating potential API issues or incomplete data scenarios.

Parameters:

Name Type Description Default
field_name str

Name of the missing required field

required
insert_report bool

Whether to add exception details to the test report

True
trim_log bool

If True, includes only application frames in stack trace

True
log_local bool

Whether to enable local logging of the exception

True
fail_test bool

If True, marks the current test as failed

True
Example

Checking required fields in user profile response

def validate_user_profile(self, profile_data): if 'user_id' not in profile_data: self.exceptions.raise_missing_required_field( field_name='user_id', fail_test=True )

Source code in libs\cafex_api\src\cafex_api\api_exceptions.py
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
def raise_missing_required_field(
    self,
    field_name: str,
    insert_report: bool = True,
    trim_log: bool = True,
    log_local: bool = True,
    fail_test: bool = True,
) -> None:
    """Raise exception when a required field is missing from API response.

    This method handles cases where mandatory fields are absent from the API response,
    indicating potential API issues or incomplete data scenarios.

    Args:
        field_name: Name of the missing required field
        insert_report: Whether to add exception details to the test report
        trim_log: If True, includes only application frames in stack trace
        log_local: Whether to enable local logging of the exception
        fail_test: If True, marks the current test as failed

    Example:
        # Checking required fields in user profile response
        def validate_user_profile(self, profile_data):
            if 'user_id' not in profile_data:
                self.exceptions.raise_missing_required_field(
                    field_name='user_id',
                    fail_test=True
                )
    """
    message = f"Required field missing in API response: {field_name}"
    self.raise_generic_exception(
        message=message,
        insert_report=insert_report,
        trim_log=trim_log,
        log_local=log_local,
        fail_test=fail_test,
    )

raise_null_response_object(insert_report=True, trim_log=True, log_local=True, fail_test=True)

Raise exception when response object is null.

This method handles cases where an API response object is entirely null, different from cases where the response exists but contains null values.

Parameters:

Name Type Description Default
insert_report bool

Whether to add exception details to the test report

True
trim_log bool

If True, includes only application frames in stack trace

True
log_local bool

Whether to enable local logging of the exception

True
fail_test bool

If True, marks the current test as failed

True
Example

In your API test class

def verify_user_response(self): response = self.api_client.get_user_details() if not response: self.exceptions.raise_null_response_object(fail_test=True)

Note

Use raise_null_response() for modern implementations. This method is maintained for backward compatibility.

Source code in libs\cafex_api\src\cafex_api\api_exceptions.py
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
def raise_null_response_object(
    self,
    insert_report: bool = True,
    trim_log: bool = True,
    log_local: bool = True,
    fail_test: bool = True,
) -> None:
    """Raise exception when response object is null.

    This method handles cases where an API response object is entirely null,
    different from cases where the response exists but contains null values.

    Args:
        insert_report: Whether to add exception details to the test report
        trim_log: If True, includes only application frames in stack trace
        log_local: Whether to enable local logging of the exception
        fail_test: If True, marks the current test as failed

    Example:
        # In your API test class
        def verify_user_response(self):
            response = self.api_client.get_user_details()
            if not response:
                self.exceptions.raise_null_response_object(fail_test=True)

    Note:
        Use raise_null_response() for modern implementations.
        This method is maintained for backward compatibility.
    """
    message = "The response object is null"
    self.raise_generic_exception(
        message=message,
        insert_report=insert_report,
        trim_log=trim_log,
        log_local=log_local,
        fail_test=fail_test,
    )

raise_null_value(message, insert_report=True, trim_log=True, log_local=True, fail_test=True)

Raise exception when a specific value in the API response is null.

This method handles cases where certain fields or values within an API response are null when they are expected to contain data.

Parameters:

Name Type Description Default
message str

Custom message describing which value is null

required
insert_report bool

Whether to add exception details to the test report

True
trim_log bool

If True, includes only application frames in stack trace

True
log_local bool

Whether to enable local logging of the exception

True
fail_test bool

If True, marks the current test as failed

True
Example

In your API response validation

def validate_user_data(self, user_response): if user_response.get('email') is None: self.exceptions.raise_null_value( message="User email field is null", fail_test=True )

Source code in libs\cafex_api\src\cafex_api\api_exceptions.py
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
def raise_null_value(
    self,
    message: str,
    insert_report: bool = True,
    trim_log: bool = True,
    log_local: bool = True,
    fail_test: bool = True,
) -> None:
    """Raise exception when a specific value in the API response is null.

    This method handles cases where certain fields or values within an API response
    are null when they are expected to contain data.

    Args:
        message: Custom message describing which value is null
        insert_report: Whether to add exception details to the test report
        trim_log: If True, includes only application frames in stack trace
        log_local: Whether to enable local logging of the exception
        fail_test: If True, marks the current test as failed

    Example:
        # In your API response validation
        def validate_user_data(self, user_response):
            if user_response.get('email') is None:
                self.exceptions.raise_null_value(
                    message="User email field is null",
                    fail_test=True
                )
    """
    self.raise_generic_exception(
        message=message,
        insert_report=insert_report,
        trim_log=trim_log,
        log_local=log_local,
        fail_test=fail_test,
    )