|
| 1 | +import re |
| 2 | +from apimatic_core.utilities.api_helper import ApiHelper |
| 3 | + |
1 | 4 |
|
2 | 5 | class ErrorCase: |
3 | 6 |
|
4 | | - def get_description(self): |
5 | | - return self._description |
| 7 | + def is_error_message_template(self): |
| 8 | + """Checks if the set exception message is a template or not. |
6 | 9 |
|
7 | | - def get_exception_type(self): |
8 | | - return self._exception_type |
| 10 | + Returns: |
| 11 | + string: True if the exception message is a template. |
| 12 | + """ |
| 13 | + return True if self._error_message_template else False |
9 | 14 |
|
10 | | - def __init__( |
11 | | - self |
12 | | - ): |
13 | | - self._description = None |
| 15 | + def __init__(self): |
| 16 | + self._error_message = None |
| 17 | + self._error_message_template = None |
14 | 18 | self._exception_type = None |
15 | 19 |
|
16 | | - def description(self, description): |
17 | | - self._description = description |
| 20 | + def error_message(self, error_message): |
| 21 | + """Setter for the error message. |
| 22 | + Args: |
| 23 | + error_message: The simple exception message. |
| 24 | + """ |
| 25 | + self._error_message = error_message |
| 26 | + return self |
| 27 | + |
| 28 | + def error_message_template(self, error_message_template): |
| 29 | + """Setter for the error message template. |
| 30 | + Args: |
| 31 | + error_message_template: The exception message template containing placeholders. |
| 32 | + """ |
| 33 | + self._error_message_template = error_message_template |
18 | 34 | return self |
19 | 35 |
|
20 | 36 | def exception_type(self, exception_type): |
| 37 | + """Setter for the exception type. |
| 38 | + Args: |
| 39 | + exception_type: The exception type to raise. |
| 40 | + """ |
21 | 41 | self._exception_type = exception_type |
22 | 42 | return self |
| 43 | + |
| 44 | + def get_error_message(self, response): |
| 45 | + """Getter for the error message for the exception case. This considers both error message |
| 46 | + and error template message. Error message template has the higher precedence over an error message. |
| 47 | + Args: |
| 48 | + response: The received http response. |
| 49 | +
|
| 50 | + Returns: |
| 51 | + string: The resolved exception message. |
| 52 | + """ |
| 53 | + if self.is_error_message_template(): |
| 54 | + return self._get_resolved_error_message_template(response) |
| 55 | + return self._error_message |
| 56 | + |
| 57 | + def raise_exception(self, response): |
| 58 | + """Raises the exception for the current error case type. |
| 59 | + Args: |
| 60 | + response: The received http response. |
| 61 | + """ |
| 62 | + raise self._exception_type(self.get_error_message(response), response) |
| 63 | + |
| 64 | + def _get_resolved_error_message_template(self, response): |
| 65 | + """Updates all placeholders in the given message template with provided value. |
| 66 | +
|
| 67 | + Args: |
| 68 | + response: The received http response. |
| 69 | +
|
| 70 | + Returns: |
| 71 | + string: The resolved template value. |
| 72 | + """ |
| 73 | + placeholders = re.findall(r'\{\$.*?\}', self._error_message_template) |
| 74 | + |
| 75 | + status_code_placeholder = set(filter(lambda element: element == '{$statusCode}', placeholders)) |
| 76 | + header_placeholders = set(filter(lambda element: element.startswith('{$response.header'), placeholders)) |
| 77 | + body_placeholders = set(filter(lambda element: element.startswith('{$response.body'), placeholders)) |
| 78 | + |
| 79 | + # Handling response code placeholder |
| 80 | + error_message_template = ApiHelper.resolve_template_placeholders(status_code_placeholder, |
| 81 | + str(response.status_code), |
| 82 | + self._error_message_template) |
| 83 | + |
| 84 | + # Handling response header placeholder |
| 85 | + error_message_template = ApiHelper.resolve_template_placeholders(header_placeholders, response.headers, |
| 86 | + error_message_template) |
| 87 | + |
| 88 | + # Handling response body placeholder |
| 89 | + response_payload = ApiHelper.json_deserialize(response.text, as_dict=True) |
| 90 | + error_message_template = ApiHelper.resolve_template_placeholders_using_json_pointer(body_placeholders, |
| 91 | + response_payload, |
| 92 | + error_message_template) |
| 93 | + |
| 94 | + return error_message_template |
0 commit comments